push 5b1efc32b5a8acb1d5b5e60584746392dd0c436e
[wine/hacks.git] / dlls / jscript / tests / regexp.js
blob7f5f907b068dba07818309fc7e112d3bae315bc3
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
20 var m, re, b;
22 re = /a+/;
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]);
39 re = /a+/g;
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");
60 re.exec("               a");
61 ok(re.lastIndex === 16, "re.lastIndex = " + re.lastIndex);
63 m = re.exec(" a");
64 ok(m === null, "m is not null");
65 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
67 m = re.exec(" a");
68 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex);
70 m = re.exec();
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]);
79 b = re.test("  a ");
80 ok(b === true, "re.test('  a ') returned " + b);
81 ok(re.lastIndex === 3, "re.lastIndex = " + re.lastIndex);
83 b = re.test(" a ");
84 ok(b === false, "re.test(' a ') returned " + b);
85 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
87 re = /\[([^\[]+)\]/g;
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]);
96 b = /a*/.test();
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(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex);
105 m = "abcabc".match(/ab/);
106 ok(typeof(m) === "object", "typeof m is not object");
107 ok(m.length === 1, "m.length is not 1");
108 ok(m["0"] === "ab", "m[0] is not \"ab\"");
110 m = "abcabc".match(/ab/g);
111 ok(typeof(m) === "object", "typeof m is not object");
112 ok(m.length === 2, "m.length is not 2");
113 ok(m["0"] === "ab", "m[0] is not \"ab\"");
114 ok(m["1"] === "ab", "m[1] is not \"ab\"");
116 m = "abcabc".match(/Ab/g);
117 ok(typeof(m) === "object", "typeof m is not object");
118 ok(m === null, "m is not null");
120 m = "abcabc".match(/Ab/gi);
121 ok(typeof(m) === "object", "typeof m is not object");
122 ok(m.length === 2, "m.length is not 2");
123 ok(m["0"] === "ab", "m[0] is not \"ab\"");
124 ok(m["1"] === "ab", "m[1] is not \"ab\"");
126 m = "aaabcabc".match(/a+b/g);
127 ok(typeof(m) === "object", "typeof m is not object");
128 ok(m.length === 2, "m.length is not 2");
129 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
130 ok(m["1"] === "ab", "m[1] is not \"ab\"");
132 m = "aaa\\\\cabc".match(/\\/g);
133 ok(typeof(m) === "object", "typeof m is not object");
134 ok(m.length === 2, "m.length is not 2");
135 ok(m["0"] === "\\", "m[0] is not \"\\\"");
136 ok(m["1"] === "\\", "m[1] is not \"\\\"");
138 m = "abcabc".match(new RegExp("ab"));
139 ok(typeof(m) === "object", "typeof m is not object");
140 ok(m.length === 1, "m.length is not 1");
141 ok(m["0"] === "ab", "m[0] is not \"ab\"");
143 m = "abcabc".match(new RegExp("ab","g"));
144 ok(typeof(m) === "object", "typeof m is not object");
145 ok(m.length === 2, "m.length is not 2");
146 ok(m["0"] === "ab", "m[0] is not \"ab\"");
147 ok(m["1"] === "ab", "m[1] is not \"ab\"");
149 m = "abcabc".match(new RegExp(/ab/g));
150 ok(typeof(m) === "object", "typeof m is not object");
151 ok(m.length === 2, "m.length is not 2");
152 ok(m["0"] === "ab", "m[0] is not \"ab\"");
153 ok(m["1"] === "ab", "m[1] is not \"ab\"");
155 m = "abcabc".match(new RegExp("ab","g", "test"));
156 ok(typeof(m) === "object", "typeof m is not object");
157 ok(m.length === 2, "m.length is not 2");
158 ok(m["0"] === "ab", "m[0] is not \"ab\"");
159 ok(m["1"] === "ab", "m[1] is not \"ab\"");
161 m = "abcabcg".match("ab", "g");
162 ok(typeof(m) === "object", "typeof m is not object");
163 ok(m.length === 1, "m.length is not 1");
164 ok(m["0"] === "ab", "m[0] is not \"ab\"");
166 m = "abcabc".match();
167 ok(m === null, "m is not null");
169 r = "- [test] -".replace(re = /\[([^\[]+)\]/g, "success");
170 ok(r === "- success -", "r = " + r + " expected '- success -'");
171 ok(re.lastIndex === 8, "re.lastIndex = " + re.lastIndex);
173 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
174 ok(r === "aa aa", "r = " + r + "aa aa");
176 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
177 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
179 r = "- [test] -".replace(/\[([^\[]+)\]/g);
180 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
182 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
183 ok(r === "- true -", "r = " + r + " expected '- true -'");
185 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
186 ok(r === "- true -", "r = " + r + " expected '- true -'");
188 var tmp = 0;
190 function replaceFunc1(m, off, str) {
191     ok(arguments.length === 3, "arguments.length = " + arguments.length);
193     switch(tmp) {
194     case 0:
195         ok(m === "[test1]", "m = " + m + " expected [test1]");
196         ok(off === 0, "off = " + off + " expected 0");
197         break;
198     case 1:
199         ok(m === "[test2]", "m = " + m + " expected [test2]");
200         ok(off === 8, "off = " + off + " expected 8");
201         break;
202     default:
203         ok(false, "unexpected call");
204     }
206     ok(str === "[test1] [test2]", "str = " + arguments[3]);
207     return "r" + tmp++;
210 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
211 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
213 tmp = 0;
215 function replaceFunc2(m, subm, off, str) {
216     ok(arguments.length === 4, "arguments.length = " + arguments.length);
218     switch(tmp) {
219     case 0:
220         ok(subm === "test1", "subm = " + subm);
221         ok(m === "[test1]", "m = " + m + " expected [test1]");
222         ok(off === 0, "off = " + off + " expected 0");
223         break;
224     case 1:
225         ok(subm === "test2", "subm = " + subm);
226         ok(m === "[test2]", "m = " + m + " expected [test2]");
227         ok(off === 8, "off = " + off + " expected 8");
228         break;
229     default:
230         ok(false, "unexpected call");
231     }
233     ok(str === "[test1] [test2]", "str = " + arguments[3]);
234     return "r" + tmp++;
237 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
238 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
240 r = "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2");
241 ok(r === "$1-$11,$1-$22", "r = '" + r + "' expected '$1-$11,$1-$22'");
243 r = "abc &1 123".replace(/(\&(\d))/g, "$&");
244 ok(r === "abc &1 123", "r = '" + r + "' expected 'abc &1 123'");
246 r = "abc &1 123".replace(/(\&(\d))/g, "$'");
247 ok(r === "abc  123 123", "r = '" + r + "' expected 'abc  123 123'");
249 r = "abc &1 123".replace(/(\&(\d))/g, "$`");
250 ok(r === "abc abc  123", "r = '" + r + "' expected 'abc abc  123'");
252 r = "abc &1 123".replace(/(\&(\d))/g, "$3");
253 ok(r === "abc $3 123", "r = '" + r + "' expected 'abc $3 123'");
255 r = "abc &1 123".replace(/(\&(\d))/g, "$");
256 ok(r === "abc $ 123", "r = '" + r + "' expected 'abc $ 123'");
258 r = "abc &1 123".replace(/(\&(\d))/g, "$a");
259 ok(r === "abc $a 123", "r = '" + r + "' expected 'abc $a 123'");
261 r = "abc &1 123".replace(/(\&(\d))/g, "$11");
262 ok(r === "abc &11 123", "r = '" + r + "' expected 'abc &11 123'");
264 r = "abc &1 123".replace(/(\&(\d))/g, "$0");
265 ok(r === "abc $0 123", "r = '" + r + "' expected 'abc $0 123'");
267 r = "1 2 3".replace("2", "$&");
268 ok(r === "1 $& 3", "r = '" + r + "' expected '1 $& 3'");
270 r = "1 2 3".replace("2", "$'");
271 ok(r === "1 $' 3", "r = '" + r + "' expected '1 $' 3'");
273 r = "1,,2,3".split(/,+/g);
274 ok(r.length === 3, "r.length = " + r.length);
275 ok(r[0] === "1", "r[0] = " + r[0]);
276 ok(r[1] === "2", "r[1] = " + r[1]);
277 ok(r[2] === "3", "r[2] = " + r[2]);
279 r = "1,,2,3".split(/,+/);
280 ok(r.length === 3, "r.length = " + r.length);
281 ok(r[0] === "1", "r[0] = " + r[0]);
282 ok(r[1] === "2", "r[1] = " + r[1]);
283 ok(r[2] === "3", "r[2] = " + r[2]);
285 r = "1,,2,".split(/,+/);
286 ok(r.length === 2, "r.length = " + r.length);
287 ok(r[0] === "1", "r[0] = " + r[0]);
288 ok(r[1] === "2", "r[1] = " + r[1]);
290 re = /,+/;
291 r = "1,,2,".split(re);
292 ok(r.length === 2, "r.length = " + r.length);
293 ok(r[0] === "1", "r[0] = " + r[0]);
294 ok(r[1] === "2", "r[1] = " + r[1]);
295 ok(re.lastIndex === 5, "re.lastIndex = " + re.lastIndex);
297 re = /,+/g;
298 r = "1,,2,".split(re);
299 ok(r.length === 2, "r.length = " + r.length);
300 ok(r[0] === "1", "r[0] = " + r[0]);
301 ok(r[1] === "2", "r[1] = " + r[1]);
302 ok(re.lastIndex === 5, "re.lastIndex = " + re.lastIndex);
304 r = "1 12 \t3".split(re = /\s+/).join(";");
305 ok(r === "1;12;3", "r = " + r);
306 ok(re.lastIndex === 6, "re.lastIndex = " + re.lastIndex);
308 r = "123".split(re = /\s+/).join(";");
309 ok(r === "123", "r = " + r);
310 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
312 /* another standard violation */
313 r = "1 12 \t3".split(re = /(\s)+/g).join(";");
314 ok(r === "1;12;3", "r = " + r);
315 ok(re.lastIndex === 6, "re.lastIndex = " + re.lastIndex);
317 re = /,+/;
318 re.lastIndex = 4;
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);
325 re = /abc[^d]/g;
326 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
328 re = /a\bc[^d]/g;
329 ok(re.source === "a\\bc[^d]", "re.source = '" + re.source + "', expected 'a\\bc[^d]'");
331 re = /abc/;
332 ok(re === RegExp(re), "re !== RegExp(re)");
334 re = RegExp("abc[^d]", "g");
335 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
337 re = /abc/;
338 ok(re === RegExp(re, undefined), "re !== RegExp(re, undefined)");
340 re = /abc/;
341 ok(re === RegExp(re, undefined, 1), "re !== RegExp(re, undefined, 1)");
343 re = /a/g;
344 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
346 m = re.exec(" a   ");
347 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
348 ok(m.index === 1, "m.index = " + m.index + " expected 1");
350 m = re.exec(" a   ");
351 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
352 ok(m === null, "m = " + m + " expected null");
354 re.lastIndex = 2;
355 m = re.exec(" a a ");
356 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
357 ok(m.index === 3, "m.index = " + m.index + " expected 3");
359 re.lastIndex = "2";
360 ok(re.lastIndex === "2", "re.lastIndex = " + re.lastIndex + " expected '2'");
361 m = re.exec(" a a ");
362 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
363 ok(m.index === 3, "m.index = " + m.index + " expected 3");
365 var li = 0;
366 var obj = new Object();
367 obj.valueOf = function() { return li; };
369 re.lastIndex = obj;
370 ok(re.lastIndex === obj, "re.lastIndex = " + re.lastIndex + " expected obj");
371 li = 2;
372 m = re.exec(" a a ");
373 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
374 ok(m.index === 1, "m.index = " + m.index + " expected 1");
376 re.lastIndex = 3;
377 re.lastIndex = "test";
378 ok(re.lastIndex === "test", "re.lastIndex = " + re.lastIndex + " expected 'test'");
379 m = re.exec(" a a ");
380 ok(re.lastIndex === 2 || re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 2 or 0");
381 if(re.lastIndex != 0)
382     ok(m.index === 1, "m.index = " + m.index + " expected 1");
383 else
384     ok(m === null, "m = " + m + " expected null");
386 re.lastIndex = 0;
387 re.lastIndex = 3.9;
388 ok(re.lastIndex === 3.9, "re.lastIndex = " + re.lastIndex + " expected 3.9");
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");
393 obj.valueOf = function() { throw 0; }
394 re.lastIndex = obj;
395 ok(re.lastIndex === obj, "unexpected re.lastIndex");
396 m = re.exec(" a a ");
397 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
398 ok(m.index === 1, "m.index = " + m.index + " expected 1");
400 re.lastIndex = -3;
401 ok(re.lastIndex === -3, "re.lastIndex = " + re.lastIndex + " expected -3");
402 m = re.exec(" a a ");
403 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
404 ok(m === null, "m = " + m + " expected null");
406 re.lastIndex = -1;
407 ok(re.lastIndex === -1, "re.lastIndex = " + re.lastIndex + " expected -1");
408 m = re.exec("  ");
409 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
410 ok(m === null, "m = " + m + " expected null");
412 reportSuccess();