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
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/
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
15 * The Original Code is JavaScript Engine testing utilities.
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.
23 * pschwartau@netscape.com
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.
37 * ***** END LICENSE BLOCK ***** */
41 * Date: 23 October 2003
42 * SUMMARY: Unescaped, unbalanced parens in a regexp should cause SyntaxError.
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
48 * 1. an unescaped, unbalanced right bracket ]
49 * 2. an unescaped, unbalanced left brace {
50 * 3. an unescaped, unbalanced right brace }
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.
57 * See http://bugzilla.mozilla.org/show_bug.cgi?id=223273
60 //-----------------------------------------------------------------------------
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!';
72 var actualvalues = [];
74 var expectedvalues = [];
78 * All the following contain unescaped, unbalanced parens and
79 * should generate SyntaxErrors. That's what we're testing for.
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.
84 * Inside such strings, remember to escape any escape character!
86 status = inSection(1);
89 status = inSection(2);
92 status = inSection(3);
93 testThis(' /(abc\\)def(g/ ');
95 status = inSection(4);
96 testThis(' /\\(abc)def)g/ ');
100 * These regexp patterns are correct and should not generate
101 * any errors. Note we use checkThis() instead of testThis().
103 status = inSection(5);
104 checkThis(' /\\(/ ');
106 status = inSection(6);
107 checkThis(' /\\)/ ');
109 status = inSection(7);
110 checkThis(' /(abc)def\\(g/ ');
112 status = inSection(8);
113 checkThis(' /(abc\\)def)g/ ');
115 status = inSection(9);
116 checkThis(' /(abc(\\))def)g/ ');
118 status = inSection(10);
119 checkThis(' /(abc([x\\)yz]+)def)g/ ');
124 * Unescaped, unbalanced left brackets should be a SyntaxError
126 status = inSection(11);
129 status = inSection(12);
130 testThis(' /[abc\\]def[g/ ');
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().
138 status = inSection(13);
141 status = inSection(14);
142 checkThis(' /\\[abc]def]g/ ');
146 * These regexp patterns are correct and should not generate
147 * any errors. Note we use checkThis() instead of testThis().
149 status = inSection(15);
150 checkThis(' /\\[/ ');
152 status = inSection(16);
153 checkThis(' /\\]/ ');
155 status = inSection(17);
156 checkThis(' /[abc]def\\[g/ ');
158 status = inSection(18);
159 checkThis(' /[abc\\]def]g/ ');
161 status = inSection(19);
162 checkThis(' /(abc[\\]]def)g/ ');
164 status = inSection(20);
165 checkThis(' /[abc(x\\]yz+)def]g/ ');
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.
174 * Note we use checkThis() instead of testThis().
176 status = inSection(21);
177 checkThis(' /abc{def/ ');
179 status = inSection(22);
180 checkThis(' /abc}def/ ');
182 status = inSection(23);
183 checkThis(' /a{2}bc{def/ ');
185 status = inSection(24);
186 checkThis(' /a}b{3}c}def/ ');
190 * These regexp patterns are correct and should not generate
191 * any errors. Note we use checkThis() instead of testThis().
193 status = inSection(25);
194 checkThis(' /abc\\{def/ ');
196 status = inSection(26);
197 checkThis(' /abc\\}def/ ');
199 status = inSection(27);
200 checkThis(' /a{2}bc\\{def/ ');
202 status = inSection(28);
203 checkThis(' /a\\}b{3}c\\}def/ ');
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
216 * Invalid syntax should generate a SyntaxError
218 function testThis(sInvalidSyntax)
220 expect = TEST_PASSED;
221 actual = TEST_FAILED_BADLY;
225 eval(sInvalidSyntax);
229 if (e instanceof SyntaxError)
230 actual = TEST_PASSED;
232 actual = TEST_FAILED;
235 statusitems[UBound] = status;
236 expectedvalues[UBound] = expect;
237 actualvalues[UBound] = actual;
243 * Valid syntax shouldn't generate any errors
245 function checkThis(sValidSyntax)
247 expect = CHECK_PASSED;
248 actual = CHECK_PASSED;
256 actual = CHECK_FAILED;
259 statusitems[UBound] = status;
260 expectedvalues[UBound] = expect;
261 actualvalues[UBound] = actual;
269 printBugNumber(BUGNUMBER);
270 printStatus(summary);
272 for (var i=0; i<UBound; i++)
274 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);