]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_5/Regress/regress-179524.js
31a7f3050d89e9fd98ddd09ed094b5f60ea1a63b
[apple/javascriptcore.git] / tests / mozilla / js1_5 / Regress / regress-179524.js
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Netscape Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/NPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is JavaScript Engine testing utilities.
15 *
16 * The Initial Developer of the Original Code is Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 2002
18 * the Initial Developer. All Rights Reserved.
19 *
20 * Contributor(s): pschwartau@netscape.com
21 *
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the NPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the NPL, the GPL or the LGPL.
33 *
34 * ***** END LICENSE BLOCK *****
35 *
36 *
37 * Date: 11 Nov 2002
38 * SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc.
39 * See http://bugzilla.mozilla.org/show_bug.cgi?id=179524
40 *
41 * Note that when testing str.replace(), we have to be careful if the first
42 * argument provided to str.replace() is not a regexp object. ECMA-262 says
43 * it is NOT converted to one, unlike the case for str.match(), str.search().
44 *
45 * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21. This means
46 * we have to be careful how we test meta-characters in the first argument
47 * to str.replace(), if that argument is a string -
48 */
49 //-----------------------------------------------------------------------------
50 var UBound = 0;
51 var bug = 179524;
52 var summary = "Don't crash on extraneous arguments to str.match(), etc.";
53 var status = '';
54 var statusitems = [];
55 var actual = '';
56 var actualvalues = [];
57 var expect= '';
58 var expectedvalues = [];
59
60
61 str = 'ABC abc';
62 var re = /z/ig;
63
64 status = inSection(1);
65 actual = str.match(re);
66 expect = null;
67 addThis();
68
69 status = inSection(2);
70 actual = str.match(re, 'i');
71 expect = null;
72 addThis();
73
74 status = inSection(3);
75 actual = str.match(re, 'g', '');
76 expect = null;
77 addThis();
78
79 status = inSection(4);
80 actual = str.match(re, 'z', new Object(), new Date());
81 expect = null;
82 addThis();
83
84
85 /*
86 * Now try the same thing with str.search()
87 */
88 status = inSection(5);
89 actual = str.search(re);
90 expect = -1;
91 addThis();
92
93 status = inSection(6);
94 actual = str.search(re, 'i');
95 expect = -1;
96 addThis();
97
98 status = inSection(7);
99 actual = str.search(re, 'g', '');
100 expect = -1;
101 addThis();
102
103 status = inSection(8);
104 actual = str.search(re, 'z', new Object(), new Date());
105 expect = -1;
106 addThis();
107
108
109 /*
110 * Now try the same thing with str.replace()
111 */
112 status = inSection(9);
113 actual = str.replace(re, 'Z');
114 expect = str;
115 addThis();
116
117 status = inSection(10);
118 actual = str.replace(re, 'Z', 'i');
119 expect = str;
120 addThis();
121
122 status = inSection(11);
123 actual = str.replace(re, 'Z', 'g', '');
124 expect = str;
125 addThis();
126
127 status = inSection(12);
128 actual = str.replace(re, 'Z', 'z', new Object(), new Date());
129 expect = str;
130 addThis();
131
132
133
134 /*
135 * Now test the case where str.match()'s first argument is not a regexp object.
136 * In that case, JS follows ECMA-262 Ed.3 by converting the 1st argument to a
137 * regexp object using the argument as a regexp pattern, but then extends ECMA
138 * by taking any optional 2nd argument to be a regexp flag string (e.g.'ig').
139 *
140 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10
141 */
142 status = inSection(13);
143 actual = str.match('a').toString();
144 expect = str.match(/a/).toString();
145 addThis();
146
147 status = inSection(14);
148 actual = str.match('a', 'i').toString();
149 expect = str.match(/a/i).toString();
150 addThis();
151
152 status = inSection(15);
153 actual = str.match('a', 'ig').toString();
154 expect = str.match(/a/ig).toString();
155 addThis();
156
157 status = inSection(16);
158 actual = str.match('\\s', 'm').toString();
159 expect = str.match(/\s/m).toString();
160 addThis();
161
162
163 /*
164 * Now try the previous three cases with extraneous parameters
165 */
166 status = inSection(17);
167 actual = str.match('a', 'i', 'g').toString();
168 expect = str.match(/a/i).toString();
169 addThis();
170
171 status = inSection(18);
172 actual = str.match('a', 'ig', new Object()).toString();
173 expect = str.match(/a/ig).toString();
174 addThis();
175
176 status = inSection(19);
177 actual = str.match('\\s', 'm', 999).toString();
178 expect = str.match(/\s/m).toString();
179 addThis();
180
181
182 /*
183 * Try an invalid second parameter (i.e. an invalid regexp flag)
184 */
185 status = inSection(20);
186 try
187 {
188 actual = str.match('a', 'z').toString();
189 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
190 addThis();
191 }
192 catch (e)
193 {
194 actual = e instanceof SyntaxError;
195 expect = true;
196 addThis();
197 }
198
199
200
201 /*
202 * Now test str.search() where the first argument is not a regexp object.
203 * The same considerations as above apply -
204 *
205 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
206 */
207 status = inSection(21);
208 actual = str.search('a');
209 expect = str.search(/a/);
210 addThis();
211
212 status = inSection(22);
213 actual = str.search('a', 'i');
214 expect = str.search(/a/i);
215 addThis();
216
217 status = inSection(23);
218 actual = str.search('a', 'ig');
219 expect = str.search(/a/ig);
220 addThis();
221
222 status = inSection(24);
223 actual = str.search('\\s', 'm');
224 expect = str.search(/\s/m);
225 addThis();
226
227
228 /*
229 * Now try the previous three cases with extraneous parameters
230 */
231 status = inSection(25);
232 actual = str.search('a', 'i', 'g');
233 expect = str.search(/a/i);
234 addThis();
235
236 status = inSection(26);
237 actual = str.search('a', 'ig', new Object());
238 expect = str.search(/a/ig);
239 addThis();
240
241 status = inSection(27);
242 actual = str.search('\\s', 'm', 999);
243 expect = str.search(/\s/m);
244 addThis();
245
246
247 /*
248 * Try an invalid second parameter (i.e. an invalid regexp flag)
249 */
250 status = inSection(28);
251 try
252 {
253 actual = str.search('a', 'z');
254 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
255 addThis();
256 }
257 catch (e)
258 {
259 actual = e instanceof SyntaxError;
260 expect = true;
261 addThis();
262 }
263
264
265
266 /*
267 * Now test str.replace() where the first argument is not a regexp object.
268 * The same considerations as above apply, EXCEPT for meta-characters.
269 * See introduction to testcase above. References:
270 *
271 * http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
272 * http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21
273 */
274 status = inSection(29);
275 actual = str.replace('a', 'Z');
276 expect = str.replace(/a/, 'Z');
277 addThis();
278
279 status = inSection(30);
280 actual = str.replace('a', 'Z', 'i');
281 expect = str.replace(/a/i, 'Z');
282 addThis();
283
284 status = inSection(31);
285 actual = str.replace('a', 'Z', 'ig');
286 expect = str.replace(/a/ig, 'Z');
287 addThis();
288
289 status = inSection(32);
290 actual = str.replace('\\s', 'Z', 'm'); //<--- NO!!! No meta-characters 1st arg!
291 actual = str.replace(' ', 'Z', 'm'); //<--- Have to do this instead
292 expect = str.replace(/\s/m, 'Z');
293 addThis();
294
295
296 /*
297 * Now try the previous three cases with extraneous parameters
298 */
299 status = inSection(33);
300 actual = str.replace('a', 'Z', 'i', 'g');
301 expect = str.replace(/a/i, 'Z');
302 addThis();
303
304 status = inSection(34);
305 actual = str.replace('a', 'Z', 'ig', new Object());
306 expect = str.replace(/a/ig, 'Z');
307 addThis();
308
309 status = inSection(35);
310 actual = str.replace('\\s', 'Z', 'm', 999); //<--- NO meta-characters 1st arg!
311 actual = str.replace(' ', 'Z', 'm', 999); //<--- Have to do this instead
312 expect = str.replace(/\s/m, 'Z');
313 addThis();
314
315
316 /*
317 * Try an invalid third parameter (i.e. an invalid regexp flag)
318 */
319 status = inSection(36);
320 try
321 {
322 actual = str.replace('a', 'Z', 'z');
323 expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
324 addThis();
325 }
326 catch (e)
327 {
328 actual = e instanceof SyntaxError;
329 expect = true;
330 addThis();
331 }
332
333
334
335
336 //-----------------------------------------------------------------------------
337 test();
338 //-----------------------------------------------------------------------------
339
340
341
342 function addThis()
343 {
344 statusitems[UBound] = status;
345 actualvalues[UBound] = actual;
346 expectedvalues[UBound] = expect;
347 UBound++;
348 }
349
350
351 function test()
352 {
353 enterFunc('test');
354 printBugNumber(bug);
355 printStatus(summary);
356
357 for (var i=0; i<UBound; i++)
358 {
359 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
360 }
361
362 exitFunc ('test');
363 }