jscript: Added RegExp.lastIndex implementation.
[wine] / dlls / jscript / tests / regexp.js
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  */
18
19
20 var m, re;
21
22 re = /a+/;
23 ok(re.lastIndex === 0, "re.lastIndex = " + re.lastIndex);
24
25 m = "abcabc".match(/ca/);
26 ok(typeof(m) === "object", "typeof m is not object");
27 ok(m.length === 1, "m.length is not 1");
28 ok(m["0"] === "ca", "m[0] is not \"ca\"");
29
30 m = "abcabc".match(/ab/);
31 ok(typeof(m) === "object", "typeof m is not object");
32 ok(m.length === 1, "m.length is not 1");
33 ok(m["0"] === "ab", "m[0] is not \"ab\"");
34
35 m = "abcabc".match(/ab/g);
36 ok(typeof(m) === "object", "typeof m is not object");
37 ok(m.length === 2, "m.length is not 2");
38 ok(m["0"] === "ab", "m[0] is not \"ab\"");
39 ok(m["1"] === "ab", "m[1] is not \"ab\"");
40
41 m = "abcabc".match(/Ab/g);
42 ok(typeof(m) === "object", "typeof m is not object");
43 ok(m === null, "m is not null");
44
45 m = "abcabc".match(/Ab/gi);
46 ok(typeof(m) === "object", "typeof m is not object");
47 ok(m.length === 2, "m.length is not 2");
48 ok(m["0"] === "ab", "m[0] is not \"ab\"");
49 ok(m["1"] === "ab", "m[1] is not \"ab\"");
50
51 m = "aaabcabc".match(/a+b/g);
52 ok(typeof(m) === "object", "typeof m is not object");
53 ok(m.length === 2, "m.length is not 2");
54 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
55 ok(m["1"] === "ab", "m[1] is not \"ab\"");
56
57 m = "aaa\\\\cabc".match(/\\/g);
58 ok(typeof(m) === "object", "typeof m is not object");
59 ok(m.length === 2, "m.length is not 2");
60 ok(m["0"] === "\\", "m[0] is not \"\\\"");
61 ok(m["1"] === "\\", "m[1] is not \"\\\"");
62
63 m = "abcabc".match(new RegExp("ab"));
64 ok(typeof(m) === "object", "typeof m is not object");
65 ok(m.length === 1, "m.length is not 1");
66 ok(m["0"] === "ab", "m[0] is not \"ab\"");
67
68 m = "abcabc".match(new RegExp("ab","g"));
69 ok(typeof(m) === "object", "typeof m is not object");
70 ok(m.length === 2, "m.length is not 2");
71 ok(m["0"] === "ab", "m[0] is not \"ab\"");
72 ok(m["1"] === "ab", "m[1] is not \"ab\"");
73
74 m = "abcabc".match(new RegExp(/ab/g));
75 ok(typeof(m) === "object", "typeof m is not object");
76 ok(m.length === 2, "m.length is not 2");
77 ok(m["0"] === "ab", "m[0] is not \"ab\"");
78 ok(m["1"] === "ab", "m[1] is not \"ab\"");
79
80 m = "abcabc".match(new RegExp("ab","g", "test"));
81 ok(typeof(m) === "object", "typeof m is not object");
82 ok(m.length === 2, "m.length is not 2");
83 ok(m["0"] === "ab", "m[0] is not \"ab\"");
84 ok(m["1"] === "ab", "m[1] is not \"ab\"");
85
86 m = "abcabcg".match("ab", "g");
87 ok(typeof(m) === "object", "typeof m is not object");
88 ok(m.length === 1, "m.length is not 1");
89 ok(m["0"] === "ab", "m[0] is not \"ab\"");
90
91 m = "abcabc".match();
92 ok(m === null, "m is not null");
93
94 r = "- [test] -".replace(/\[([^\[]+)\]/g, "success");
95 ok(r === "- success -", "r = " + r + " expected '- success -'");
96
97 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
98 ok(r === "aa aa", "r = " + r + "aa aa");
99
100 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
101 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
102
103 r = "- [test] -".replace(/\[([^\[]+)\]/g);
104 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
105
106 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
107 ok(r === "- true -", "r = " + r + " expected '- true -'");
108
109 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
110 ok(r === "- true -", "r = " + r + " expected '- true -'");
111
112 var tmp = 0;
113
114 function replaceFunc1(m, off, str) {
115     ok(arguments.length === 3, "arguments.length = " + arguments.length);
116
117     switch(tmp) {
118     case 0:
119         ok(m === "[test1]", "m = " + m + " expected [test1]");
120         ok(off === 0, "off = " + off + " expected 0");
121         break;
122     case 1:
123         ok(m === "[test2]", "m = " + m + " expected [test2]");
124         ok(off === 8, "off = " + off + " expected 8");
125         break;
126     default:
127         ok(false, "unexpected call");
128     }
129
130     ok(str === "[test1] [test2]", "str = " + arguments[3]);
131     return "r" + tmp++;
132 }
133
134 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
135 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
136
137 tmp = 0;
138
139 function replaceFunc2(m, subm, off, str) {
140     ok(arguments.length === 4, "arguments.length = " + arguments.length);
141
142     switch(tmp) {
143     case 0:
144         ok(subm === "test1", "subm = " + subm);
145         ok(m === "[test1]", "m = " + m + " expected [test1]");
146         ok(off === 0, "off = " + off + " expected 0");
147         break;
148     case 1:
149         ok(subm === "test2", "subm = " + subm);
150         ok(m === "[test2]", "m = " + m + " expected [test2]");
151         ok(off === 8, "off = " + off + " expected 8");
152         break;
153     default:
154         ok(false, "unexpected call");
155     }
156
157     ok(str === "[test1] [test2]", "str = " + arguments[3]);
158     return "r" + tmp++;
159 }
160
161 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
162 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
163
164 r = "$1,$2".replace(/(\$(\d))/g, "$$1-$1$2");
165 ok(r === "$1-$11,$1-$22", "r = '" + r + "' expected '$1-$11,$1-$22'");
166
167 r = "abc &1 123".replace(/(\&(\d))/g, "$&");
168 ok(r === "abc &1 123", "r = '" + r + "' expected 'abc &1 123'");
169
170 r = "abc &1 123".replace(/(\&(\d))/g, "$'");
171 ok(r === "abc  123 123", "r = '" + r + "' expected 'abc  123 123'");
172
173 r = "abc &1 123".replace(/(\&(\d))/g, "$`");
174 ok(r === "abc abc  123", "r = '" + r + "' expected 'abc abc  123'");
175
176 r = "abc &1 123".replace(/(\&(\d))/g, "$3");
177 ok(r === "abc $3 123", "r = '" + r + "' expected 'abc $3 123'");
178
179 r = "abc &1 123".replace(/(\&(\d))/g, "$");
180 ok(r === "abc $ 123", "r = '" + r + "' expected 'abc $ 123'");
181
182 r = "abc &1 123".replace(/(\&(\d))/g, "$a");
183 ok(r === "abc $a 123", "r = '" + r + "' expected 'abc $a 123'");
184
185 r = "abc &1 123".replace(/(\&(\d))/g, "$11");
186 ok(r === "abc &11 123", "r = '" + r + "' expected 'abc &11 123'");
187
188 r = "abc &1 123".replace(/(\&(\d))/g, "$0");
189 ok(r === "abc $0 123", "r = '" + r + "' expected 'abc $0 123'");
190
191 r = "1 2 3".replace("2", "$&");
192 ok(r === "1 $& 3", "r = '" + r + "' expected '1 $& 3'");
193
194 r = "1 2 3".replace("2", "$'");
195 ok(r === "1 $' 3", "r = '" + r + "' expected '1 $' 3'");
196
197 r = "1,,2,3".split(/,+/g);
198 ok(r.length === 3, "r.length = " + r.length);
199 ok(r[0] === "1", "r[0] = " + r[0]);
200 ok(r[1] === "2", "r[1] = " + r[1]);
201 ok(r[2] === "3", "r[2] = " + r[2]);
202
203 r = "1,,2,3".split(/,+/);
204 ok(r.length === 3, "r.length = " + r.length);
205 ok(r[0] === "1", "r[0] = " + r[0]);
206 ok(r[1] === "2", "r[1] = " + r[1]);
207 ok(r[2] === "3", "r[2] = " + r[2]);
208
209 r = "1,,2,".split(/,+/);
210 ok(r.length === 2, "r.length = " + r.length);
211 ok(r[0] === "1", "r[0] = " + r[0]);
212 ok(r[1] === "2", "r[1] = " + r[1]);
213
214 re = /abc[^d]/g;
215 ok(re.source === "abc[^d]", "re.source = '" + re.source + "', expected 'abc[^d]'");
216
217 re = /a\bc[^d]/g;
218 ok(re.source === "a\\bc[^d]", "re.source = '" + re.source + "', expected 'a\\bc[^d]'");
219
220 reportSuccess();