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/. */
9
* SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc.
10
* See http://bugzilla.mozilla.org/show_bug.cgi?id=179524
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().
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 -
20
//-----------------------------------------------------------------------------
22
var BUGNUMBER = 179524;
23
var summary = "Don't crash on extraneous arguments to str.match(), etc.";
27
var actualvalues = [];
29
var expectedvalues = [];
35
status = inSection(1);
36
actual = str.match(re);
40
status = inSection(2);
41
actual = str.match(re, 'i');
45
status = inSection(3);
46
actual = str.match(re, 'g', '');
50
status = inSection(4);
51
actual = str.match(re, 'z', new Object(), new Date());
57
* Now try the same thing with str.search()
59
status = inSection(5);
60
actual = str.search(re);
64
status = inSection(6);
65
actual = str.search(re, 'i');
69
status = inSection(7);
70
actual = str.search(re, 'g', '');
74
status = inSection(8);
75
actual = str.search(re, 'z', new Object(), new Date());
81
* Now try the same thing with str.replace()
83
status = inSection(9);
84
actual = str.replace(re, 'Z');
88
status = inSection(10);
89
actual = str.replace(re, 'Z', 'i');
93
status = inSection(11);
94
actual = str.replace(re, 'Z', 'g', '');
98
status = inSection(12);
99
actual = str.replace(re, 'Z', 'z', new Object(), new Date());
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').
111
* Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10
113
status = inSection(13);
114
actual = str.match('a').toString();
115
expect = str.match(/a/).toString();
118
status = inSection(14);
119
actual = str.match('a', 'i').toString();
120
expect = str.match(/a/i).toString();
123
status = inSection(15);
124
actual = str.match('a', 'ig').toString();
125
expect = str.match(/a/ig).toString();
128
status = inSection(16);
129
actual = str.match('\\s', 'm').toString();
130
expect = str.match(/\s/m).toString();
135
* Now try the previous three cases with extraneous parameters
137
status = inSection(17);
138
actual = str.match('a', 'i', 'g').toString();
139
expect = str.match(/a/i).toString();
142
status = inSection(18);
143
actual = str.match('a', 'ig', new Object()).toString();
144
expect = str.match(/a/ig).toString();
147
status = inSection(19);
148
actual = str.match('\\s', 'm', 999).toString();
149
expect = str.match(/\s/m).toString();
154
* Try an invalid second parameter (i.e. an invalid regexp flag)
156
status = inSection(20);
159
actual = str.match('a', 'z').toString();
160
expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
165
actual = e instanceof SyntaxError;
173
* Now test str.search() where the first argument is not a regexp object.
174
* The same considerations as above apply -
176
* Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
178
status = inSection(21);
179
actual = str.search('a');
180
expect = str.search(/a/);
183
status = inSection(22);
184
actual = str.search('a', 'i');
185
expect = str.search(/a/i);
188
status = inSection(23);
189
actual = str.search('a', 'ig');
190
expect = str.search(/a/ig);
193
status = inSection(24);
194
actual = str.search('\\s', 'm');
195
expect = str.search(/\s/m);
200
* Now try the previous three cases with extraneous parameters
202
status = inSection(25);
203
actual = str.search('a', 'i', 'g');
204
expect = str.search(/a/i);
207
status = inSection(26);
208
actual = str.search('a', 'ig', new Object());
209
expect = str.search(/a/ig);
212
status = inSection(27);
213
actual = str.search('\\s', 'm', 999);
214
expect = str.search(/\s/m);
219
* Try an invalid second parameter (i.e. an invalid regexp flag)
221
status = inSection(28);
224
actual = str.search('a', 'z');
225
expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
230
actual = e instanceof SyntaxError;
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:
242
* http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
243
* http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21
245
status = inSection(29);
246
actual = str.replace('a', 'Z');
247
expect = str.replace(/a/, 'Z');
250
status = inSection(30);
251
actual = str.replace('a', 'Z', 'i');
252
expect = str.replace(/a/i, 'Z');
255
status = inSection(31);
256
actual = str.replace('a', 'Z', 'ig');
257
expect = str.replace(/a/ig, 'Z');
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');
268
* Now try the previous three cases with extraneous parameters
270
status = inSection(33);
271
actual = str.replace('a', 'Z', 'i', 'g');
272
expect = str.replace(/a/i, 'Z');
275
status = inSection(34);
276
actual = str.replace('a', 'Z', 'ig', new Object());
277
expect = str.replace(/a/ig, 'Z');
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');
288
* Try an invalid third parameter (i.e. an invalid regexp flag)
290
status = inSection(36);
293
actual = str.replace('a', 'Z', 'z');
294
expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!';
299
actual = e instanceof SyntaxError;
307
//-----------------------------------------------------------------------------
309
//-----------------------------------------------------------------------------
315
statusitems[UBound] = status;
316
actualvalues[UBound] = actual;
317
expectedvalues[UBound] = expect;
325
printBugNumber(BUGNUMBER);
326
printStatus(summary);
328
for (var i=0; i<UBound; i++)
330
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);