fac111227fea618327fec29023b8c261b31c7665
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\"");
117 m
= "abcabc".match(/Ab/g);
118 ok(typeof(m
) === "object", "typeof m is not object");
119 ok(m
=== null, "m is not null");
121 m
= "abcabc".match(/Ab/gi);
122 ok(typeof(m
) === "object", "typeof m is not object");
123 ok(m
.length
=== 2, "m.length is not 2");
124 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
125 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
127 m
= "aaabcabc".match(/a+b/g);
128 ok(typeof(m
) === "object", "typeof m is not object");
129 ok(m
.length
=== 2, "m.length is not 2");
130 ok(m
["0"] === "aaab", "m[0] is not \"ab\"");
131 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
133 m
= "aaa\\\\cabc".match(/\\/g
);
134 ok(typeof(m
) === "object", "typeof m is not object");
135 ok(m
.length
=== 2, "m.length is not 2");
136 ok(m
["0"] === "\\", "m[0] is not \"\\\"");
137 ok(m
["1"] === "\\", "m[1] is not \"\\\"");
139 m
= "abcabc".match(new RegExp("ab"));
140 ok(typeof(m
) === "object", "typeof m is not object");
141 ok(m
.length
=== 1, "m.length is not 1");
142 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
144 m
= "abcabc".match(new RegExp("ab","g"));
145 ok(typeof(m
) === "object", "typeof m is not object");
146 ok(m
.length
=== 2, "m.length is not 2");
147 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
148 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
150 m
= "abcabc".match(new RegExp(/ab/g));
151 ok(typeof(m
) === "object", "typeof m is not object");
152 ok(m
.length
=== 2, "m.length is not 2");
153 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
154 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
156 m
= "abcabc".match(new RegExp("ab","g", "test"));
157 ok(typeof(m
) === "object", "typeof m is not object");
158 ok(m
.length
=== 2, "m.length is not 2");
159 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
160 ok(m
["1"] === "ab", "m[1] is not \"ab\"");
162 m
= "abcabcg".match("ab", "g");
163 ok(typeof(m
) === "object", "typeof m is not object");
164 ok(m
.length
=== 1, "m.length is not 1");
165 ok(m
["0"] === "ab", "m[0] is not \"ab\"");
167 m
= "abcabc".match();
168 ok(m
=== null, "m is not null");
170 r
= "- [test] -".replace(re
= /\[([^\[]+)\]/g, "success");
171 ok(r
=== "- success -", "r = " + r
+ " expected '- success -'");
172 ok(re
.lastIndex
=== 8, "re.lastIndex = " + re
.lastIndex
);
174 r
= "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
175 ok(r
=== "aa aa", "r = " + r
+ "aa aa");
177 r
= "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
178 ok(r
=== "aa [test]", "r = " + r
+ " expected 'aa [test]'");
180 r
= "- [test] -".replace(/\[([^\[]+)\]/g);
181 ok(r
=== "- undefined -", "r = " + r
+ " expected '- undefined -'");
183 r
= "- [test] -".replace(/\[([^\[]+)\]/g, true);
184 ok(r
=== "- true -", "r = " + r
+ " expected '- true -'");
186 r
= "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
187 ok(r
=== "- true -", "r = " + r
+ " expected '- true -'");
191 function replaceFunc1(m
, off
, str
) {
192 ok(arguments
.length
=== 3, "arguments.length = " + arguments
.length
);
196 ok(m
=== "[test1]", "m = " + m
+ " expected [test1]");
197 ok(off
=== 0, "off = " + off
+ " expected 0");
200 ok(m
=== "[test2]", "m = " + m
+ " expected [test2]");
201 ok(off
=== 8, "off = " + off
+ " expected 8");
204 ok(false, "unexpected call");
207 ok(str
=== "[test1] [test2]", "str = " + arguments
[3]);
211 r
= "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1
);
212 ok(r
=== "r0 r1", "r = " + r
+ " expected 'r0 r1'");
216 function replaceFunc2(m
, subm
, off
, str
) {
217 ok(arguments
.length
=== 4, "arguments.length = " + arguments
.length
);
221 ok(subm
=== "test1", "subm = " + subm
);
222 ok(m
=== "[test1]", "m = " + m
+ " expected [test1]");
223 ok(off
=== 0, "off = " + off
+ " expected 0");
226 ok(subm
=== "test2", "subm = " + subm
);
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, replaceFunc2
);
239 ok(r
=== "r0 r1", "r = '" + r
+ "' expected 'r0 r1'");
241 r
= "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2");
242 ok(r
=== "$1-$11,$1-$22", "r = '" + r
+ "' expected '$1-$11,$1-$22'");
244 r
= "abc &1 123".replace(/(\&(\d))/g, "$&");
245 ok(r
=== "abc &1 123", "r = '" + r
+ "' expected 'abc &1 123'");
247 r
= "abc &1 123".replace(/(\&(\d))/g, "$'");
248 ok(r
=== "abc 123 123", "r = '" + r
+ "' expected 'abc 123 123'");
250 r
= "abc &1 123".replace(/(\&(\d))/g, "$`");
251 ok(r
=== "abc abc 123", "r = '" + r
+ "' expected 'abc abc 123'");
253 r
= "abc &1 123".replace(/(\&(\d))/g, "$3");
254 ok(r
=== "abc $3 123", "r = '" + r
+ "' expected 'abc $3 123'");
256 r
= "abc &1 123".replace(/(\&(\d))/g, "$");
257 ok(r
=== "abc $ 123", "r = '" + r
+ "' expected 'abc $ 123'");
259 r
= "abc &1 123".replace(/(\&(\d))/g, "$a");
260 ok(r
=== "abc $a 123", "r = '" + r
+ "' expected 'abc $a 123'");
262 r
= "abc &1 123".replace(/(\&(\d))/g, "$11");
263 ok(r
=== "abc &11 123", "r = '" + r
+ "' expected 'abc &11 123'");
265 r
= "abc &1 123".replace(/(\&(\d))/g, "$0");
266 ok(r
=== "abc $0 123", "r = '" + r
+ "' expected 'abc $0 123'");
268 r
= "1 2 3".replace("2", "$&");
269 ok(r
=== "1 $& 3", "r = '" + r
+ "' expected '1 $& 3'");
271 r
= "1 2 3".replace("2", "$'");
272 ok(r
=== "1 $' 3", "r = '" + r
+ "' expected '1 $' 3'");
274 r
= "1,,2,3".split(/,+/g);
275 ok(r
.length
=== 3, "r.length = " + r
.length
);
276 ok(r
[0] === "1", "r[0] = " + r
[0]);
277 ok(r
[1] === "2", "r[1] = " + r
[1]);
278 ok(r
[2] === "3", "r[2] = " + r
[2]);
280 r
= "1,,2,3".split(/,+/);
281 ok(r
.length
=== 3, "r.length = " + r
.length
);
282 ok(r
[0] === "1", "r[0] = " + r
[0]);
283 ok(r
[1] === "2", "r[1] = " + r
[1]);
284 ok(r
[2] === "3", "r[2] = " + r
[2]);
286 r
= "1,,2,".split(/,+/);
287 ok(r
.length
=== 2, "r.length = " + r
.length
);
288 ok(r
[0] === "1", "r[0] = " + r
[0]);
289 ok(r
[1] === "2", "r[1] = " + r
[1]);
292 r
= "1,,2,".split(re
);
293 ok(r
.length
=== 2, "r.length = " + r
.length
);
294 ok(r
[0] === "1", "r[0] = " + r
[0]);
295 ok(r
[1] === "2", "r[1] = " + r
[1]);
296 ok(re
.lastIndex
=== 5, "re.lastIndex = " + re
.lastIndex
);
299 r
= "1,,2,".split(re
);
300 ok(r
.length
=== 2, "r.length = " + r
.length
);
301 ok(r
[0] === "1", "r[0] = " + r
[0]);
302 ok(r
[1] === "2", "r[1] = " + r
[1]);
303 ok(re
.lastIndex
=== 5, "re.lastIndex = " + re
.lastIndex
);
305 r
= "1 12 \t3".split(re
= /\s+/).join(";");
306 ok(r
=== "1;12;3", "r = " + r
);
307 ok(re
.lastIndex
=== 6, "re.lastIndex = " + re
.lastIndex
);
309 r
= "123".split(re
= /\s+/).join(";");
310 ok(r
=== "123", "r = " + r
);
311 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
313 /* another standard violation */
314 r
= "1 12 \t3".split(re
= /(\s)+/g).join(";");
315 ok(r
=== "1;12;3", "r = " + r
);
316 ok(re
.lastIndex
=== 6, "re.lastIndex = " + re
.lastIndex
);
320 r
= "1,,2,".split(re
);
321 ok(r
.length
=== 2, "r.length = " + r
.length
);
322 ok(r
[0] === "1", "r[0] = " + r
[0]);
323 ok(r
[1] === "2", "r[1] = " + r
[1]);
324 ok(re
.lastIndex
=== 5, "re.lastIndex = " + re
.lastIndex
);
327 ok(re
.source
=== "abc[^d]", "re.source = '" + re
.source
+ "', expected 'abc[^d]'");
330 ok(re
.source
=== "a\\bc[^d]", "re.source = '" + re
.source
+ "', expected 'a\\bc[^d]'");
333 ok(re
=== RegExp(re
), "re !== RegExp(re)");
335 re
= RegExp("abc[^d]", "g");
336 ok(re
.source
=== "abc[^d]", "re.source = '" + re
.source
+ "', expected 'abc[^d]'");
339 ok(re
=== RegExp(re
, undefined), "re !== RegExp(re, undefined)");
342 ok(re
=== RegExp(re
, undefined, 1), "re !== RegExp(re, undefined, 1)");
345 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
348 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
+ " expected 2");
349 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
352 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
353 ok(m
=== null, "m = " + m
+ " expected null");
356 m
= re
.exec(" a a ");
357 ok(re
.lastIndex
=== 4, "re.lastIndex = " + re
.lastIndex
+ " expected 4");
358 ok(m
.index
=== 3, "m.index = " + m
.index
+ " expected 3");
361 ok(re
.lastIndex
=== "2", "re.lastIndex = " + re
.lastIndex
+ " expected '2'");
362 m
= re
.exec(" a a ");
363 ok(re
.lastIndex
=== 4, "re.lastIndex = " + re
.lastIndex
+ " expected 4");
364 ok(m
.index
=== 3, "m.index = " + m
.index
+ " expected 3");
367 var obj
= new Object();
368 obj
.valueOf = function() { return li
; };
371 ok(re
.lastIndex
=== obj
, "re.lastIndex = " + re
.lastIndex
+ " expected obj");
373 m
= re
.exec(" a a ");
374 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
+ " expected 2");
375 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
378 re
.lastIndex
= "test";
379 ok(re
.lastIndex
=== "test", "re.lastIndex = " + re
.lastIndex
+ " expected 'test'");
380 m
= re
.exec(" a a ");
381 ok(re
.lastIndex
=== 2 || re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 2 or 0");
382 if(re
.lastIndex
!= 0)
383 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
385 ok(m
=== null, "m = " + m
+ " expected null");
389 ok(re
.lastIndex
=== 3.9, "re.lastIndex = " + re
.lastIndex
+ " expected 3.9");
390 m
= re
.exec(" a a ");
391 ok(re
.lastIndex
=== 4, "re.lastIndex = " + re
.lastIndex
+ " expected 4");
392 ok(m
.index
=== 3, "m.index = " + m
.index
+ " expected 3");
394 obj
.valueOf = function() { throw 0; }
396 ok(re
.lastIndex
=== obj
, "unexpected re.lastIndex");
397 m
= re
.exec(" a a ");
398 ok(re
.lastIndex
=== 2, "re.lastIndex = " + re
.lastIndex
+ " expected 2");
399 ok(m
.index
=== 1, "m.index = " + m
.index
+ " expected 1");
402 ok(re
.lastIndex
=== -3, "re.lastIndex = " + re
.lastIndex
+ " expected -3");
403 m
= re
.exec(" a a ");
404 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
405 ok(m
=== null, "m = " + m
+ " expected null");
408 ok(re
.lastIndex
=== -1, "re.lastIndex = " + re
.lastIndex
+ " expected -1");
410 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
+ " expected 0");
411 ok(m
=== null, "m = " + m
+ " expected null");
414 i
= 'baacd'.search(re
);
415 ok(i
=== 1, "'baacd'.search(re) = " + i
);
416 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
419 i
= 'baacdaa'.search(re
);
420 ok(i
=== 1, "'baacd'.search(re) = " + i
);
421 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
424 i
= 'baacd'.search(re
);
425 ok(i
=== 1, "'baacd'.search(re) = " + i
);
426 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
429 i
= 'baacdaa'.search(re
);
430 ok(i
=== 1, "'baacd'.search(re) = " + i
);
431 ok(re
.lastIndex
=== 3, "re.lastIndex = " + re
.lastIndex
);
435 i
= 'abc'.search(re
);
436 ok(i
=== -1, "'abc'.search(/d/g) = " + i
);
437 ok(re
.lastIndex
=== 0, "re.lastIndex = " + re
.lastIndex
);
439 i
= 'abcdde'.search(/[df]/);
440 ok(i
=== 3, "'abc'.search(/[df]/) = " + i
);
442 i
= 'abcdde'.search(/[df]/, "a");
443 ok(i
=== 3, "'abc'.search(/[df]/) = " + i
);
445 i
= 'abcdde'.search("[df]");
446 ok(i
=== 3, "'abc'.search(/d*/) = " + i
);
449 toString: function() { return "abc"; }
451 i
= String
.prototype.search
.call(obj
, "b");
452 ok(i
=== 1, "String.prototype.seatch.apply(obj, 'b') = " + i
);
454 i
= " undefined ".search();
455 ok(i
=== null, "' undefined '.search() = " + i
);