Debian Codegraph @

rhino/testsrc/tests/js1_5/Regress/regress-179524.js

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