|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * |
|
8 * Date: 11 Nov 2002 |
|
9 * SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc. |
|
10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=179524 |
|
11 * |
|
12 * Note that when testing str.replace(), we have to be careful if the first |
|
13 * argument provided to str.replace() is not a regexp object. ECMA-262 says |
|
14 * it is NOT converted to one, unlike the case for str.match(), str.search(). |
|
15 * |
|
16 * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21. This means |
|
17 * we have to be careful how we test meta-characters in the first argument |
|
18 * to str.replace(), if that argument is a string - |
|
19 */ |
|
20 //----------------------------------------------------------------------------- |
|
21 var UBound = 0; |
|
22 var BUGNUMBER = 179524; |
|
23 var summary = "Don't crash on extraneous arguments to str.match(), etc."; |
|
24 var status = ''; |
|
25 var statusitems = []; |
|
26 var actual = ''; |
|
27 var actualvalues = []; |
|
28 var expect= ''; |
|
29 var expectedvalues = []; |
|
30 |
|
31 |
|
32 str = 'ABC abc'; |
|
33 var re = /z/ig; |
|
34 |
|
35 status = inSection(1); |
|
36 actual = str.match(re); |
|
37 expect = null; |
|
38 addThis(); |
|
39 |
|
40 status = inSection(2); |
|
41 actual = str.match(re, 'i'); |
|
42 expect = null; |
|
43 addThis(); |
|
44 |
|
45 status = inSection(3); |
|
46 actual = str.match(re, 'g', ''); |
|
47 expect = null; |
|
48 addThis(); |
|
49 |
|
50 status = inSection(4); |
|
51 actual = str.match(re, 'z', new Object(), new Date()); |
|
52 expect = null; |
|
53 addThis(); |
|
54 |
|
55 |
|
56 /* |
|
57 * Now try the same thing with str.search() |
|
58 */ |
|
59 status = inSection(5); |
|
60 actual = str.search(re); |
|
61 expect = -1; |
|
62 addThis(); |
|
63 |
|
64 status = inSection(6); |
|
65 actual = str.search(re, 'i'); |
|
66 expect = -1; |
|
67 addThis(); |
|
68 |
|
69 status = inSection(7); |
|
70 actual = str.search(re, 'g', ''); |
|
71 expect = -1; |
|
72 addThis(); |
|
73 |
|
74 status = inSection(8); |
|
75 actual = str.search(re, 'z', new Object(), new Date()); |
|
76 expect = -1; |
|
77 addThis(); |
|
78 |
|
79 |
|
80 /* |
|
81 * Now try the same thing with str.replace() |
|
82 */ |
|
83 status = inSection(9); |
|
84 actual = str.replace(re, 'Z'); |
|
85 expect = str; |
|
86 addThis(); |
|
87 |
|
88 status = inSection(10); |
|
89 actual = str.replace(re, 'Z', 'i'); |
|
90 expect = str; |
|
91 addThis(); |
|
92 |
|
93 status = inSection(11); |
|
94 actual = str.replace(re, 'Z', 'g', ''); |
|
95 expect = str; |
|
96 addThis(); |
|
97 |
|
98 status = inSection(12); |
|
99 actual = str.replace(re, 'Z', 'z', new Object(), new Date()); |
|
100 expect = str; |
|
101 addThis(); |
|
102 |
|
103 |
|
104 |
|
105 /* |
|
106 * Now test the case where str.match()'s first argument is not a regexp object. |
|
107 * In that case, JS follows ECMA-262 Ed.3 by converting the 1st argument to a |
|
108 * regexp object using the argument as a regexp pattern, but then extends ECMA |
|
109 * by taking any optional 2nd argument to be a regexp flag string (e.g.'ig'). |
|
110 * |
|
111 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10 |
|
112 */ |
|
113 status = inSection(13); |
|
114 actual = str.match('a').toString(); |
|
115 expect = str.match(/a/).toString(); |
|
116 addThis(); |
|
117 |
|
118 status = inSection(14); |
|
119 actual = str.match('a', 'i').toString(); |
|
120 expect = str.match(/a/i).toString(); |
|
121 addThis(); |
|
122 |
|
123 status = inSection(15); |
|
124 actual = str.match('a', 'ig').toString(); |
|
125 expect = str.match(/a/ig).toString(); |
|
126 addThis(); |
|
127 |
|
128 status = inSection(16); |
|
129 actual = str.match('\\s', 'm').toString(); |
|
130 expect = str.match(/\s/m).toString(); |
|
131 addThis(); |
|
132 |
|
133 |
|
134 /* |
|
135 * Now try the previous three cases with extraneous parameters |
|
136 */ |
|
137 status = inSection(17); |
|
138 actual = str.match('a', 'i', 'g').toString(); |
|
139 expect = str.match(/a/i).toString(); |
|
140 addThis(); |
|
141 |
|
142 status = inSection(18); |
|
143 actual = str.match('a', 'ig', new Object()).toString(); |
|
144 expect = str.match(/a/ig).toString(); |
|
145 addThis(); |
|
146 |
|
147 status = inSection(19); |
|
148 actual = str.match('\\s', 'm', 999).toString(); |
|
149 expect = str.match(/\s/m).toString(); |
|
150 addThis(); |
|
151 |
|
152 |
|
153 /* |
|
154 * Try an invalid second parameter (i.e. an invalid regexp flag) |
|
155 */ |
|
156 status = inSection(20); |
|
157 try |
|
158 { |
|
159 actual = str.match('a', 'z').toString(); |
|
160 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; |
|
161 addThis(); |
|
162 } |
|
163 catch (e) |
|
164 { |
|
165 actual = e instanceof SyntaxError; |
|
166 expect = true; |
|
167 addThis(); |
|
168 } |
|
169 |
|
170 |
|
171 |
|
172 /* |
|
173 * Now test str.search() where the first argument is not a regexp object. |
|
174 * The same considerations as above apply - |
|
175 * |
|
176 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16 |
|
177 */ |
|
178 status = inSection(21); |
|
179 actual = str.search('a'); |
|
180 expect = str.search(/a/); |
|
181 addThis(); |
|
182 |
|
183 status = inSection(22); |
|
184 actual = str.search('a', 'i'); |
|
185 expect = str.search(/a/i); |
|
186 addThis(); |
|
187 |
|
188 status = inSection(23); |
|
189 actual = str.search('a', 'ig'); |
|
190 expect = str.search(/a/ig); |
|
191 addThis(); |
|
192 |
|
193 status = inSection(24); |
|
194 actual = str.search('\\s', 'm'); |
|
195 expect = str.search(/\s/m); |
|
196 addThis(); |
|
197 |
|
198 |
|
199 /* |
|
200 * Now try the previous three cases with extraneous parameters |
|
201 */ |
|
202 status = inSection(25); |
|
203 actual = str.search('a', 'i', 'g'); |
|
204 expect = str.search(/a/i); |
|
205 addThis(); |
|
206 |
|
207 status = inSection(26); |
|
208 actual = str.search('a', 'ig', new Object()); |
|
209 expect = str.search(/a/ig); |
|
210 addThis(); |
|
211 |
|
212 status = inSection(27); |
|
213 actual = str.search('\\s', 'm', 999); |
|
214 expect = str.search(/\s/m); |
|
215 addThis(); |
|
216 |
|
217 |
|
218 /* |
|
219 * Try an invalid second parameter (i.e. an invalid regexp flag) |
|
220 */ |
|
221 status = inSection(28); |
|
222 try |
|
223 { |
|
224 actual = str.search('a', 'z'); |
|
225 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; |
|
226 addThis(); |
|
227 } |
|
228 catch (e) |
|
229 { |
|
230 actual = e instanceof SyntaxError; |
|
231 expect = true; |
|
232 addThis(); |
|
233 } |
|
234 |
|
235 |
|
236 |
|
237 /* |
|
238 * Now test str.replace() where the first argument is not a regexp object. |
|
239 * The same considerations as above apply, EXCEPT for meta-characters. |
|
240 * See introduction to testcase above. References: |
|
241 * |
|
242 * http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16 |
|
243 * http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21 |
|
244 */ |
|
245 status = inSection(29); |
|
246 actual = str.replace('a', 'Z'); |
|
247 expect = str.replace(/a/, 'Z'); |
|
248 addThis(); |
|
249 |
|
250 status = inSection(30); |
|
251 actual = str.replace('a', 'Z', 'i'); |
|
252 expect = str.replace(/a/i, 'Z'); |
|
253 addThis(); |
|
254 |
|
255 status = inSection(31); |
|
256 actual = str.replace('a', 'Z', 'ig'); |
|
257 expect = str.replace(/a/ig, 'Z'); |
|
258 addThis(); |
|
259 |
|
260 status = inSection(32); |
|
261 actual = str.replace('\\s', 'Z', 'm'); //<--- NO!!! No meta-characters 1st arg! |
|
262 actual = str.replace(' ', 'Z', 'm'); //<--- Have to do this instead |
|
263 expect = str.replace(/\s/m, 'Z'); |
|
264 addThis(); |
|
265 |
|
266 |
|
267 /* |
|
268 * Now try the previous three cases with extraneous parameters |
|
269 */ |
|
270 status = inSection(33); |
|
271 actual = str.replace('a', 'Z', 'i', 'g'); |
|
272 expect = str.replace(/a/i, 'Z'); |
|
273 addThis(); |
|
274 |
|
275 status = inSection(34); |
|
276 actual = str.replace('a', 'Z', 'ig', new Object()); |
|
277 expect = str.replace(/a/ig, 'Z'); |
|
278 addThis(); |
|
279 |
|
280 status = inSection(35); |
|
281 actual = str.replace('\\s', 'Z', 'm', 999); //<--- NO meta-characters 1st arg! |
|
282 actual = str.replace(' ', 'Z', 'm', 999); //<--- Have to do this instead |
|
283 expect = str.replace(/\s/m, 'Z'); |
|
284 addThis(); |
|
285 |
|
286 |
|
287 /* |
|
288 * Try an invalid third parameter (i.e. an invalid regexp flag) |
|
289 */ |
|
290 status = inSection(36); |
|
291 try |
|
292 { |
|
293 actual = str.replace('a', 'Z', 'z'); |
|
294 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; |
|
295 addThis(); |
|
296 } |
|
297 catch (e) |
|
298 { |
|
299 actual = e instanceof SyntaxError; |
|
300 expect = true; |
|
301 addThis(); |
|
302 } |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 //----------------------------------------------------------------------------- |
|
308 test(); |
|
309 //----------------------------------------------------------------------------- |
|
310 |
|
311 |
|
312 |
|
313 function addThis() |
|
314 { |
|
315 statusitems[UBound] = status; |
|
316 actualvalues[UBound] = actual; |
|
317 expectedvalues[UBound] = expect; |
|
318 UBound++; |
|
319 } |
|
320 |
|
321 |
|
322 function test() |
|
323 { |
|
324 enterFunc('test'); |
|
325 printBugNumber(BUGNUMBER); |
|
326 printStatus(summary); |
|
327 |
|
328 for (var i=0; i<UBound; i++) |
|
329 { |
|
330 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
|
331 } |
|
332 |
|
333 exitFunc ('test'); |
|
334 } |