Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / extensions / regress-228087.js
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is JavaScript Engine testing utilities.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corp.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *   bex@xaotec.com, PhilSchwartau@aol.com
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 /*
40  *
41  * Date:    12 December 2003
42  * SUMMARY: Testing regexps with unescaped braces
43  * See http://bugzilla.mozilla.org/show_bug.cgi?id=228087
44  *
45  * Note: unbalanced, unescaped braces are not permitted by ECMA-262 Ed.3,
46  * but we decided to follow Perl and IE and allow this for compatibility.
47  *
48  * See http://bugzilla.mozilla.org/show_bug.cgi?id=188206 and its testcase.
49  * See http://bugzilla.mozilla.org/show_bug.cgi?id=223273 and its testcase.
50  */
51 //-----------------------------------------------------------------------------
52 var i = 0;
53 var BUGNUMBER = 228087;
54 var summary = 'Testing regexps with unescaped braces';
55 var status = '';
56 var statusmessages = new Array();
57 var pattern = '';
58 var patterns = new Array();
59 var string = '';
60 var strings = new Array();
61 var actualmatch = '';
62 var actualmatches = new Array();
63 var expectedmatch = '';
64 var expectedmatches = new Array();
65 var e;
66
67
68 string = 'foo {1} foo {2} foo';
69
70 // try an example with the braces escaped
71 status = inSection(1);
72 try
73 {
74   pattern = new RegExp('\{1.*\}', 'g');
75   actualmatch = string.match(pattern);
76 }
77 catch (e)
78 {
79   pattern = 'error';
80   actualmatch = '';
81 }
82 expectedmatch = Array('{1} foo {2}');
83 addThis();
84
85 // just like Section 1, without the braces being escaped
86 status = inSection(2);
87 try
88 {
89   pattern = new RegExp('{1.*}', 'g');
90   actualmatch = string.match(pattern);
91 }
92 catch (e)
93 {
94   pattern = 'error';
95   actualmatch = '';
96 }
97 expectedmatch = Array('{1} foo {2}');
98 addThis();
99
100 // try an example with the braces escaped
101 status = inSection(3);
102 try
103 {
104   pattern = new RegExp('\{1[.!\}]*\}', 'g');
105   actualmatch = string.match(pattern);
106 }
107 catch (e)
108 {
109   pattern = 'error';
110   actualmatch = '';
111 }
112 expectedmatch = Array('{1}');
113 addThis();
114
115 // just like Section 3, without the braces being escaped
116 status = inSection(4);
117 try
118 {
119   pattern = new RegExp('{1[.!}]*}', 'g');
120   actualmatch = string.match(pattern);
121 }
122 catch (e)
123 {
124   pattern = 'error';
125   actualmatch = '';
126 }
127 expectedmatch = Array('{1}');
128 addThis();
129
130
131 string = 'abccccc{3 }c{ 3}c{3, }c{3 ,}c{3 ,4}c{3, 4}c{3,4 }de';
132
133 // use braces in a normal quantifier construct
134 status = inSection(5);
135 try
136 {
137   pattern = new RegExp('c{3}');
138   actualmatch = string.match(pattern);
139 }
140 catch (e)
141 {
142   pattern = 'error';
143   actualmatch = '';
144 }
145 expectedmatch = Array('ccc');
146 addThis();
147
148 // now disrupt the quantifer - the braces should now be interpreted literally
149 status = inSection(6);
150 try
151 {
152   pattern = new RegExp('c{3 }');
153   actualmatch = string.match(pattern);
154 }
155 catch (e)
156 {
157   pattern = 'error';
158   actualmatch = '';
159 }
160 expectedmatch = Array('c{3 }');
161 addThis();
162
163 status = inSection(7);
164 try
165 {
166   pattern = new RegExp('c{3.}');
167   actualmatch = string.match(pattern);
168 }
169 catch (e)
170 {
171   pattern = 'error';
172   actualmatch = '';
173 }
174 expectedmatch = Array('c{3 }');
175 addThis();
176
177 status = inSection(8);
178 try
179 {
180   // need to escape the \ in \s since
181   // this has been converted to a constructor call
182   // instead of a literal regexp
183   pattern = new RegExp('c{3\\s}');
184   actualmatch = string.match(pattern);
185 }
186 catch (e)
187 {
188   pattern = 'error';
189   actualmatch = '';
190 }
191 expectedmatch = Array('c{3 }');
192 addThis();
193
194 status = inSection(9);
195 try
196 {
197   pattern = new RegExp('c{3[ ]}');
198   actualmatch = string.match(pattern);
199 }
200 catch (e)
201 {
202   pattern = 'error';
203   actualmatch = '';
204 }
205 expectedmatch = Array('c{3 }');
206 addThis();
207
208 status = inSection(10);
209 try
210 {
211   pattern = new RegExp('c{ 3}');
212   actualmatch = string.match(pattern);
213 }
214 catch (e)
215 {
216   pattern = 'error';
217   actualmatch = '';
218 }
219 expectedmatch = Array('c{ 3}');
220 addThis();
221
222 // using braces in a normal quantifier construct again
223 status = inSection(11);
224 try
225 {
226   pattern = new RegExp('c{3,}');
227   actualmatch = string.match(pattern);
228 }
229 catch (e)
230 {
231   pattern = 'error';
232   actualmatch = '';
233 }
234 expectedmatch = Array('ccccc');
235 addThis();
236
237 // now disrupt it - the braces should now be interpreted literally
238 status = inSection(12);
239 try
240 {
241   pattern = new RegExp('c{3, }');
242   actualmatch = string.match(pattern);
243 }
244 catch (e)
245 {
246   pattern = 'error';
247   actualmatch = '';
248 }
249 expectedmatch = Array('c{3, }');
250 addThis();
251
252 status = inSection(13);
253 try
254 {
255   pattern = new RegExp('c{3 ,}');
256   actualmatch = string.match(pattern);
257 }
258 catch (e)
259 {
260   pattern = 'error';
261   actualmatch = '';
262 }
263 expectedmatch = Array('c{3 ,}');
264 addThis();
265
266 // using braces in a normal quantifier construct again
267 status = inSection(14);
268 try
269 {
270   pattern = new RegExp('c{3,4}');
271   actualmatch = string.match(pattern);
272 }
273 catch (e)
274 {
275   pattern = 'error';
276   actualmatch = '';
277 }
278 expectedmatch = Array('cccc');
279 addThis();
280
281 // now disrupt it - the braces should now be interpreted literally
282 status = inSection(15);
283 try
284 {
285   pattern = new RegExp('c{3 ,4}');
286   actualmatch = string.match(pattern);
287 }
288 catch (e)
289 {
290   pattern = 'error';
291   actualmatch = '';
292 }
293 expectedmatch = Array('c{3 ,4}');
294 addThis();
295
296 status = inSection(16);
297 try
298 {
299   pattern = new RegExp('c{3, 4}');
300   actualmatch = string.match(pattern);
301 }
302 catch (e)
303 {
304   pattern = 'error';
305   actualmatch = '';
306 }
307 expectedmatch = Array('c{3, 4}');
308 addThis();
309
310 status = inSection(17);
311 try
312 {
313   pattern = new RegExp('c{3,4 }');
314   actualmatch = string.match(pattern);
315 }
316 catch (e)
317 {
318   pattern = 'error';
319   actualmatch = '';
320 }
321 expectedmatch = Array('c{3,4 }');
322 addThis();
323
324
325
326
327 //-------------------------------------------------------------------------------------------------
328 test();
329 //-------------------------------------------------------------------------------------------------
330
331
332
333 function addThis()
334 {
335   statusmessages[i] = status;
336   patterns[i] = pattern;
337   strings[i] = string;
338   actualmatches[i] = actualmatch;
339   expectedmatches[i] = expectedmatch;
340   i++;
341 }
342
343
344 function test()
345 {
346   enterFunc ('test');
347   printBugNumber(BUGNUMBER);
348   printStatus (summary);
349   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
350   exitFunc ('test');
351 }