sync jscript_winetest with wine 1.1.27
[reactos.git] / rostests / winetests / jscript / 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;
21
22 m = "abcabc".match(/ca/);
23 ok(typeof(m) === "object", "typeof m is not object");
24 ok(m.length === 1, "m.length is not 1");
25 ok(m["0"] === "ca", "m[0] is not \"ca\"");
26
27 m = "abcabc".match(/ab/);
28 ok(typeof(m) === "object", "typeof m is not object");
29 ok(m.length === 1, "m.length is not 1");
30 ok(m["0"] === "ab", "m[0] is not \"ab\"");
31
32 m = "abcabc".match(/ab/g);
33 ok(typeof(m) === "object", "typeof m is not object");
34 ok(m.length === 2, "m.length is not 2");
35 ok(m["0"] === "ab", "m[0] is not \"ab\"");
36 ok(m["1"] === "ab", "m[1] is not \"ab\"");
37
38 m = "abcabc".match(/Ab/g);
39 ok(typeof(m) === "object", "typeof m is not object");
40 ok(m === null, "m is not null");
41
42 m = "abcabc".match(/Ab/gi);
43 ok(typeof(m) === "object", "typeof m is not object");
44 ok(m.length === 2, "m.length is not 2");
45 ok(m["0"] === "ab", "m[0] is not \"ab\"");
46 ok(m["1"] === "ab", "m[1] is not \"ab\"");
47
48 m = "aaabcabc".match(/a+b/g);
49 ok(typeof(m) === "object", "typeof m is not object");
50 ok(m.length === 2, "m.length is not 2");
51 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
52 ok(m["1"] === "ab", "m[1] is not \"ab\"");
53
54 m = "aaa\\\\cabc".match(/\\/g);
55 ok(typeof(m) === "object", "typeof m is not object");
56 ok(m.length === 2, "m.length is not 2");
57 ok(m["0"] === "\\", "m[0] is not \"\\\"");
58 ok(m["1"] === "\\", "m[1] is not \"\\\"");
59
60 m = "abcabc".match(new RegExp("ab"));
61 ok(typeof(m) === "object", "typeof m is not object");
62 ok(m.length === 1, "m.length is not 1");
63 ok(m["0"] === "ab", "m[0] is not \"ab\"");
64
65 m = "abcabc".match(new RegExp("ab","g"));
66 ok(typeof(m) === "object", "typeof m is not object");
67 ok(m.length === 2, "m.length is not 2");
68 ok(m["0"] === "ab", "m[0] is not \"ab\"");
69 ok(m["1"] === "ab", "m[1] is not \"ab\"");
70
71 m = "abcabc".match(new RegExp(/ab/g));
72 ok(typeof(m) === "object", "typeof m is not object");
73 ok(m.length === 2, "m.length is not 2");
74 ok(m["0"] === "ab", "m[0] is not \"ab\"");
75 ok(m["1"] === "ab", "m[1] is not \"ab\"");
76
77 m = "abcabc".match(new RegExp("ab","g", "test"));
78 ok(typeof(m) === "object", "typeof m is not object");
79 ok(m.length === 2, "m.length is not 2");
80 ok(m["0"] === "ab", "m[0] is not \"ab\"");
81 ok(m["1"] === "ab", "m[1] is not \"ab\"");
82
83 m = "abcabcg".match("ab", "g");
84 ok(typeof(m) === "object", "typeof m is not object");
85 ok(m.length === 1, "m.length is not 1");
86 ok(m["0"] === "ab", "m[0] is not \"ab\"");
87
88 m = "abcabc".match();
89 ok(m === null, "m is not null");
90
91 r = "- [test] -".replace(/\[([^\[]+)\]/g, "success");
92 ok(r === "- success -", "r = " + r + " expected '- success -'");
93
94 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
95 ok(r === "aa aa", "r = " + r + "aa aa");
96
97 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
98 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
99
100 r = "- [test] -".replace(/\[([^\[]+)\]/g);
101 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
102
103 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
104 ok(r === "- true -", "r = " + r + " expected '- true -'");
105
106 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
107 ok(r === "- true -", "r = " + r + " expected '- true -'");
108
109 var tmp = 0;
110
111 function replaceFunc1(m, off, str) {
112 ok(arguments.length === 3, "arguments.length = " + arguments.length);
113
114 switch(tmp) {
115 case 0:
116 ok(m === "[test1]", "m = " + m + " expected [test1]");
117 ok(off === 0, "off = " + off + " expected 0");
118 break;
119 case 1:
120 ok(m === "[test2]", "m = " + m + " expected [test2]");
121 ok(off === 8, "off = " + off + " expected 8");
122 break;
123 default:
124 ok(false, "unexpected call");
125 }
126
127 ok(str === "[test1] [test2]", "str = " + arguments[3]);
128 return "r" + tmp++;
129 }
130
131 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
132 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
133
134 tmp = 0;
135
136 function replaceFunc2(m, subm, off, str) {
137 ok(arguments.length === 4, "arguments.length = " + arguments.length);
138
139 switch(tmp) {
140 case 0:
141 ok(subm === "test1", "subm = " + subm);
142 ok(m === "[test1]", "m = " + m + " expected [test1]");
143 ok(off === 0, "off = " + off + " expected 0");
144 break;
145 case 1:
146 ok(subm === "test2", "subm = " + subm);
147 ok(m === "[test2]", "m = " + m + " expected [test2]");
148 ok(off === 8, "off = " + off + " expected 8");
149 break;
150 default:
151 ok(false, "unexpected call");
152 }
153
154 ok(str === "[test1] [test2]", "str = " + arguments[3]);
155 return "r" + tmp++;
156 }
157
158 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
159 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
160
161 r = "1,,2,3".split(/,+/g);
162 ok(r.length === 3, "r.length = " + r.length);
163 ok(r[0] === "1", "r[0] = " + r[0]);
164 ok(r[1] === "2", "r[1] = " + r[1]);
165 ok(r[2] === "3", "r[2] = " + r[2]);
166
167 r = "1,,2,3".split(/,+/);
168 ok(r.length === 3, "r.length = " + r.length);
169 ok(r[0] === "1", "r[0] = " + r[0]);
170 ok(r[1] === "2", "r[1] = " + r[1]);
171 ok(r[2] === "3", "r[2] = " + r[2]);
172
173 r = "1,,2,".split(/,+/);
174 ok(r.length === 2, "r.length = " + r.length);
175 ok(r[0] === "1", "r[0] = " + r[0]);
176 ok(r[1] === "2", "r[1] = " + r[1]);
177
178 reportSuccess();