2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
25 m
= re
.exec(" aabaaa");
26 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
27 ok(m
.index
=== 1, "m.index = " + m
.index
);
28 ok(m
.input
=== " aabaaa", "m.input = " + m
.input
);
29 ok(m
.length
=== 1, "m.length = " + m
.length
);
30 ok(m
[0] === "aa", "m[0] = " + m
[0]);
32 m
= re
.exec(" aabaaa");
33 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
34 ok(m
.index
=== 1, "m.index = " + m
.index
);
35 ok(m
.input
=== " aabaaa", "m.input = " + m
.input
);
36 ok(m
.length
=== 1, "m.length = " + m
.length
);
37 ok(m
[0] === "aa", "m[0] = " + m
[0]);
40 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
42 m
= re
.exec(" aabaaa");
43 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
44 ok(m
.index
=== 1, "m.index = " + m
.index
);
45 ok(m
.input
=== " aabaaa", "m.input = " + m
.input
);
46 ok(m
.length
=== 1, "m.length = " + m
.length
);
47 ok(m
[0] === "aa", "m[0] = " + m
[0]);
49 m
= re
.exec(" aabaaa");
50 ok(re
.lastIndex
=== 7, "re.lastIndex = " + re
.lastIndex
);
51 ok(m
.index
=== 4, "m.index = " + m
.index
);
52 ok(m
.input
=== " aabaaa", "m.input = " + m
.input
);
53 ok(m
.length
=== 1, "m.length = " + m
.length
);
54 ok(m
[0] === "aaa", "m[0] = " + m
[0]);
56 m
= re
.exec(" aabaaa");
57 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
58 ok(m
=== null, "m is not null");
61 ok(re
.lastIndex
=== 16, "re.lastIndex = " + re
.lastIndex
);
64 ok(m
=== null, "m is not null");
65 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
68 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
);
71 ok(m
=== null, "m is not null");
72 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
74 m
= /(a|b)+|(c)/.exec("aa");
75 ok(m
[0] === "aa", "m[0] = " + m
[0]);
76 ok(m
[1] === "a", "m[1] = " + m
[1]);
77 ok(m
[2] === "", "m[2] = " + m
[2]);
80 ok(b
=== true, "re.test(' a ') returned " + b
);
81 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
84 ok(b
=== false, "re.test(' a ') returned " + b
);
85 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
88 m
= re
.exec(" [test] ");
89 ok(re
.lastIndex
=== 7, "re.lastIndex = " + re
.lastIndex
);
90 ok(m
.index
=== 1, "m.index = " + m
.index
);
91 ok(m
.input
=== " [test] ", "m.input = " + m
.input
);
92 ok(m
.length
=== 2, "m.length = " + m
.length
);
93 ok(m
[0] === "[test]", "m[0] = " + m
[0]);
94 ok(m
[1] === "test", "m[1] = " + m
[1]);
97 ok(b
=== true, "/a*/.test() returned " + b
);
99 m
= "abcabc".match(re
= /ca/);
100 ok(typeof(m
) === "object", "typeof m is not object");
101 ok(m
.length
=== 1, "m.length is not 1");
102 ok(m
["0"] === "ca", "m[0] is not \"ca\"");
103 ok(m
.constructor === Array
, "unexpected m.constructor");
104 ok(re
.lastIndex
=== 4, "re.lastIndex = " + re
.lastIndex
);
106 m
= "abcabc".match(/ab/);
107 ok(typeof(m
) === "object", "typeof m is not object");
108 ok(m
.length
=== 1, "m.length is not 1");
109 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
111 m
= "abcabc".match(/ab/g);
112 ok(typeof(m
) === "object", "typeof m is not object");
113 ok(m
.length
=== 2, "m.length is not 2");
114 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
115 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
116 /* ok(m.input === "abcabc", "m.input = " + m.input); */
118 m
= "abcabc".match(/Ab/g);
119 ok(typeof(m
) === "object", "typeof m is not object");
120 ok(m
=== null, "m is not null");
122 m
= "abcabc".match(/Ab/gi);
123 ok(typeof(m
) === "object", "typeof m is not object");
124 ok(m
.length
=== 2, "m.length is not 2");
125 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
126 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
128 m
= "aaabcabc".match(/a+b/g);
129 ok(typeof(m
) === "object", "typeof m is not object");
130 ok(m
.length
=== 2, "m.length is not 2");
131 ok(m
["0"] === "aaab", "m[0] is not \"ab\"");
132 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
134 m
= "aaa\\\\cabc".match(/\\/g
);
135 ok(typeof(m
) === "object", "typeof m is not object");
136 ok(m
.length
=== 2, "m.length is not 2");
137 ok(m
["0"] === "\\", "m[0] is not \"\\\"");
138 ok(m
["1"] === "\\", "m[1] is not \"\\\"");
140 m
= "abcabc".match(new RegExp("ab"));
141 ok(typeof(m
) === "object", "typeof m is not object");
142 ok(m
.length
=== 1, "m.length is not 1");
143 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
145 m
= "abcabc".match(new RegExp("ab","g"));
146 ok(typeof(m
) === "object", "typeof m is not object");
147 ok(m
.length
=== 2, "m.length is not 2");
148 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
149 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
151 m
= "abcabc".match(new RegExp(/ab/g));
152 ok(typeof(m
) === "object", "typeof m is not object");
153 ok(m
.length
=== 2, "m.length is not 2");
154 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
155 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
157 m
= "abcabc".match(new RegExp("ab","g", "test"));
158 ok(typeof(m
) === "object", "typeof m is not object");
159 ok(m
.length
=== 2, "m.length is not 2");
160 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
161 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
163 m
= "abcabcg".match("ab", "g");
164 ok(typeof(m
) === "object", "typeof m is not object");
165 ok(m
.length
=== 1, "m.length is not 1");
166 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
168 m
= "abcabc".match();
169 ok(m
=== null, "m is not null");
171 m
= "abcabc".match(/(a)(b)cabc/);
172 ok(typeof(m
) === "object", "typeof m is not object");
173 ok(m
.length
=== 3, "m.length is not 3");
174 ok(m
[0] === "abcabc", "m[0] is not \"abc\"");
175 ok(m
[1] === "a", "m[1] is not \"a\"");
176 ok(m
[2] === "b", "m[2] is not \"b\"");
180 m
= "abcabc".match(re
);
181 ok(typeof(m
) === "object", "typeof m is not object");
182 ok(m
.length
=== 2, "m.length = " + m
.length
+ "expected 3");
183 ok(m
[0] === "abcabc", "m[0] is not \"abc\"");
184 ok(m
[1] === "a", "m[1] is not \"a\"");
185 ok(re
.lastIndex
=== 6, "re.lastIndex = " + re
.lastIndex
);
189 m
= "abcabcxxx".match(re
);
190 ok(typeof(m
) === "object", "typeof m is not object");
191 ok(m
.length
=== 2, "m.length = " + m
.length
+ "expected 3");
192 ok(m
[0] === "abcabc", "m[0] is not \"abc\"");
193 ok(m
[1] === "a", "m[1] is not \"a\"");
194 ok(m
.input
=== "abcabcxxx", "m.input = " + m
.input
);
195 ok(re
.lastIndex
=== 6, "re.lastIndex = " + re
.lastIndex
);
197 r
= "- [test] -".replace(re
= /\[([^\[]+)\]/g, "success");
198 ok(r
=== "- success -", "r = " + r
+ " expected '- success -'");
199 ok(re
.lastIndex
=== 8, "re.lastIndex = " + re
.lastIndex
);
201 r
= "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
202 ok(r
=== "aa aa", "r = " + r
+ "aa aa");
204 r
= "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
205 ok(r
=== "aa [test]", "r = " + r
+ " expected 'aa [test]'");
207 r
= "- [test] -".replace(/\[([^\[]+)\]/g);
208 ok(r
=== "- undefined -", "r = " + r
+ " expected '- undefined -'");
210 r
= "- [test] -".replace(/\[([^\[]+)\]/g, true);
211 ok(r
=== "- true -", "r = " + r
+ " expected '- true -'");
213 r
= "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
214 ok(r
=== "- true -", "r = " + r
+ " expected '- true -'");
218 function replaceFunc1(m
, off
, str
) {
219 ok(arguments
.length
=== 3, "arguments.length = " + arguments
.length
);
223 ok(m
=== "[test1]", "m = " + m
+ " expected [test1]");
224 ok(off
=== 0, "off = " + off
+ " expected 0");
227 ok(m
=== "[test2]", "m = " + m
+ " expected [test2]");
228 ok(off
=== 8, "off = " + off
+ " expected 8");
231 ok(false, "unexpected call");
234 ok(str
=== "[test1] [test2]", "str = " + arguments
[3]);
238 r
= "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1
);
239 ok(r
=== "r0 r1", "r = " + r
+ " expected 'r0 r1'");
243 function replaceFunc2(m
, subm
, off
, str
) {
244 ok(arguments
.length
=== 4, "arguments.length = " + arguments
.length
);
248 ok(subm
=== "test1", "subm = " + subm
);
249 ok(m
=== "[test1]", "m = " + m
+ " expected [test1]");
250 ok(off
=== 0, "off = " + off
+ " expected 0");
253 ok(subm
=== "test2", "subm = " + subm
);
254 ok(m
=== "[test2]", "m = " + m
+ " expected [test2]");
255 ok(off
=== 8, "off = " + off
+ " expected 8");
258 ok(false, "unexpected call");
261 ok(str
=== "[test1] [test2]", "str = " + arguments
[3]);
265 r
= "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2
);
266 ok(r
=== "r0 r1", "r = '" + r
+ "' expected 'r0 r1'");
268 r
= "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2");
269 ok(r
=== "$1-$11,$1-$22", "r = '" + r
+ "' expected '$1-$11,$1-$22'");
271 r
= "abc &1 123".replace(/(\&(\d))/g, "$&");
272 ok(r
=== "abc &1 123", "r = '" + r
+ "' expected 'abc &1 123'");
274 r
= "abc &1 123".replace(/(\&(\d))/g, "$'");
275 ok(r
=== "abc 123 123", "r = '" + r
+ "' expected 'abc 123 123'");
277 r
= "abc &1 123".replace(/(\&(\d))/g, "$`");
278 ok(r
=== "abc abc 123", "r = '" + r
+ "' expected 'abc abc 123'");
280 r
= "abc &1 123".replace(/(\&(\d))/g, "$3");
281 ok(r
=== "abc $3 123", "r = '" + r
+ "' expected 'abc $3 123'");
283 r
= "abc &1 123".replace(/(\&(\d))/g, "$");
284 ok(r
=== "abc $ 123", "r = '" + r
+ "' expected 'abc $ 123'");
286 r
= "abc &1 123".replace(/(\&(\d))/g, "$a");
287 ok(r
=== "abc $a 123", "r = '" + r
+ "' expected 'abc $a 123'");
289 r
= "abc &1 123".replace(/(\&(\d))/g, "$11");
290 ok(r
=== "abc &11 123", "r = '" + r
+ "' expected 'abc &11 123'");
292 r
= "abc &1 123".replace(/(\&(\d))/g, "$0");
293 ok(r
=== "abc $0 123", "r = '" + r
+ "' expected 'abc $0 123'");
295 r
= "1 2 3".replace("2", "$&");
296 ok(r
=== "1 $& 3", "r = '" + r
+ "' expected '1 $& 3'");
298 r
= "1 2 3".replace("2", "$'");
299 ok(r
=== "1 $' 3", "r = '" + r
+ "' expected '1 $' 3'");
301 r
= "1,,2,3".split(/,+/g);
302 ok(r
.length
=== 3, "r.length = " + r
.length
);
303 ok(r
[0] === "1", "r[0] = " + r
[0]);
304 ok(r
[1] === "2", "r[1] = " + r
[1]);
305 ok(r
[2] === "3", "r[2] = " + r
[2]);
307 r
= "1,,2,3".split(/,+/);
308 ok(r
.length
=== 3, "r.length = " + r
.length
);
309 ok(r
[0] === "1", "r[0] = " + r
[0]);
310 ok(r
[1] === "2", "r[1] = " + r
[1]);
311 ok(r
[2] === "3", "r[2] = " + r
[2]);
313 r
= "1,,2,".split(/,+/);
314 ok(r
.length
=== 2, "r.length = " + r
.length
);
315 ok(r
[0] === "1", "r[0] = " + r
[0]);
316 ok(r
[1] === "2", "r[1] = " + r
[1]);
319 r
= "1,,2,".split(re
);
320 ok(r
.length
=== 2, "r.length = " + r
.length
);
321 ok(r
[0] === "1", "r[0] = " + r
[0]);
322 ok(r
[1] === "2", "r[1] = " + r
[1]);
323 ok(re
.lastIndex
=== 5, "re.lastIndex = " + re
.lastIndex
);
326 r
= "1,,2,".split(re
);
327 ok(r
.length
=== 2, "r.length = " + r
.length
);
328 ok(r
[0] === "1", "r[0] = " + r
[0]);
329 ok(r
[1] === "2", "r[1] = " + r
[1]);
330 ok(re
.lastIndex
=== 5, "re.lastIndex = " + re
.lastIndex
);
332 r
= "1 12 \t3".split(re
= /\s+/).join(";");
333 ok(r
=== "1;12;3", "r = " + r
);
334 ok(re
.lastIndex
=== 6, "re.lastIndex = " + re
.lastIndex
);
336 r
= "123".split(re
= /\s+/).join(";");
337 ok(r
=== "123", "r = " + r
);
338 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
340 /* another standard violation */
341 r
= "1 12 \t3".split(re
= /(\s)+/g).join(";");
342 ok(r
=== "1;12;3", "r = " + r
);
343 ok(re
.lastIndex
=== 6, "re.lastIndex = " + re
.lastIndex
);
347 r
= "1,,2,".split(re
);
348 ok(r
.length
=== 2, "r.length = " + r
.length
);
349 ok(r
[0] === "1", "r[0] = " + r
[0]);
350 ok(r
[1] === "2", "r[1] = " + r
[1]);
351 ok(re
.lastIndex
=== 5, "re.lastIndex = " + re
.lastIndex
);
354 ok(re
.source
=== "abc[^d]", "re.source = '" + re
.source
+ "', expected 'abc[^d]'");
357 ok(re
.source
=== "a\\bc[^d]", "re.source = '" + re
.source
+ "', expected 'a\\bc[^d]'");
360 ok(re
=== RegExp(re
), "re !== RegExp(re)");
362 re
= RegExp("abc[^d]", "g");
363 ok(re
.source
=== "abc[^d]", "re.source = '" + re
.source
+ "', expected 'abc[^d]'");
366 ok(re
=== RegExp(re
, undefined), "re !== RegExp(re, undefined)");
369 ok(re
=== RegExp(re
, undefined, 1), "re !== RegExp(re, undefined, 1)");
372 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
375 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
+ " expected 2");
376 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
379 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
380 ok(m
=== null, "m = " + m
+ " expected null");
383 m
= re
.exec(" a a ");
384 ok(re
.lastIndex
=== 4, "re.lastIndex = " + re
.lastIndex
+ " expected 4");
385 ok(m
.index
=== 3, "m.index = " + m
.index
+ " expected 3");
388 ok(re
.lastIndex
=== "2", "re.lastIndex = " + re
.lastIndex
+ " expected '2'");
389 m
= re
.exec(" a a ");
390 ok(re
.lastIndex
=== 4, "re.lastIndex = " + re
.lastIndex
+ " expected 4");
391 ok(m
.index
=== 3, "m.index = " + m
.index
+ " expected 3");
394 var obj
= new Object();
395 obj
.valueOf = function() { return li
; };
398 ok(re
.lastIndex
=== obj
, "re.lastIndex = " + re
.lastIndex
+ " expected obj");
400 m
= re
.exec(" a a ");
401 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
+ " expected 2");
402 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
405 re
.lastIndex
= "test";
406 ok(re
.lastIndex
=== "test", "re.lastIndex = " + re
.lastIndex
+ " expected 'test'");
407 m
= re
.exec(" a a ");
408 ok(re
.lastIndex
=== 2 || re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 2 or 0");
409 if(re
.lastIndex
!= 0)
410 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
412 ok(m
=== null, "m = " + m
+ " expected null");
416 ok(re
.lastIndex
=== 3.9, "re.lastIndex = " + re
.lastIndex
+ " expected 3.9");
417 m
= re
.exec(" a a ");
418 ok(re
.lastIndex
=== 4, "re.lastIndex = " + re
.lastIndex
+ " expected 4");
419 ok(m
.index
=== 3, "m.index = " + m
.index
+ " expected 3");
421 obj
.valueOf = function() { throw 0; }
423 ok(re
.lastIndex
=== obj
, "unexpected re.lastIndex");
424 m
= re
.exec(" a a ");
425 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
+ " expected 2");
426 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
429 ok(re
.lastIndex
=== -3, "re.lastIndex = " + re
.lastIndex
+ " expected -3");
430 m
= re
.exec(" a a ");
431 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
432 ok(m
=== null, "m = " + m
+ " expected null");
435 ok(re
.lastIndex
=== -1, "re.lastIndex = " + re
.lastIndex
+ " expected -1");
437 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
438 ok(m
=== null, "m = " + m
+ " expected null");
442 ok(re
.lastIndex
=== -3, "re.lastIndex = " + re
.lastIndex
+ " expected -3");
443 m
= re
.exec(" a a ");
444 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
445 ok(m
.index
=== 1, "m = " + m
+ " expected 1");
448 ok(re
.lastIndex
=== -1, "re.lastIndex = " + re
.lastIndex
+ " expected -1");
450 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
451 ok(m
=== null, "m = " + m
+ " expected null");
454 i
= 'baacd'.search(re
);
455 ok(i
=== 1, "'baacd'.search(re) = " + i
);
456 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
459 i
= 'baacdaa'.search(re
);
460 ok(i
=== 1, "'baacd'.search(re) = " + i
);
461 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
464 i
= 'baacd'.search(re
);
465 ok(i
=== 1, "'baacd'.search(re) = " + i
);
466 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
469 i
= 'baacdaa'.search(re
);
470 ok(i
=== 1, "'baacd'.search(re) = " + i
);
471 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
475 i
= 'abc'.search(re
);
476 ok(i
=== -1, "'abc'.search(/d/g) = " + i
);
477 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
479 i
= 'abcdde'.search(/[df]/);
480 ok(i
=== 3, "'abc'.search(/[df]/) = " + i
);
482 i
= 'abcdde'.search(/[df]/, "a");
483 ok(i
=== 3, "'abc'.search(/[df]/) = " + i
);
485 i
= 'abcdde'.search("[df]");
486 ok(i
=== 3, "'abc'.search(/d*/) = " + i
);
489 toString: function() { return "abc"; }
491 i
= String
.prototype.search
.call(obj
, "b");
492 ok(i
=== 1, "String.prototype.seatch.apply(obj, 'b') = " + i
);
494 i
= " undefined ".search();
495 ok(i
=== null, "' undefined '.search() = " + i
);