push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / dlls / jscript / tests / regexp.js
blob9a14a934dd8a61abf9b9257f29391d5d3bb86440
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(/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\"");
104 m = "abcabc".match(/ab/);
105 ok(typeof(m) === "object", "typeof m is not object");
106 ok(m.length === 1, "m.length is not 1");
107 ok(m["0"] === "ab", "m[0] is not \"ab\"");
109 m = "abcabc".match(/ab/g);
110 ok(typeof(m) === "object", "typeof m is not object");
111 ok(m.length === 2, "m.length is not 2");
112 ok(m["0"] === "ab", "m[0] is not \"ab\"");
113 ok(m["1"] === "ab", "m[1] is not \"ab\"");
115 m = "abcabc".match(/Ab/g);
116 ok(typeof(m) === "object", "typeof m is not object");
117 ok(m === null, "m is not null");
119 m = "abcabc".match(/Ab/gi);
120 ok(typeof(m) === "object", "typeof m is not object");
121 ok(m.length === 2, "m.length is not 2");
122 ok(m["0"] === "ab", "m[0] is not \"ab\"");
123 ok(m["1"] === "ab", "m[1] is not \"ab\"");
125 m = "aaabcabc".match(/a+b/g);
126 ok(typeof(m) === "object", "typeof m is not object");
127 ok(m.length === 2, "m.length is not 2");
128 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
129 ok(m["1"] === "ab", "m[1] is not \"ab\"");
131 m = "aaa\\\\cabc".match(/\\/g);
132 ok(typeof(m) === "object", "typeof m is not object");
133 ok(m.length === 2, "m.length is not 2");
134 ok(m["0"] === "\\", "m[0] is not \"\\\"");
135 ok(m["1"] === "\\", "m[1] is not \"\\\"");
137 m = "abcabc".match(new RegExp("ab"));
138 ok(typeof(m) === "object", "typeof m is not object");
139 ok(m.length === 1, "m.length is not 1");
140 ok(m["0"] === "ab", "m[0] is not \"ab\"");
142 m = "abcabc".match(new RegExp("ab","g"));
143 ok(typeof(m) === "object", "typeof m is not object");
144 ok(m.length === 2, "m.length is not 2");
145 ok(m["0"] === "ab", "m[0] is not \"ab\"");
146 ok(m["1"] === "ab", "m[1] is not \"ab\"");
148 m = "abcabc".match(new RegExp(/ab/g));
149 ok(typeof(m) === "object", "typeof m is not object");
150 ok(m.length === 2, "m.length is not 2");
151 ok(m["0"] === "ab", "m[0] is not \"ab\"");
152 ok(m["1"] === "ab", "m[1] is not \"ab\"");
154 m = "abcabc".match(new RegExp("ab","g", "test"));
155 ok(typeof(m) === "object", "typeof m is not object");
156 ok(m.length === 2, "m.length is not 2");
157 ok(m["0"] === "ab", "m[0] is not \"ab\"");
158 ok(m["1"] === "ab", "m[1] is not \"ab\"");
160 m = "abcabcg".match("ab", "g");
161 ok(typeof(m) === "object", "typeof m is not object");
162 ok(m.length === 1, "m.length is not 1");
163 ok(m["0"] === "ab", "m[0] is not \"ab\"");
165 m = "abcabc".match();
166 ok(m === null, "m is not null");
168 r = "- [test] -".replace(/\[([^\[]+)\]/g, "success");
169 ok(r === "- success -", "r = " + r + " expected '- success -'");
171 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
172 ok(r === "aa aa", "r = " + r + "aa aa");
174 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
175 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
177 r = "- [test] -".replace(/\[([^\[]+)\]/g);
178 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
180 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
181 ok(r === "- true -", "r = " + r + " expected '- true -'");
183 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
184 ok(r === "- true -", "r = " + r + " expected '- true -'");
186 var tmp = 0;
188 function replaceFunc1(m, off, str) {
189     ok(arguments.length === 3, "arguments.length = " + arguments.length);
191     switch(tmp) {
192     case 0:
193         ok(m === "[test1]", "m = " + m + " expected [test1]");
194         ok(off === 0, "off = " + off + " expected 0");
195         break;
196     case 1:
197         ok(m === "[test2]", "m = " + m + " expected [test2]");
198         ok(off === 8, "off = " + off + " expected 8");
199         break;
200     default:
201         ok(false, "unexpected call");
202     }
204     ok(str === "[test1] [test2]", "str = " + arguments[3]);
205     return "r" + tmp++;
208 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
209 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
211 tmp = 0;
213 function replaceFunc2(m, subm, off, str) {
214     ok(arguments.length === 4, "arguments.length = " + arguments.length);
216     switch(tmp) {
217     case 0:
218         ok(subm === "test1", "subm = " + subm);
219         ok(m === "[test1]", "m = " + m + " expected [test1]");
220         ok(off === 0, "off = " + off + " expected 0");
221         break;
222     case 1:
223         ok(subm === "test2", "subm = " + subm);
224         ok(m === "[test2]", "m = " + m + " expected [test2]");
225         ok(off === 8, "off = " + off + " expected 8");
226         break;
227     default:
228         ok(false, "unexpected call");
229     }
231     ok(str === "[test1] [test2]", "str = " + arguments[3]);
232     return "r" + tmp++;
235 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
236 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
238 r = "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2");
239 ok(r === "$1-$11,$1-$22", "r = '" + r + "' expected '$1-$11,$1-$22'");
241 r = "abc &1 123".replace(/(\&(\d))/g, "$&");
242 ok(r === "abc &1 123", "r = '" + r + "' expected 'abc &1 123'");
244 r = "abc &1 123".replace(/(\&(\d))/g, "$'");
245 ok(r === "abc  123 123", "r = '" + r + "' expected 'abc  123 123'");
247 r = "abc &1 123".replace(/(\&(\d))/g, "$`");
248 ok(r === "abc abc  123", "r = '" + r + "' expected 'abc abc  123'");
250 r = "abc &1 123".replace(/(\&(\d))/g, "$3");
251 ok(r === "abc $3 123", "r = '" + r + "' expected 'abc $3 123'");
253 r = "abc &1 123".replace(/(\&(\d))/g, "$");
254 ok(r === "abc $ 123", "r = '" + r + "' expected 'abc $ 123'");
256 r = "abc &1 123".replace(/(\&(\d))/g, "$a");
257 ok(r === "abc $a 123", "r = '" + r + "' expected 'abc $a 123'");
259 r = "abc &1 123".replace(/(\&(\d))/g, "$11");
260 ok(r === "abc &11 123", "r = '" + r + "' expected 'abc &11 123'");
262 r = "abc &1 123".replace(/(\&(\d))/g, "$0");
263 ok(r === "abc $0 123", "r = '" + r + "' expected 'abc $0 123'");
265 r = "1 2 3".replace("2", "$&");
266 ok(r === "1 $& 3", "r = '" + r + "' expected '1 $& 3'");
268 r = "1 2 3".replace("2", "$'");
269 ok(r === "1 $' 3", "r = '" + r + "' expected '1 $' 3'");
271 r = "1,,2,3".split(/,+/g);
272 ok(r.length === 3, "r.length = " + r.length);
273 ok(r[0] === "1", "r[0] = " + r[0]);
274 ok(r[1] === "2", "r[1] = " + r[1]);
275 ok(r[2] === "3", "r[2] = " + r[2]);
277 r = "1,,2,3".split(/,+/);
278 ok(r.length === 3, "r.length = " + r.length);
279 ok(r[0] === "1", "r[0] = " + r[0]);
280 ok(r[1] === "2", "r[1] = " + r[1]);
281 ok(r[2] === "3", "r[2] = " + r[2]);
283 r = "1,,2,".split(/,+/);
284 ok(r.length === 2, "r.length = " + r.length);
285 ok(r[0] === "1", "r[0] = " + r[0]);
286 ok(r[1] === "2", "r[1] = " + r[1]);
288 re = /abc[^d]/g;
289 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
291 re = /a\bc[^d]/g;
292 ok(re.source === "a\\bc[^d]", "re.source = '" + re.source + "', expected 'a\\bc[^d]'");
294 re = /abc/;
295 ok(re === RegExp(re), "re !== RegExp(re)");
297 re = RegExp("abc[^d]", "g");
298 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
300 re = /abc/;
301 ok(re === RegExp(re, undefined), "re !== RegExp(re, undefined)");
303 re = /abc/;
304 ok(re === RegExp(re, undefined, 1), "re !== RegExp(re, undefined, 1)");
306 re = /a/g;
307 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
309 m = re.exec(" a   ");
310 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
311 ok(m.index === 1, "m.index = " + m.index + " expected 1");
313 m = re.exec(" a   ");
314 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
315 ok(m === null, "m = " + m + " expected null");
317 re.lastIndex = 2;
318 m = re.exec(" a a ");
319 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
320 ok(m.index === 3, "m.index = " + m.index + " expected 3");
322 re.lastIndex = "2";
323 ok(re.lastIndex === "2", "re.lastIndex = " + re.lastIndex + " expected '2'");
324 m = re.exec(" a a ");
325 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
326 ok(m.index === 3, "m.index = " + m.index + " expected 3");
328 var li = 0;
329 var obj = new Object();
330 obj.valueOf = function() { return li; };
332 re.lastIndex = obj;
333 ok(re.lastIndex === obj, "re.lastIndex = " + re.lastIndex + " expected obj");
334 li = 2;
335 m = re.exec(" a a ");
336 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
337 ok(m.index === 1, "m.index = " + m.index + " expected 1");
339 re.lastIndex = 3;
340 re.lastIndex = "test";
341 ok(re.lastIndex === "test", "re.lastIndex = " + re.lastIndex + " expected 'test'");
342 m = re.exec(" a a ");
343 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
344 ok(m.index === 1, "m.index = " + m.index + " expected 1");
346 re.lastIndex = 0;
347 re.lastIndex = 3.9;
348 ok(re.lastIndex === 3.9, "re.lastIndex = " + re.lastIndex + " expected 3.9");
349 m = re.exec(" a a ");
350 ok(re.lastIndex === 4, "re.lastIndex = " + re.lastIndex + " expected 4");
351 ok(m.index === 3, "m.index = " + m.index + " expected 3");
353 obj.valueOf = function() { throw 0; }
354 re.lastIndex = obj;
355 ok(re.lastIndex === obj, "unexpected re.lastIndex");
356 m = re.exec(" a a ");
357 ok(re.lastIndex === 2, "re.lastIndex = " + re.lastIndex + " expected 2");
358 ok(m.index === 1, "m.index = " + m.index + " expected 1");
360 re.lastIndex = -3;
361 ok(re.lastIndex === -3, "re.lastIndex = " + re.lastIndex + " expected -3");
362 m = re.exec(" a a ");
363 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
364 ok(m === null, "m = " + m + " expected null");
366 re.lastIndex = -1;
367 ok(re.lastIndex === -1, "re.lastIndex = " + re.lastIndex + " expected -1");
368 m = re.exec("  ");
369 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex + " expected 0");
370 ok(m === null, "m = " + m + " expected null");
372 reportSuccess();