Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / RegExp / regress-223273.js
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
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  *   pschwartau@netscape.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:    23 October 2003
42  * SUMMARY: Unescaped, unbalanced parens in a regexp should cause SyntaxError.
43  *
44  * The same would also be true for unescaped, unbalanced brackets or braces
45  * if we followed the ECMA-262 Ed. 3 spec on this. But it was decided for
46  * backward compatibility reasons to follow Perl 5, which permits
47  *
48  * 1. an unescaped, unbalanced right bracket ]
49  * 2. an unescaped, unbalanced left brace    {
50  * 3. an unescaped, unbalanced right brace   }
51  *
52  * If any of these should occur, Perl treats each as a literal
53  * character.  Therefore we permit all three of these cases, even
54  * though not ECMA-compliant.  Note Perl errors on an unescaped,
55  * unbalanced left bracket; so will we.
56  *
57  * See http://bugzilla.mozilla.org/show_bug.cgi?id=223273
58  *
59  */
60 //-----------------------------------------------------------------------------
61 var UBound = 0;
62 var BUGNUMBER = 223273;
63 var summary = 'Unescaped, unbalanced parens in regexp should be a SyntaxError';
64 var TEST_PASSED = 'SyntaxError';
65 var TEST_FAILED = 'Generated an error, but NOT a SyntaxError!';
66 var TEST_FAILED_BADLY = 'Did not generate ANY error!!!';
67 var CHECK_PASSED = 'Should not generate an error';
68 var CHECK_FAILED = 'Generated an error!';
69 var status = '';
70 var statusitems = [];
71 var actual = '';
72 var actualvalues = [];
73 var expect= '';
74 var expectedvalues = [];
75
76
77 /*
78  * All the following contain unescaped, unbalanced parens and
79  * should generate SyntaxErrors. That's what we're testing for.
80  *
81  * To allow the test to compile and run, we have to hide the errors
82  * inside eval strings, and check they are caught at run-time.
83  *
84  * Inside such strings, remember to escape any escape character!
85  */
86 status = inSection(1);
87 testThis(' /(/ ');
88
89 status = inSection(2);
90 testThis(' /)/ ');
91
92 status = inSection(3);
93 testThis(' /(abc\\)def(g/ ');
94
95 status = inSection(4);
96 testThis(' /\\(abc)def)g/ ');
97
98
99 /*
100  * These regexp patterns are correct and should not generate
101  * any errors. Note we use checkThis() instead of testThis().
102  */
103 status = inSection(5);
104 checkThis(' /\\(/ ');
105
106 status = inSection(6);
107 checkThis(' /\\)/ ');
108
109 status = inSection(7);
110 checkThis(' /(abc)def\\(g/ ');
111
112 status = inSection(8);
113 checkThis(' /(abc\\)def)g/ ');
114
115 status = inSection(9);
116 checkThis(' /(abc(\\))def)g/ ');
117
118 status = inSection(10);
119 checkThis(' /(abc([x\\)yz]+)def)g/ ');
120
121
122
123 /*
124  * Unescaped, unbalanced left brackets should be a SyntaxError
125  */
126 status = inSection(11);
127 testThis(' /[/ ');
128
129 status = inSection(12);
130 testThis(' /[abc\\]def[g/ ');
131
132
133 /*
134  * We permit unescaped, unbalanced right brackets, as does Perl.
135  * No error should result, even though this is not ECMA-compliant.
136  * Note we use checkThis() instead of testThis().
137  */
138 status = inSection(13);
139 checkThis(' /]/ ');
140
141 status = inSection(14);
142 checkThis(' /\\[abc]def]g/ ');
143
144
145 /*
146  * These regexp patterns are correct and should not generate
147  * any errors. Note we use checkThis() instead of testThis().
148  */
149 status = inSection(15);
150 checkThis(' /\\[/ ');
151
152 status = inSection(16);
153 checkThis(' /\\]/ ');
154
155 status = inSection(17);
156 checkThis(' /[abc]def\\[g/ ');
157
158 status = inSection(18);
159 checkThis(' /[abc\\]def]g/ ');
160
161 status = inSection(19);
162 checkThis(' /(abc[\\]]def)g/ ');
163
164 status = inSection(20);
165 checkThis(' /[abc(x\\]yz+)def]g/ ');
166
167
168
169 /*
170  * Run some tests for unbalanced braces. We again follow Perl, and
171  * thus permit unescaped unbalanced braces - both left and right,
172  * even though this is not ECMA-compliant.
173  *
174  * Note we use checkThis() instead of testThis().
175  */
176 status = inSection(21);
177 checkThis(' /abc{def/ ');
178
179 status = inSection(22);
180 checkThis(' /abc}def/ ');
181
182 status = inSection(23);
183 checkThis(' /a{2}bc{def/ ');
184
185 status = inSection(24);
186 checkThis(' /a}b{3}c}def/ ');
187
188
189 /*
190  * These regexp patterns are correct and should not generate
191  * any errors. Note we use checkThis() instead of testThis().
192  */
193 status = inSection(25);
194 checkThis(' /abc\\{def/ ');
195
196 status = inSection(26);
197 checkThis(' /abc\\}def/ ');
198
199 status = inSection(27);
200 checkThis(' /a{2}bc\\{def/ ');
201
202 status = inSection(28);
203 checkThis(' /a\\}b{3}c\\}def/ ');
204
205
206
207
208 //-----------------------------------------------------------------------------
209 test();
210 //-----------------------------------------------------------------------------
211
212
213
214
215 /*
216  * Invalid syntax should generate a SyntaxError
217  */
218 function testThis(sInvalidSyntax)
219 {
220   expect = TEST_PASSED;
221   actual = TEST_FAILED_BADLY;
222
223   try
224   {
225     eval(sInvalidSyntax);
226   }
227   catch(e)
228   {
229     if (e instanceof SyntaxError)
230       actual = TEST_PASSED;
231     else
232       actual = TEST_FAILED;
233   }
234
235   statusitems[UBound] = status;
236   expectedvalues[UBound] = expect;
237   actualvalues[UBound] = actual;
238   UBound++;
239 }
240
241
242 /*
243  * Valid syntax shouldn't generate any errors
244  */
245 function checkThis(sValidSyntax)
246 {
247   expect = CHECK_PASSED;
248   actual = CHECK_PASSED;
249
250   try
251   {
252     eval(sValidSyntax);
253   }
254   catch(e)
255   {
256     actual = CHECK_FAILED;
257   }
258
259   statusitems[UBound] = status;
260   expectedvalues[UBound] = expect;
261   actualvalues[UBound] = actual;
262   UBound++;
263 }
264
265
266 function test()
267 {
268   enterFunc('test');
269   printBugNumber(BUGNUMBER);
270   printStatus(summary);
271
272   for (var i=0; i<UBound; i++)
273   {
274     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
275   }
276
277   exitFunc ('test');
278 }