tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

statement-versus-statementlistitem.js (4813B)


      1 // |reftest| skip-if(!xulRuntime.shell) -- needs evaluate, parseModule
      2 /*
      3 * Any copyright is dedicated to the Public Domain.
      4 * http://creativecommons.org/licenses/publicdomain/
      5 */
      6 
      7 //-----------------------------------------------------------------------------
      8 var BUGNUMBER = 1288459;
      9 var summary =
     10  "Properly implement the spec's distinctions between StatementListItem and " +
     11  "Statement grammar productions and their uses";
     12 
     13 print(BUGNUMBER + ": " + summary);
     14 
     15 /**************
     16 * BEGIN TEST *
     17 **************/
     18 
     19 var counter = 0;
     20 
     21 // Test all the different contexts that expect a Statement.
     22 
     23 function contextAcceptsStatement(pat)
     24 {
     25  var goodCode =
     26 `function f${counter++}()
     27 {
     28  ${pat.replace("@@@", "let \n {} \n ;")}
     29 }
     30 `;
     31 
     32  evaluate(goodCode);
     33 
     34  var badCode =
     35 `function f${counter++}()
     36 {
     37  ${pat.replace("@@@", "let {} \n ;")}
     38 }
     39 `;
     40 
     41  try
     42  {
     43    evaluate(badCode);
     44    throw new Error("didn't throw");
     45  }
     46  catch (e)
     47  {
     48    assertEq(e instanceof SyntaxError, true,
     49             "didn't get a syntax error, instead got: " + e);
     50  }
     51 }
     52 
     53 contextAcceptsStatement("if (0) @@@");
     54 contextAcceptsStatement("if (0) ; else @@@");
     55 contextAcceptsStatement("if (0) ; else if (0) @@@");
     56 contextAcceptsStatement("if (0) ; else if (0) ; else @@@");
     57 // Can't test do-while loops this way because the Statement isn't in trailing
     58 // position, so 'let' followed by newline *must* be followed by 'while'.
     59 contextAcceptsStatement("while (1) @@@");
     60 contextAcceptsStatement("for (1; 1; 0) @@@");
     61 contextAcceptsStatement("for (var x; 1; 0) @@@");
     62 contextAcceptsStatement("for (let x; 1; 0) @@@");
     63 contextAcceptsStatement("for (const x = 1; 1; 0) @@@");
     64 contextAcceptsStatement("for (x in 0) @@@");
     65 contextAcceptsStatement("for (var x in 0) @@@");
     66 contextAcceptsStatement("for (let x in 0) @@@");
     67 contextAcceptsStatement("for (const x in 0) @@@");
     68 contextAcceptsStatement("for (x of []) @@@");
     69 contextAcceptsStatement("for (var x of []) @@@");
     70 contextAcceptsStatement("for (let x of []) @@@");
     71 contextAcceptsStatement("for (const x of []) @@@");
     72 contextAcceptsStatement("with (1) @@@");
     73 contextAcceptsStatement("foo: @@@");
     74 
     75 // Test all the different contexts that expect a StatementListItem.
     76 
     77 function contextAcceptsStatementListItem(pat)
     78 {
     79  var goodCode =
     80 `function f${counter++}()
     81 {
     82  ${pat.replace("@@@", "let \n [] = [] ;")}
     83 }
     84 `;
     85 
     86  evaluate(goodCode);
     87 
     88  var moarGoodCode = pat.replace("@@@", "let \n [] = [] ;");
     89  evaluate(moarGoodCode);
     90 
     91  var goodModuleCode = pat.replace("@@@", "let \n [] = [] ;");
     92  parseModule(goodModuleCode);
     93 
     94  var badCode =
     95 `function f${counter++}()
     96 {
     97  ${pat.replace("@@@", "let \n let = 3 ;")}
     98 }
     99 `;
    100 
    101  try
    102  {
    103    evaluate(badCode);
    104    throw new Error("didn't throw");
    105  }
    106  catch (e)
    107  {
    108    assertEq(e instanceof SyntaxError, true,
    109             "didn't get a syntax error, instead got: " + e);
    110  }
    111 }
    112 
    113 contextAcceptsStatementListItem("{ @@@ }");
    114 contextAcceptsStatementListItem("switch (1) { case 1: @@@ }");
    115 contextAcceptsStatementListItem("switch (1) { default: @@@ }");
    116 contextAcceptsStatementListItem("switch (1) { case 0: case 1: @@@ }");
    117 contextAcceptsStatementListItem("switch (1) { case 0: break; case 1: @@@ }");
    118 contextAcceptsStatementListItem("switch (1) { default: @@@ case 2: }");
    119 contextAcceptsStatementListItem("try { @@@ } catch (e) { }");
    120 contextAcceptsStatementListItem("try { @@@ } finally { }");
    121 contextAcceptsStatementListItem("try { @@@ } catch (e) { } finally { }");
    122 contextAcceptsStatementListItem("try { } catch (e) { @@@ }");
    123 contextAcceptsStatementListItem("try { } finally { @@@ }");
    124 contextAcceptsStatementListItem("try { } catch (e) { @@@ } finally { }");
    125 contextAcceptsStatementListItem("try { } catch (e) { } finally { @@@ }");
    126 contextAcceptsStatementListItem("_ => { @@@ }");
    127 contextAcceptsStatementListItem("function q() { @@@ }");
    128 contextAcceptsStatementListItem("function* q() { @@@ }");
    129 contextAcceptsStatementListItem("(function() { @@@ })");
    130 contextAcceptsStatementListItem("(function*() { @@@ })");
    131 contextAcceptsStatementListItem("({ *q() { @@@ } })");
    132 contextAcceptsStatementListItem("({ q() { @@@ } })");
    133 contextAcceptsStatementListItem("({ get q() { @@@ } })");
    134 contextAcceptsStatementListItem("({ set q(v) { @@@ } })");
    135 contextAcceptsStatementListItem("(class { f() { @@@ } })");
    136 contextAcceptsStatementListItem("(class { static f() { @@@ } })");
    137 contextAcceptsStatementListItem("(class { static *f() { @@@ } })");
    138 contextAcceptsStatementListItem("(class { static get f() { @@@ } })");
    139 contextAcceptsStatementListItem("(class { static set f(v) { @@@ } })");
    140 contextAcceptsStatementListItem("(class { static() { @@@ } })");
    141 contextAcceptsStatementListItem("@@@");
    142 
    143 /******************************************************************************/
    144 
    145 if (typeof reportCompare === "function")
    146  reportCompare(true, true);
    147 
    148 print("Tests complete");