1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is JavaScript Engine testing utilities.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corp.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
23 * pschwartau@netscape.com
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 * SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc.
43 * See http://bugzilla.mozilla.org/show_bug.cgi?id=179524
45 * Note that when testing str.replace(), we have to be careful if the first
46 * argument provided to str.replace() is not a regexp object. ECMA-262 says
47 * it is NOT converted to one, unlike the case for str.match(), str.search().
49 * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21. This means
50 * we have to be careful how we test meta-characters in the first argument
51 * to str.replace(), if that argument is a string -
53 //-----------------------------------------------------------------------------
54 var gTestfile
= 'regress-179524.js';
56 var BUGNUMBER
= 179524;
57 var summary
= "Don't crash on extraneous arguments to str.match(), etc.";
61 var actualvalues
= [];
63 var expectedvalues
= [];
69 status
= inSection(1);
70 actual
= str
.match(re
);
74 status
= inSection(2);
75 actual
= str
.match(re
, 'i');
79 status
= inSection(3);
80 actual
= str
.match(re
, 'g', '');
84 status
= inSection(4);
85 actual
= str
.match(re
, 'z', new Object(), new Date());
91 * Now try the same thing with str.search()
93 status
= inSection(5);
94 actual
= str
.search(re
);
98 status
= inSection(6);
99 actual
= str
.search(re
, 'i');
103 status
= inSection(7);
104 actual
= str
.search(re
, 'g', '');
108 status
= inSection(8);
109 actual
= str
.search(re
, 'z', new Object(), new Date());
115 * Now try the same thing with str.replace()
117 status
= inSection(9);
118 actual
= str
.replace(re
, 'Z');
122 status
= inSection(10);
123 actual
= str
.replace(re
, 'Z', 'i');
127 status
= inSection(11);
128 actual
= str
.replace(re
, 'Z', 'g', '');
132 status
= inSection(12);
133 actual
= str
.replace(re
, 'Z', 'z', new Object(), new Date());
140 * Now test the case where str.match()'s first argument is not a regexp object.
141 * In that case, JS follows ECMA-262 Ed.3 by converting the 1st argument to a
142 * regexp object using the argument as a regexp pattern, but then extends ECMA
143 * by taking any optional 2nd argument to be a regexp flag string (e.g.'ig').
145 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10
147 status
= inSection(13);
148 actual
= str
.match('a').toString();
149 expect
= str
.match(/a
/).toString();
152 status
= inSection(14);
153 actual
= str
.match('a', 'i').toString();
154 expect
= str
.match(/a
/i
).toString();
157 status
= inSection(15);
158 actual
= str
.match('a', 'ig').toString();
159 expect
= str
.match(/a
/ig
).toString();
162 status
= inSection(16);
163 actual
= str
.match('\\s', 'm').toString();
164 expect
= str
.match(/\s/m).toString();
169 * Now try the previous three cases with extraneous parameters
171 status
= inSection(17);
172 actual
= str
.match('a', 'i', 'g').toString();
173 expect
= str
.match(/a
/i
).toString();
176 status
= inSection(18);
177 actual
= str
.match('a', 'ig', new Object()).toString();
178 expect
= str
.match(/a
/ig
).toString();
181 status
= inSection(19);
182 actual
= str
.match('\\s', 'm', 999).toString();
183 expect
= str
.match(/\s/m).toString();
188 * Try an invalid second parameter (i.e. an invalid regexp flag)
190 status
= inSection(20);
193 actual
= str
.match('a', 'z').toString();
194 expect
= 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
199 actual
= e
instanceof SyntaxError
;
207 * Now test str.search() where the first argument is not a regexp object.
208 * The same considerations as above apply -
210 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
212 status
= inSection(21);
213 actual
= str
.search('a');
214 expect
= str
.search(/a
/);
217 status
= inSection(22);
218 actual
= str
.search('a', 'i');
219 expect
= str
.search(/a
/i
);
222 status
= inSection(23);
223 actual
= str
.search('a', 'ig');
224 expect
= str
.search(/a
/ig
);
227 status
= inSection(24);
228 actual
= str
.search('\\s', 'm');
229 expect
= str
.search(/\s/m);
234 * Now try the previous three cases with extraneous parameters
236 status
= inSection(25);
237 actual
= str
.search('a', 'i', 'g');
238 expect
= str
.search(/a
/i
);
241 status
= inSection(26);
242 actual
= str
.search('a', 'ig', new Object());
243 expect
= str
.search(/a
/ig
);
246 status
= inSection(27);
247 actual
= str
.search('\\s', 'm', 999);
248 expect
= str
.search(/\s/m);
253 * Try an invalid second parameter (i.e. an invalid regexp flag)
255 status
= inSection(28);
258 actual
= str
.search('a', 'z');
259 expect
= 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
264 actual
= e
instanceof SyntaxError
;
272 * Now test str.replace() where the first argument is not a regexp object.
273 * The same considerations as above apply, EXCEPT for meta-characters.
274 * See introduction to testcase above. References:
276 * http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
277 * http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21
279 status
= inSection(29);
280 actual
= str
.replace('a', 'Z');
281 expect
= str
.replace(/a
/, 'Z');
284 status
= inSection(30);
285 actual
= str
.replace('a', 'Z', 'i');
286 expect
= str
.replace(/a
/i
, 'Z');
289 status
= inSection(31);
290 actual
= str
.replace('a', 'Z', 'ig');
291 expect
= str
.replace(/a
/ig
, 'Z');
294 status
= inSection(32);
295 actual
= str
.replace('\\s', 'Z', 'm'); //<--- NO!!! No meta-characters 1st arg!
296 actual
= str
.replace(' ', 'Z', 'm'); //<--- Have to do this instead
297 expect
= str
.replace(/\s/m, 'Z');
302 * Now try the previous three cases with extraneous parameters
304 status
= inSection(33);
305 actual
= str
.replace('a', 'Z', 'i', 'g');
306 expect
= str
.replace(/a
/i
, 'Z');
309 status
= inSection(34);
310 actual
= str
.replace('a', 'Z', 'ig', new Object());
311 expect
= str
.replace(/a
/ig
, 'Z');
314 status
= inSection(35);
315 actual
= str
.replace('\\s', 'Z', 'm', 999); //<--- NO meta-characters 1st arg!
316 actual
= str
.replace(' ', 'Z', 'm', 999); //<--- Have to do this instead
317 expect
= str
.replace(/\s/m, 'Z');
322 * Try an invalid third parameter (i.e. an invalid regexp flag)
324 status
= inSection(36);
327 actual
= str
.replace('a', 'Z', 'z');
328 expect
= 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
333 actual
= e
instanceof SyntaxError
;
341 //-----------------------------------------------------------------------------
343 //-----------------------------------------------------------------------------
349 statusitems
[UBound
] = status
;
350 actualvalues
[UBound
] = actual
;
351 expectedvalues
[UBound
] = expect
;
359 printBugNumber(BUGNUMBER
);
360 printStatus(summary
);
362 for (var i
=0; i
<UBound
; i
++)
364 reportCompare(expectedvalues
[i
], actualvalues
[i
], statusitems
[i
]);