| OLD | NEW |
| (Empty) |
| 1 description( | |
| 2 "This test checks that the following expressions or statements are valid ECMASCR
IPT code or should throw parse error" | |
| 3 ); | |
| 4 | |
| 5 function runTest(_a, errorType) | |
| 6 { | |
| 7 var success; | |
| 8 if (typeof _a != "string") | |
| 9 testFailed("runTest expects string argument: " + _a); | |
| 10 try { | |
| 11 eval(_a); | |
| 12 success = true; | |
| 13 } catch (e) { | |
| 14 success = !(e instanceof SyntaxError); | |
| 15 } | |
| 16 if ((!!errorType) == !success) { | |
| 17 if (errorType) | |
| 18 testPassed('Invalid: "' + _a + '"'); | |
| 19 else | |
| 20 testPassed('Valid: "' + _a + '"'); | |
| 21 } else { | |
| 22 if (errorType) | |
| 23 testFailed('Invalid: "' + _a + '" should throw ' + errorType.name); | |
| 24 else | |
| 25 testFailed('Valid: "' + _a + '" should NOT throw '); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 function valid(_a) | |
| 30 { | |
| 31 // Test both the grammar and the syntax checker | |
| 32 runTest(_a, false); | |
| 33 runTest("function f() { " + _a + " }", false); | |
| 34 } | |
| 35 | |
| 36 function invalid(_a, _type) | |
| 37 { | |
| 38 _type = _type || SyntaxError; | |
| 39 // Test both the grammar and the syntax checker | |
| 40 runTest(_a, true); | |
| 41 runTest("function f() { " + _a + " }", true); | |
| 42 } | |
| 43 | |
| 44 // known issue: | |
| 45 // some statements requires statement as argument, and | |
| 46 // it seems the End-Of-File terminator is converted to semicolon | |
| 47 // "a:[EOF]" is not parse error, while "{ a: }" is parse error | |
| 48 // "if (a)[EOF]" is not parse error, while "{ if (a) }" is parse error | |
| 49 // known issues of bison parser: | |
| 50 // accepts: 'function f() { return 6 + }' (only inside a function declaration) | |
| 51 // some comma expressions: see reparsing-semicolon-insertion.js | |
| 52 | |
| 53 debug ("Unary operators and member access"); | |
| 54 | |
| 55 valid (""); | |
| 56 invalid("(a"); | |
| 57 invalid("a[5"); | |
| 58 invalid("a[5 + 6"); | |
| 59 invalid("a."); | |
| 60 invalid("()"); | |
| 61 invalid("a.'l'"); | |
| 62 valid ("a: +~!new a"); | |
| 63 invalid("new -a"); | |
| 64 valid ("new (-1)") | |
| 65 valid ("a: b: c: new f(x++)++") | |
| 66 valid ("(a)++"); | |
| 67 valid ("(1--).x"); | |
| 68 invalid("a-- ++"); | |
| 69 invalid("(a:) --b"); | |
| 70 valid ("++ -- ++ a"); | |
| 71 valid ("++ new new a ++"); | |
| 72 valid ("delete void 0"); | |
| 73 invalid("delete the void"); | |
| 74 invalid("(a++"); | |
| 75 valid ("++a--"); | |
| 76 valid ("++((a))--"); | |
| 77 valid ("(a.x++)++"); | |
| 78 invalid("1: null"); | |
| 79 invalid("+-!~"); | |
| 80 invalid("+-!~(("); | |
| 81 invalid("a)"); | |
| 82 invalid("a]"); | |
| 83 invalid(".l"); | |
| 84 invalid("1.l"); | |
| 85 valid ("1 .l"); | |
| 86 | |
| 87 debug ("Binary and conditional operators"); | |
| 88 | |
| 89 valid ("a + + typeof this"); | |
| 90 invalid("a + * b"); | |
| 91 invalid("a ? b"); | |
| 92 invalid("a ? b :"); | |
| 93 invalid("%a"); | |
| 94 invalid("a-"); | |
| 95 valid ("a = b ? b = c : d = e"); | |
| 96 valid ("s: a[1].l ? b.l['s'] ? c++ : d : true"); | |
| 97 valid ("a ? b + 1 ? c + 3 * d.l : d[5][6] : e"); | |
| 98 valid ("a in b instanceof delete -c"); | |
| 99 invalid("a in instanceof b.l"); | |
| 100 valid ("- - true % 5"); | |
| 101 invalid("- false = 3"); | |
| 102 valid ("a: b: c: (1 + null) = 3"); | |
| 103 valid ("a[2] = b.l += c /= 4 * 7 ^ !6"); | |
| 104 invalid("a + typeof b += c in d"); | |
| 105 invalid("typeof a &= typeof b"); | |
| 106 valid ("a: ((typeof (a))) >>>= a || b.l && c"); | |
| 107 valid ("a: b: c[a /= f[a %= b]].l[c[x] = 7] -= a ? b <<= f : g"); | |
| 108 valid ("-void+x['y'].l == x.l != 5 - f[7]"); | |
| 109 | |
| 110 debug ("Function calls (and new with arguments)"); | |
| 111 | |
| 112 valid ("a()()()"); | |
| 113 valid ("s: l: a[2](4 == 6, 5 = 6)(f[4], 6)"); | |
| 114 valid ("s: eval(a.apply(), b.call(c[5] - f[7]))"); | |
| 115 invalid("a("); | |
| 116 invalid("a(5"); | |
| 117 invalid("a(5,"); | |
| 118 invalid("a(5,)"); | |
| 119 invalid("a(5,6"); | |
| 120 valid ("a(b[7], c <d> e.l, new a() > b)"); | |
| 121 invalid("a(b[5)"); | |
| 122 invalid("a(b.)"); | |
| 123 valid ("~new new a(1)(i++)(c[l])"); | |
| 124 invalid("a(*a)"); | |
| 125 valid ("((((a))((b)()).l))()"); | |
| 126 valid ("(a)[b + (c) / (d())].l--"); | |
| 127 valid ("new (5)"); | |
| 128 invalid("new a(5"); | |
| 129 valid ("new (f + 5)(6, (g)() - 'l'() - true(false))"); | |
| 130 invalid("a(.length)"); | |
| 131 | |
| 132 debug ("function declaration and expression"); | |
| 133 | |
| 134 valid ("function f() {}"); | |
| 135 valid ("function f(a,b) {}"); | |
| 136 invalid("function () {}"); | |
| 137 invalid("function f(a b) {}"); | |
| 138 invalid("function f(a,) {}"); | |
| 139 invalid("function f(a,"); | |
| 140 invalid("function f(a, 1) {}"); | |
| 141 valid ("function g(arguments, eval) {}"); | |
| 142 valid ("function f() {} + function g() {}"); | |
| 143 invalid("(function a{})"); | |
| 144 invalid("(function this(){})"); | |
| 145 valid ("(delete new function f(){} + function(a,b){}(5)(6))"); | |
| 146 valid ("6 - function (m) { function g() {} }"); | |
| 147 invalid("function l() {"); | |
| 148 invalid("function l++(){}"); | |
| 149 | |
| 150 debug ("Array and object literal, comma operator"); | |
| 151 | |
| 152 // Note these are tested elsewhere, no need to repeat those tests here | |
| 153 valid ("[] in [5,6] * [,5,] / [,,5,,] || [a,] && new [,b] % [,,]"); | |
| 154 invalid("[5,"); | |
| 155 invalid("[,"); | |
| 156 invalid("(a,)"); | |
| 157 valid ("1 + {get get(){}, set set(a){}, get1:4, set1:get-set, }"); | |
| 158 invalid("1 + {a"); | |
| 159 invalid("1 + {a:"); | |
| 160 invalid("1 + {get l("); | |
| 161 invalid(",a"); | |
| 162 valid ("(4,(5,a(3,4))),f[4,a-6]"); | |
| 163 invalid("(,f)"); | |
| 164 invalid("a,,b"); | |
| 165 invalid("a ? b, c : d"); | |
| 166 | |
| 167 debug ("simple statements"); | |
| 168 | |
| 169 valid ("{ }"); | |
| 170 invalid("{ { }"); | |
| 171 valid ("{ ; ; ; }"); | |
| 172 valid ("a: { ; }"); | |
| 173 invalid("{ a: }"); | |
| 174 valid ("{} f; { 6 + f() }"); | |
| 175 valid ("{ a[5],6; {} ++b-new (-5)() } c().l++"); | |
| 176 valid ("{ l1: l2: l3: { this } a = 32 ; { i++ ; { { { } } ++i } } }"); | |
| 177 valid ("if (a) ;"); | |
| 178 invalid("{ if (a) }"); | |
| 179 invalid("if a {}"); | |
| 180 invalid("if (a"); | |
| 181 invalid("if (a { }"); | |
| 182 valid ("x: s: if (a) ; else b"); | |
| 183 invalid("else {}"); | |
| 184 valid ("if (a) if (b) y; else {} else ;"); | |
| 185 invalid("if (a) {} else x; else"); | |
| 186 invalid("if (a) { else }"); | |
| 187 valid ("if (a.l + new b()) 4 + 5 - f()"); | |
| 188 valid ("if (a) with (x) ; else with (y) ;"); | |
| 189 invalid("with a.b { }"); | |
| 190 valid ("while (a() - new b) ;"); | |
| 191 invalid("while a {}"); | |
| 192 valid ("do ; while(0) i++"); // Is this REALLY valid? (Firefox also accepts thi
s) | |
| 193 valid ("do if (a) x; else y; while(z)"); | |
| 194 invalid("do g; while 4"); | |
| 195 invalid("do g; while ((4)"); | |
| 196 valid ("{ { do do do ; while(0) while(0) while(0) } }"); | |
| 197 valid ("do while (0) if (a) {} else y; while(0)"); | |
| 198 valid ("if (a) while (b) if (c) with(d) {} else e; else f"); | |
| 199 invalid("break ; break your_limits ; continue ; continue living ; debugger"); | |
| 200 invalid("debugger X"); | |
| 201 invalid("break 0.2"); | |
| 202 invalid("continue a++"); | |
| 203 invalid("continue (my_friend)"); | |
| 204 valid ("while (1) break"); | |
| 205 valid ("do if (a) with (b) continue; else debugger; while (false)"); | |
| 206 invalid("do if (a) while (false) else debugger"); | |
| 207 invalid("while if (a) ;"); | |
| 208 valid ("if (a) function f() {} else function g() {}"); | |
| 209 valid ("if (a()) while(0) function f() {} else function g() {}"); | |
| 210 invalid("if (a()) function f() { else function g() }"); | |
| 211 invalid("if (a) if (b) ; else function f {}"); | |
| 212 invalid("if (a) if (b) ; else function (){}"); | |
| 213 valid ("throw a"); | |
| 214 valid ("throw a + b in void c"); | |
| 215 invalid("throw"); | |
| 216 | |
| 217 debug ("var and const statements"); | |
| 218 | |
| 219 valid ("var a, b = null"); | |
| 220 valid ("const a = 5, b, c"); | |
| 221 invalid("var"); | |
| 222 invalid("var = 7"); | |
| 223 invalid("var c (6)"); | |
| 224 valid ("if (a) var a,b; else const b, c"); | |
| 225 invalid("var 5 = 6"); | |
| 226 valid ("while (0) var a, b, c=6, d, e, f=5*6, g=f*h, h"); | |
| 227 invalid("var a = if (b) { c }"); | |
| 228 invalid("var a = var b"); | |
| 229 valid ("const a = b += c, a, a, a = (b - f())"); | |
| 230 invalid("var a %= b | 5"); | |
| 231 invalid("var (a) = 5"); | |
| 232 invalid("var a = (4, b = 6"); | |
| 233 invalid("const 'l' = 3"); | |
| 234 invalid("var var = 3"); | |
| 235 valid ("var varr = 3 in 1"); | |
| 236 valid ("const a, a, a = void 7 - typeof 8, a = 8"); | |
| 237 valid ("const x_x = 6 /= 7 ? e : f"); | |
| 238 invalid("var a = ?"); | |
| 239 invalid("const a = *7"); | |
| 240 invalid("var a = :)"); | |
| 241 valid ("var a = a in b in c instanceof d"); | |
| 242 invalid("var a = b ? c, b"); | |
| 243 invalid("const a = b : c"); | |
| 244 | |
| 245 debug ("for statement"); | |
| 246 | |
| 247 valid ("for ( ; ; ) { break }"); | |
| 248 valid ("for ( a ; ; ) { break }"); | |
| 249 valid ("for ( ; a ; ) { break }"); | |
| 250 valid ("for ( ; ; a ) { break }"); | |
| 251 valid ("for ( a ; a ; ) break"); | |
| 252 valid ("for ( a ; ; a ) break"); | |
| 253 valid ("for ( ; a ; a ) break"); | |
| 254 invalid("for () { }"); | |
| 255 invalid("for ( a ) { }"); | |
| 256 invalid("for ( ; ) ;"); | |
| 257 invalid("for a ; b ; c { }"); | |
| 258 invalid("for (a ; { }"); | |
| 259 invalid("for ( a ; ) ;"); | |
| 260 invalid("for ( ; a ) break"); | |
| 261 valid ("for (var a, b ; ; ) { break } "); | |
| 262 valid ("for (var a = b, b = a ; ; ) break"); | |
| 263 valid ("for (var a = b, c, d, b = a ; x in b ; ) { break }"); | |
| 264 valid ("for (var a = b, c, d ; ; 1 in a()) break"); | |
| 265 invalid("for ( ; var a ; ) break"); | |
| 266 invalid("for (const a; ; ) break"); | |
| 267 invalid("for ( %a ; ; ) { }"); | |
| 268 valid ("for (a in b) break"); | |
| 269 valid ("for (a() in b) break"); | |
| 270 valid ("for (a().l[4] in b) break"); | |
| 271 valid ("for (new a in b in c in d) break"); | |
| 272 valid ("for (new new new a in b) break"); | |
| 273 invalid("for (delete new a() in b) break"); | |
| 274 invalid("for (a * a in b) break"); | |
| 275 valid ("for ((a * a) in b) break"); | |
| 276 invalid("for (a++ in b) break"); | |
| 277 valid ("for ((a++) in b) break"); | |
| 278 invalid("for (++a in b) break"); | |
| 279 valid ("for ((++a) in b) break"); | |
| 280 invalid("for (a, b in c) break"); | |
| 281 invalid("for (a,b in c ;;) break"); | |
| 282 valid ("for (a,(b in c) ;;) break"); | |
| 283 valid ("for ((a, b) in c) break"); | |
| 284 invalid("for (a ? b : c in c) break"); | |
| 285 valid ("for ((a ? b : c) in c) break"); | |
| 286 valid ("for (var a in b in c) break"); | |
| 287 valid ("for (var a = 5 += 6 in b) break"); | |
| 288 invalid("for (var a += 5 in b) break"); | |
| 289 invalid("for (var a = in b) break"); | |
| 290 invalid("for (var a, b in b) break"); | |
| 291 invalid("for (var a = -6, b in b) break"); | |
| 292 invalid("for (var a, b = 8 in b) break"); | |
| 293 valid ("for (var a = (b in c) in d) break"); | |
| 294 invalid("for (var a = (b in c in d) break"); | |
| 295 invalid("for (var (a) in b) { }"); | |
| 296 valid ("for (var a = 7, b = c < d >= d ; f()[6]++ ; --i()[1]++ ) {}"); | |
| 297 | |
| 298 debug ("try statement"); | |
| 299 | |
| 300 invalid("try { break } catch(e) {}"); | |
| 301 valid ("try {} finally { c++ }"); | |
| 302 valid ("try { with (x) { } } catch(e) {} finally { if (a) ; }"); | |
| 303 invalid("try {}"); | |
| 304 invalid("catch(e) {}"); | |
| 305 invalid("finally {}"); | |
| 306 invalid("try a; catch(e) {}"); | |
| 307 invalid("try {} catch(e) a()"); | |
| 308 invalid("try {} finally a()"); | |
| 309 invalid("try {} catch(e)"); | |
| 310 invalid("try {} finally"); | |
| 311 invalid("try {} finally {} catch(e) {}"); | |
| 312 invalid("try {} catch (...) {}"); | |
| 313 invalid("try {} catch {}"); | |
| 314 valid ("if (a) try {} finally {} else b;"); | |
| 315 valid ("if (--a()) do with(1) try {} catch(ke) { f() ; g() } while (a in b) els
e {}"); | |
| 316 invalid("if (a) try {} else b; catch (e) { }"); | |
| 317 invalid("try { finally {}"); | |
| 318 | |
| 319 debug ("switch statement"); | |
| 320 | |
| 321 valid ("switch (a) {}"); | |
| 322 invalid("switch () {}"); | |
| 323 invalid("case 5:"); | |
| 324 invalid("default:"); | |
| 325 invalid("switch (a) b;"); | |
| 326 invalid("switch (a) case 3: b;"); | |
| 327 valid ("switch (f()) { case 5 * f(): default: case '6' - 9: ++i }"); | |
| 328 invalid("switch (true) { default: case 6: default: }"); | |
| 329 invalid("switch (l) { f(); }"); | |
| 330 invalid("switch (l) { case 1: ; a: case 5: }"); | |
| 331 valid ("switch (g() - h[5].l) { case 1 + 6: a: b: c: ++f }"); | |
| 332 invalid("switch (g) { case 1: a: }"); | |
| 333 invalid("switch (g) { case 1: a: default: }"); | |
| 334 invalid("switch g { case 1: l() }"); | |
| 335 invalid("switch (g) { case 1:"); | |
| 336 valid ("switch (l) { case a = b ? c : d : }"); | |
| 337 valid ("switch (sw) { case a ? b - 7[1] ? [c,,] : d = 6 : { } : }"); | |
| 338 invalid("switch (l) { case b ? c : }"); | |
| 339 valid ("switch (l) { case 1: a: with(g) switch (g) { case 2: default: } default
: }"); | |
| 340 invalid("switch (4 - ) { }"); | |
| 341 invalid("switch (l) { default case: 5; }"); | |
| 342 | |
| 343 invalid("L: L: ;"); | |
| 344 invalid("L: L1: L: ;"); | |
| 345 invalid("L: L1: L2: L3: L4: L: ;"); | |
| 346 | |
| 347 invalid("for(var a,b 'this shouldn\'t be allowed' false ; ) ;"); | |
| 348 invalid("for(var a,b '"); | |
| 349 | |
| 350 valid("function __proto__(){}") | |
| 351 valid("(function __proto__(){})") | |
| 352 valid("'use strict'; function __proto__(){}") | |
| 353 valid("'use strict'; (function __proto__(){})") | |
| 354 | |
| 355 valid("if (0) $foo; ") | |
| 356 valid("if (0) _foo; ") | |
| 357 valid("if (0) foo$; ") | |
| 358 valid("if (0) foo_; ") | |
| 359 valid("if (0) obj.$foo; ") | |
| 360 valid("if (0) obj._foo; ") | |
| 361 valid("if (0) obj.foo$; ") | |
| 362 valid("if (0) obj.foo_; ") | |
| 363 valid("if (0) obj.foo\\u03bb; ") | |
| 364 valid("if (0) new a(b+c).d = 5"); | |
| 365 valid("if (0) new a(b+c) = 5"); | |
| 366 valid("([1 || 1].a = 1)"); | |
| 367 valid("({a: 1 || 1}.a = 1)"); | |
| 368 | |
| 369 invalid("var a.b = c"); | |
| 370 invalid("var a.b;"); | |
| 371 | |
| 372 try { eval("a.b.c = {};"); } catch(e1) { e=e1; shouldBe("e.line", "1") } | |
| 373 foo = 'FAIL'; | |
| 374 bar = 'PASS'; | |
| 375 try { | |
| 376 eval("foo = 'PASS'; a.b.c = {}; bar = 'FAIL';"); | |
| 377 } catch(e) { | |
| 378 shouldBe("foo", "'PASS'"); | |
| 379 shouldBe("bar", "'PASS'"); | |
| 380 } | |
| OLD | NEW |