| 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: 21 January 2003 |
| 9 |
* SUMMARY: Invalid use of regexp quantifiers should generate SyntaxErrors |
| 10 |
* |
| 11 |
* See http://bugzilla.mozilla.org/show_bug.cgi?id=188206 |
| 12 |
* and http://bugzilla.mozilla.org/show_bug.cgi?id=85721#c48 etc. |
| 13 |
* and http://bugzilla.mozilla.org/show_bug.cgi?id=190685 |
| 14 |
* and http://bugzilla.mozilla.org/show_bug.cgi?id=197451 |
| 15 |
*/ |
| 16 |
//----------------------------------------------------------------------------- |
| 17 |
var UBound = 0; |
| 18 |
var BUGNUMBER = 188206; |
| 19 |
var summary = 'Invalid use of regexp quantifiers should generate SyntaxErrors'; |
| 20 |
var TEST_PASSED = 'SyntaxError'; |
| 21 |
var TEST_FAILED = 'Generated an error, but NOT a SyntaxError!'; |
| 22 |
var TEST_FAILED_BADLY = 'Did not generate ANY error!!!'; |
| 23 |
var CHECK_PASSED = 'Should not generate an error'; |
| 24 |
var CHECK_FAILED = 'Generated an error!'; |
| 25 |
var status = ''; |
| 26 |
var statusitems = []; |
| 27 |
var actual = ''; |
| 28 |
var actualvalues = []; |
| 29 |
var expect= ''; |
| 30 |
var expectedvalues = []; |
| 31 |
|
| 32 |
|
| 33 |
/* |
| 34 |
* All the following are invalid uses of regexp quantifiers and |
| 35 |
* should generate SyntaxErrors. That's what we're testing for. |
| 36 |
* |
| 37 |
* To allow the test to compile and run, we have to hide the errors |
| 38 |
* inside eval strings, and check they are caught at run-time - |
| 39 |
* |
| 40 |
*/ |
| 41 |
status = inSection(1); |
| 42 |
testThis(' /a**/ '); |
| 43 |
|
| 44 |
status = inSection(2); |
| 45 |
testThis(' /a***/ '); |
| 46 |
|
| 47 |
status = inSection(3); |
| 48 |
testThis(' /a++/ '); |
| 49 |
|
| 50 |
status = inSection(4); |
| 51 |
testThis(' /a+++/ '); |
| 52 |
|
| 53 |
/* |
| 54 |
* The ? quantifier, unlike * or +, may appear twice in succession. |
| 55 |
* Thus we need at least three in a row to provoke a SyntaxError - |
| 56 |
*/ |
| 57 |
|
| 58 |
status = inSection(5); |
| 59 |
testThis(' /a???/ '); |
| 60 |
|
| 61 |
status = inSection(6); |
| 62 |
testThis(' /a????/ '); |
| 63 |
|
| 64 |
|
| 65 |
/* |
| 66 |
* Now do some weird things on the left side of the regexps - |
| 67 |
*/ |
| 68 |
status = inSection(9); |
| 69 |
testThis(' /+a/ '); |
| 70 |
|
| 71 |
status = inSection(10); |
| 72 |
testThis(' /++a/ '); |
| 73 |
|
| 74 |
status = inSection(11); |
| 75 |
testThis(' /?a/ '); |
| 76 |
|
| 77 |
status = inSection(12); |
| 78 |
testThis(' /??a/ '); |
| 79 |
|
| 80 |
|
| 81 |
/* |
| 82 |
* Misusing the {DecmalDigits} quantifier - according to BOTH ECMA and Perl. |
| 83 |
* |
| 84 |
* Just as with the * and + quantifiers above, can't have two {DecmalDigits} |
| 85 |
* quantifiers in succession - it's a SyntaxError. |
| 86 |
*/ |
| 87 |
status = inSection(28); |
| 88 |
testThis(' /x{1}{1}/ '); |
| 89 |
|
| 90 |
status = inSection(29); |
| 91 |
testThis(' /x{1,}{1}/ '); |
| 92 |
|
| 93 |
status = inSection(30); |
| 94 |
testThis(' /x{1,2}{1}/ '); |
| 95 |
|
| 96 |
status = inSection(31); |
| 97 |
testThis(' /x{1}{1,}/ '); |
| 98 |
|
| 99 |
status = inSection(32); |
| 100 |
testThis(' /x{1,}{1,}/ '); |
| 101 |
|
| 102 |
status = inSection(33); |
| 103 |
testThis(' /x{1,2}{1,}/ '); |
| 104 |
|
| 105 |
status = inSection(34); |
| 106 |
testThis(' /x{1}{1,2}/ '); |
| 107 |
|
| 108 |
status = inSection(35); |
| 109 |
testThis(' /x{1,}{1,2}/ '); |
| 110 |
|
| 111 |
status = inSection(36); |
| 112 |
testThis(' /x{1,2}{1,2}/ '); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
//----------------------------------------------------------------------------- |
| 117 |
test(); |
| 118 |
//----------------------------------------------------------------------------- |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
/* |
| 123 |
* Invalid syntax should generate a SyntaxError |
| 124 |
*/ |
| 125 |
function testThis(sInvalidSyntax) |
| 126 |
{ |
| 127 |
expect = TEST_PASSED; |
| 128 |
actual = TEST_FAILED_BADLY; |
| 129 |
|
| 130 |
try |
| 131 |
{ |
| 132 |
eval(sInvalidSyntax); |
| 133 |
} |
| 134 |
catch(e) |
| 135 |
{ |
| 136 |
if (e instanceof SyntaxError) |
| 137 |
actual = TEST_PASSED; |
| 138 |
else |
| 139 |
actual = TEST_FAILED; |
| 140 |
} |
| 141 |
|
| 142 |
statusitems[UBound] = status; |
| 143 |
expectedvalues[UBound] = expect; |
| 144 |
actualvalues[UBound] = actual; |
| 145 |
UBound++; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
/* |
| 150 |
* Allowed syntax shouldn't generate any errors |
| 151 |
*/ |
| 152 |
function checkThis(sAllowedSyntax) |
| 153 |
{ |
| 154 |
expect = CHECK_PASSED; |
| 155 |
actual = CHECK_PASSED; |
| 156 |
|
| 157 |
try |
| 158 |
{ |
| 159 |
eval(sAllowedSyntax); |
| 160 |
} |
| 161 |
catch(e) |
| 162 |
{ |
| 163 |
actual = CHECK_FAILED; |
| 164 |
} |
| 165 |
|
| 166 |
statusitems[UBound] = status; |
| 167 |
expectedvalues[UBound] = expect; |
| 168 |
actualvalues[UBound] = actual; |
| 169 |
UBound++; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
function test() |
| 174 |
{ |
| 175 |
printBugNumber(BUGNUMBER); |
| 176 |
printStatus(summary); |
| 177 |
|
| 178 |
for (var i=0; i<UBound; i++) |
| 179 |
{ |
| 180 |
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 181 |
} |
| 182 |
} |