jscript: Added Date constructor object 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;
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 1");
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 1");
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 1");
51 ok(m["0"] === "aaab", "m[0] is not \"ab\"");
52 ok(m["1"] === "ab", "m[1] is not \"ab\"");
53
54 m = "abcabc".match(new RegExp("ab"));
55 ok(typeof(m) === "object", "typeof m is not object");
56 ok(m.length === 1, "m.length is not 1");
57 ok(m["0"] === "ab", "m[0] is not \"ab\"");
58
59 m = "abcabc".match(new RegExp("ab","g"));
60 ok(typeof(m) === "object", "typeof m is not object");
61 ok(m.length === 2, "m.length is not 1");
62 ok(m["0"] === "ab", "m[0] is not \"ab\"");
63 ok(m["1"] === "ab", "m[1] 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 1");
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", "test"));
72 ok(typeof(m) === "object", "typeof m is not object");
73 ok(m.length === 2, "m.length is not 1");
74 ok(m["0"] === "ab", "m[0] is not \"ab\"");
75 ok(m["1"] === "ab", "m[1] is not \"ab\"");
76
77 r = "- [test] -".replace(/\[([^\[]+)\]/g, "success");
78 ok(r === "- success -", "r = " + r + " expected '- success -'");
79
80 r = "[test] [test]".replace(/\[([^\[]+)\]/g, "aa");
81 ok(r === "aa aa", "r = " + r + "aa aa");
82
83 r = "[test] [test]".replace(/\[([^\[]+)\]/, "aa");
84 ok(r === "aa [test]", "r = " + r + " expected 'aa [test]'");
85
86 r = "- [test] -".replace(/\[([^\[]+)\]/g);
87 ok(r === "- undefined -", "r = " + r + " expected '- undefined -'");
88
89 r = "- [test] -".replace(/\[([^\[]+)\]/g, true);
90 ok(r === "- true -", "r = " + r + " expected '- true -'");
91
92 r = "- [test] -".replace(/\[([^\[]+)\]/g, true, "test");
93 ok(r === "- true -", "r = " + r + " expected '- true -'");
94
95 var tmp = 0;
96
97 function replaceFunc1(m, off, str) {
98     ok(arguments.length === 3, "arguments.length = " + arguments.length);
99
100     switch(tmp) {
101     case 0:
102         ok(m === "[test1]", "m = " + m + " expected [test1]");
103         ok(off === 0, "off = " + off + " expected 0");
104         break;
105     case 1:
106         ok(m === "[test2]", "m = " + m + " expected [test2]");
107         ok(off === 8, "off = " + off + " expected 8");
108         break;
109     default:
110         ok(false, "unexpected call");
111     }
112
113     ok(str === "[test1] [test2]", "str = " + arguments[3]);
114     return "r" + tmp++;
115 }
116
117 r = "[test1] [test2]".replace(/\[[^\[]+\]/g, replaceFunc1);
118 ok(r === "r0 r1", "r = " + r + " expected 'r0 r1'");
119
120 tmp = 0;
121
122 function replaceFunc2(m, subm, off, str) {
123     ok(arguments.length === 4, "arguments.length = " + arguments.length);
124
125     switch(tmp) {
126     case 0:
127         ok(subm === "test1", "subm = " + subm);
128         ok(m === "[test1]", "m = " + m + " expected [test1]");
129         ok(off === 0, "off = " + off + " expected 0");
130         break;
131     case 1:
132         ok(subm === "test2", "subm = " + subm);
133         ok(m === "[test2]", "m = " + m + " expected [test2]");
134         ok(off === 8, "off = " + off + " expected 8");
135         break;
136     default:
137         ok(false, "unexpected call");
138     }
139
140     ok(str === "[test1] [test2]", "str = " + arguments[3]);
141     return "r" + tmp++;
142 }
143
144 r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
145 ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
146
147 reportSuccess();