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