Debian Codegraph @

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