summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/regress/regress-179524.js
blob: 436bdfc43a49d69c0d2eb7dbbb518d9e2a47115e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// |reftest| skip-if(!xulRuntime.shell)
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 *
 * Date:    11 Nov 2002
 * SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc.
 * See http://bugzilla.mozilla.org/show_bug.cgi?id=179524
 *
 * Note that when testing str.replace(), we have to be careful if the first
 * argument provided to str.replace() is not a regexp object. ECMA-262 says
 * it is NOT converted to one, unlike the case for str.match(), str.search().
 *
 * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21. This means
 * we have to be careful how we test meta-characters in the first argument
 * to str.replace(), if that argument is a string -
 */
//-----------------------------------------------------------------------------
var UBound = 0;
var BUGNUMBER = 179524;
var summary = "Don't crash on extraneous arguments to str.match(), etc.";
var status = '';
var statusitems = [];
var actual = '';
var actualvalues = [];
var expect= '';
var expectedvalues = [];

str = 'ABC abc';
var re = /z/ig;

status = inSection(1);
actual = str.match(re);
expect = null;
addThis();

status = inSection(2);
actual = str.match(re, 'i');
expect = null;
addThis();

status = inSection(3);
actual = str.match(re, 'g', '');
expect = null;
addThis();

status = inSection(4);
actual = str.match(re, 'z', new Object(), new Date());
expect = null;
addThis();


/*
 * Now try the same thing with str.search()
 */
status = inSection(5);
actual = str.search(re);
expect = -1;
addThis();

status = inSection(6);
actual = str.search(re, 'i');
expect = -1;
addThis();

status = inSection(7);
actual = str.search(re, 'g', '');
expect = -1;
addThis();

status = inSection(8);
actual = str.search(re, 'z', new Object(), new Date());
expect = -1;
addThis();


/*
 * Now try the same thing with str.replace()
 */
status = inSection(9);
actual = str.replace(re, 'Z');
expect = str;
addThis();

status = inSection(10);
actual = str.replace(re, 'Z', 'i');
expect = str;
addThis();

status = inSection(11);
actual = str.replace(re, 'Z', 'g', '');
expect = str;
addThis();

status = inSection(12);
actual = str.replace(re, 'Z', 'z', new Object(), new Date());
expect = str;
addThis();



/*
 * Now test the case where str.match()'s first argument is not a regexp object.
 * In that case, JS follows ECMA-262 Ed.3 by converting the 1st argument to a
 * regexp object using the argument as a regexp pattern, but then extends ECMA
 * by taking any optional 2nd argument to be a regexp flag string (e.g.'ig').
 *
 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10
 */
status = inSection(13);
actual = str.match('a').toString();
expect = str.match(/a/).toString();
addThis();

status = inSection(14);
actual = str.match('a', 'i').toString();
expect = str.match(/a/).toString();
addThis();

status = inSection(15);
actual = str.match('a', 'ig').toString();
expect = str.match(/a/).toString();
addThis();

status = inSection(16);
actual = str.match('\\s', 'm').toString();
expect = str.match(/\s/).toString();
addThis();


/*
 * Now try the previous three cases with extraneous parameters
 */
status = inSection(17);
actual = str.match('a', 'i', 'g').toString();
expect = str.match(/a/).toString();
addThis();

status = inSection(18);
actual = str.match('a', 'ig', new Object()).toString();
expect = str.match(/a/).toString();
addThis();

status = inSection(19);
actual = str.match('\\s', 'm', 999).toString();
expect = str.match(/\s/).toString();
addThis();

status = inSection(20);
actual = str.match('a', 'z').toString();
expect = str.match(/a/).toString();
addThis();



/*
 * Now test str.search() where the first argument is not a regexp object.
 * The same considerations as above apply -
 *
 * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
 */
status = inSection(21);
actual = str.search('a');
expect = str.search(/a/);
addThis();

status = inSection(22);
actual = str.search('a', 'i');
expect = str.search(/a/);
addThis();

status = inSection(23);
actual = str.search('a', 'ig');
expect = str.search(/a/);
addThis();

status = inSection(24);
actual = str.search('\\s', 'm');
expect = str.search(/\s/);
addThis();


/*
 * Now try the previous three cases with extraneous parameters
 */
status = inSection(25);
actual = str.search('a', 'i', 'g');
expect = str.search(/a/);
addThis();

status = inSection(26);
actual = str.search('a', 'ig', new Object());
expect = str.search(/a/);
addThis();

status = inSection(27);
actual = str.search('\\s', 'm', 999);
expect = str.search(/\s/);
addThis();

status = inSection(28);
actual = str.search('a', 'z');
expect = str.search(/a/);
addThis();



/*
 * Now test str.replace() where the first argument is not a regexp object.
 * The same considerations as above apply, EXCEPT for meta-characters.
 * See introduction to testcase above. References:
 *
 * http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16
 * http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21
 */
status = inSection(29);
actual = str.replace('a', 'Z');
expect = str.replace(/a/, 'Z');
addThis();

status = inSection(30);
actual = str.replace('a', 'Z', 'i');
expect = str.replace(/a/, 'Z');
addThis();

status = inSection(31);
actual = str.replace('a', 'Z', 'ig');
expect = str.replace(/a/, 'Z');
addThis();

status = inSection(32);
actual = str.replace('\\s', 'Z', 'm'); //<--- NO!!! No meta-characters 1st arg!
actual = str.replace(' ', 'Z', 'm');   //<--- Have to do this instead
expect = str.replace(/\s/, 'Z');
addThis();


/*
 * Now try the previous three cases with extraneous parameters
 */
status = inSection(33);
actual = str.replace('a', 'Z', 'i', 'g');
expect = str.replace(/a/, 'Z');
addThis();

status = inSection(34);
actual = str.replace('a', 'Z', 'ig', new Object());
expect = str.replace(/a/, 'Z');
addThis();

status = inSection(35);
actual = str.replace('\\s', 'Z', 'm', 999); //<--- NO meta-characters 1st arg!
actual = str.replace(' ', 'Z', 'm', 999);   //<--- Have to do this instead
expect = str.replace(/\s/, 'Z');
addThis();

status = inSection(36);
actual = str.replace('a', 'Z', 'z');
expect = str.replace(/a/, 'Z');
addThis();




//-----------------------------------------------------------------------------
test();
//-----------------------------------------------------------------------------



function addThis()
{
  statusitems[UBound] = status;
  actualvalues[UBound] = actual;
  expectedvalues[UBound] = expect;
  UBound++;
}


function test()
{
  printBugNumber(BUGNUMBER);
  printStatus(summary);

  for (var i=0; i<UBound; i++)
  {
    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
  }
}