Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: test/mjsunit/string-match.js

Issue 60007: Added more tests for string.match. (Closed)
Patch Set: Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 /**
29 * @fileoverview Test String.prototype.match
30 */
31
32
33 function testMatch(name, input, regexp, result, captures, from, to) {
34 var matchResult = input.match(regexp);
35 assertEquals(result, matchResult, name + "-match");
36
37 var match = input.substring(from, to);
38 var preMatch = input.substring(0, from);
39 var postMatch = input.substring(to);
40 var lastParen = captures.length > 0 ? captures[captures.length - 1] : "";
41
42 if (regexp.global) {
43 // Returns array of matched strings.
44 var lastMatch = matchResult[matchResult.length - 1];
45 assertEquals(match, lastMatch, name + "-match-string_g");
46 } else {
47 // Returns array of match and captures.
48 assertEquals(match, matchResult[0], name + "-match-string");
49 assertEquals(captures.length + 1, matchResult.length, name + "-cap-return");
50 for (var i = 1; i < matchResult.length; i++) {
51 assertEquals(captures[i - 1], matchResult[i], name + "-cap-return-" + i);
52 }
53 }
54
55 assertEquals(match, RegExp["$&"], name + "-$&");
56 assertEquals(match, RegExp.lastMatch, name + "-lastMatch");
57
58 assertEquals(undefined, RegExp.$0, name + "-nocapture-10");
59 for (var i = 1; i <= 9; i++) {
60 if (i <= captures.length) {
61 assertEquals(captures[i - 1], RegExp["$" + i], name + "-capture-" + i);
62 } else {
63 assertEquals("", RegExp["$" + i], name + "-nocapture-" + i);
64 }
65 }
66 assertEquals(undefined, RegExp.$10, name + "-nocapture-10");
67
68 assertEquals(input, RegExp.input, name + "-input");
69 assertEquals(input, RegExp.$input, name + "-$input");
70 assertEquals(input, RegExp.$_, name + "-$_");
71
72 assertEquals(preMatch, RegExp["$`"], name + "-$`");
73 assertEquals(preMatch, RegExp.leftContext, name + "-leftContex");
74
75 assertEquals(postMatch, RegExp["$'"], name + "-$'");
76 assertEquals(postMatch, RegExp.rightContext, name + "-rightContex");
77
78 assertEquals(lastParen, RegExp["$+"], name + "-$+");
79 assertEquals(lastParen, RegExp.lastParen, name + "-lastParen");
80
81 }
82
83
84 var stringSample = "A man, a plan, a canal: Panama";
85 var stringSample2 = "Argle bargle glop glyf!";
86 var stringSample3 = "abcdefghijxxxxxxxxxx";
87
88
89 // Non-capturing, non-global regexp.
90
91 var re_nog = /\w+/;
92
93 testMatch("Nonglobal", stringSample, re_nog,
94 ["A"],
95 [], 0, 1);
96
97
98 re_nog.lastIndex = 2;
99
100 testMatch("Nonglobal-ignore-lastIndex", stringSample, re_nog,
101 ["A"],
102 [], 0, 1);
103
104
105 // Capturing non-global regexp.
106
107 var re_multicap = /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/;
108 testMatch("Capture-Nonglobal", stringSample3, re_multicap,
109 ["abcdefghij", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],
110 ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"], 0, 10);
111
112
113 // Global regexp (also check that capture from before are cleared)
114
115 var re = /\w+/g;
116 testMatch("Global", stringSample2, re,
117 ["Argle", "bargle", "glop", "glyf"], [], 18, 22);
118
119 re.lastIndex = 10;
120
121 testMatch("Global-ignore-lastIndex", stringSample2, re,
122 ["Argle", "bargle", "glop", "glyf"], [], 18, 22);
123
124
125 // Capturing global regexp
126
127 var re_cap = /\w(\w*)/g;
128
129 testMatch("Capture-Global", stringSample, re_cap,
130 ["A", "man", "a", "plan", "a", "canal", "Panama"],
131 ["anama"], 24, 30);
132
133
134 // Atom, non-global
135
136 var re_atom = /an/;
137
138 testMatch("Atom", stringSample, re_atom,
139 ["an"],
140 [], 3, 5);
141
142
143 // Atom, global
144
145 var re_atomg = /an/g;
146
147 testMatch("Global-Atom", stringSample, re_atomg,
148 ["an", "an", "an", "an"],
149 [], 25, 27);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698