js/src/tests/ecma_6/Comprehensions/error-messages.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_6/Comprehensions/error-messages.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +// This file tests contextual restrictions for yield and arguments, and is
    1.11 +// derived from js1_8/genexps/regress-634472.js.
    1.12 +
    1.13 +function error(str) {
    1.14 +  var base;
    1.15 +  try {
    1.16 +    // the following line must not be broken up into multiple lines
    1.17 +    base = (function(){try{eval('throw new Error()')}catch(e){return e.lineNumber}})(); eval(str);
    1.18 +    return null;
    1.19 +  } catch (e) {
    1.20 +    e.lineNumber = e.lineNumber - base + 1;
    1.21 +    return e;
    1.22 +  }
    1.23 +}
    1.24 +
    1.25 +const YIELD_PAREN          = error("(function*(){(for (y of (yield 1, 2)) y)})").message;
    1.26 +const GENEXP_YIELD         = error("(function*(){(for (x of yield 1) x)})").message;
    1.27 +const TOP_YIELD            = error("yield").message;
    1.28 +const GENERIC              = error("(for)").message;
    1.29 +const BAD_GENERATOR_SYNTAX = error("(for (x of []) x, 1)").message;
    1.30 +const MISSING_SEMI         = error("yield 1").message;
    1.31 +const MISSING_PAREN        = error("(yield 1)").message;
    1.32 +const PAREN_PAREN          = error("(foo").message;
    1.33 +const FOR_OF_PAREN         = error("(for (x of y, z) w)").message;
    1.34 +
    1.35 +const cases = [
    1.36 +// Expressions involving yield without a value, not currently implemented.  Many
    1.37 +// of these errors would need to be updated.
    1.38 +//{ expr: "yield",        top: TOP_YIELD, fun: null,              gen: GENEXP_YIELD, desc: "simple yield" },
    1.39 +//{ expr: "1, yield",     top: TOP_YIELD, fun: null,              gen: GENEXP_YIELD, desc: "simple yield at end of list" },
    1.40 +//{ expr: "yield, 1",     top: TOP_YIELD, fun: YIELD_PAREN, gen: YIELD_PAREN,  desc: "simple yield in list" },
    1.41 +//{ expr: "(yield)",      top: TOP_YIELD, fun: null,              gen: GENEXP_YIELD, desc: "simple yield, parenthesized" },
    1.42 +//{ expr: "(1, yield)",   top: TOP_YIELD, fun: null,              gen: GENEXP_YIELD, desc: "simple yield at end of list, parenthesized" },
    1.43 +//{ expr: "(yield, 1)",   top: TOP_YIELD, fun: YIELD_PAREN, gen: YIELD_PAREN,  desc: "simple yield in list, parenthesized" },
    1.44 +//{ expr: "((((yield))))",   top: TOP_YIELD, fun: null, gen: GENEXP_YIELD, desc: "deeply nested yield" },
    1.45 +//{ expr: "(for (x of []) yield)",           top: TOP_YIELD, fun: GENERIC,      gen: GENERIC,      desc: "simple yield in genexp" },
    1.46 +//{ expr: "(for (x of []) yield, 1)",        top: TOP_YIELD, fun: YIELD_PAREN,  gen: YIELD_PAREN,  desc: "simple yield in list in genexp" },
    1.47 +//{ expr: "(for (x of []) 1, yield)",        top: TOP_YIELD, fun: GENERIC,      gen: GENERIC,      desc: "simple yield at end of list in genexp" },
    1.48 +//{ expr: "(for (x of []) (yield))",         top: TOP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, desc: "simple yield, parenthesized in genexp" },
    1.49 +//{ expr: "(for (x of []) 1, (yield))",      top: TOP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, desc: "simple yield, parenthesized in list in genexp" },
    1.50 +//{ expr: "(for (x of []) (1, yield))",      top: TOP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, desc: "simple yield at end of list, parenthesized in genexp" },
    1.51 +//{ expr: "(for (x of []) 1, (2, yield))",   top: TOP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, desc: "simple yield at end of list, parenthesized in list in genexp" },
    1.52 +//{ expr: "(for (x of []) (yield, 1))",      top: TOP_YIELD, fun: YIELD_PAREN,  gen: YIELD_PAREN,  desc: "simple yield in list, parenthesized in genexp" },
    1.53 +//{ expr: "(for (x of []) 1, (yield, 2))",   top: TOP_YIELD, fun: YIELD_PAREN,  gen: YIELD_PAREN,  desc: "simple yield in list, parenthesized in list in genexp" },
    1.54 +//{ expr: "(for (x of []) (function*() { yield }))",           top: null, fun: null, gen: null, desc: "legal yield in nested function" },
    1.55 +
    1.56 +  // yield expressions
    1.57 +  { expr: "yield 1",      top: MISSING_SEMI, fun: MISSING_SEMI, gen: null, genexp: GENEXP_YIELD, desc: "yield w/ arg" },
    1.58 +  { expr: "1, yield 2",   top: MISSING_SEMI, fun: MISSING_SEMI, gen: null, genexp: FOR_OF_PAREN, desc: "yield w/ arg at end of list" },
    1.59 +  { expr: "yield 1, 2",   top: MISSING_SEMI, fun: MISSING_SEMI, gen: null, genexp: FOR_OF_PAREN, desc: "yield w/ arg in list" },
    1.60 +  { expr: "(yield 1)",    top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: GENEXP_YIELD, desc: "yield w/ arg, parenthesized" },
    1.61 +  { expr: "(1, yield 2)", top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: GENEXP_YIELD, desc: "yield w/ arg at end of list, parenthesized" },
    1.62 +  { expr: "(yield 1, 2)", top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: YIELD_PAREN, desc: "yield w/ arg in list, parenthesized" },
    1.63 +
    1.64 +  // deeply nested yield expressions
    1.65 +  { expr: "((((yield 1))))", top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: GENEXP_YIELD, desc: "deeply nested yield w/ arg" },
    1.66 +
    1.67 +  // arguments
    1.68 +  { expr: "arguments",    top: null, fun: null, gen: null, genexp: null, desc: "arguments in list" },
    1.69 +  { expr: "1, arguments", top: null, fun: null, gen: null, genexp: FOR_OF_PAREN, desc: "arguments in list" },
    1.70 +
    1.71 +  // yield in generator expressions
    1.72 +  { expr: "(for (x of []) yield 1)",         top: GENEXP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, genexp: GENEXP_YIELD, desc: "yield w/ arg in genexp" },
    1.73 +  { expr: "(for (x of []) yield 1, 2)",      top: GENEXP_YIELD, fun: GENEXP_YIELD,  gen: GENEXP_YIELD, genexp: GENEXP_YIELD, desc: "yield w/ arg in list in genexp" },
    1.74 +  { expr: "(for (x of []) 1, yield 2)",      top: PAREN_PAREN, fun: PAREN_PAREN,  gen: PAREN_PAREN, genexp: PAREN_PAREN, desc: "yield w/ arg at end of list in genexp" },
    1.75 +  { expr: "(for (x of []) (yield 1))",       top: GENEXP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, genexp: GENEXP_YIELD, desc: "yield w/ arg, parenthesized in genexp" },
    1.76 +  { expr: "(for (x of []) 1, (yield 2))",    top: PAREN_PAREN, fun: PAREN_PAREN, gen: PAREN_PAREN, genexp: PAREN_PAREN, desc: "yield w/ arg, parenthesized in list in genexp" },
    1.77 +  { expr: "(for (x of []) (1, yield 2))",    top: GENEXP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, genexp: GENEXP_YIELD, desc: "yield w/ arg at end of list, parenthesized in genexp" },
    1.78 +  { expr: "(for (x of []) 1, (2, yield 3))", top: PAREN_PAREN, fun: PAREN_PAREN, gen: PAREN_PAREN, genexp: PAREN_PAREN, desc: "yield w/ arg at end of list, parenthesized in list in genexp" },
    1.79 +  { expr: "(for (x of []) (yield 1, 2))",    top: YIELD_PAREN, fun: YIELD_PAREN, gen: YIELD_PAREN, genexp: YIELD_PAREN, desc: "yield w/ arg in list, parenthesized in genexp" },
    1.80 +  { expr: "(for (x of []) 1, (yield 2, 3))", top: PAREN_PAREN, fun: PAREN_PAREN, gen: PAREN_PAREN, genexp: PAREN_PAREN, desc: "yield w/ arg in list, parenthesized in list in genexp" },
    1.81 +
    1.82 +  // deeply nested yield in generator expressions
    1.83 +  { expr: "(for (x of []) (((1, yield 2))))",               top: GENEXP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, genexp: GENEXP_YIELD, desc: "deeply nested yield in genexp" },
    1.84 +  { expr: "(for (y of []) (for (x of []) ((1, yield 2))))", top: GENEXP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, genexp: GENEXP_YIELD, desc: "deeply nested yield in multiple genexps" },
    1.85 +
    1.86 +  // arguments in generator expressions
    1.87 +  { expr: "(for (x of []) arguments)",         top: null, fun: null, gen: null, genexp: null, desc: "simple arguments in genexp" },
    1.88 +  { expr: "(for (x of []) 1, arguments)",      top: BAD_GENERATOR_SYNTAX, fun: BAD_GENERATOR_SYNTAX, gen: BAD_GENERATOR_SYNTAX, genexp: BAD_GENERATOR_SYNTAX, desc: "arguments in list in genexp" },
    1.89 +  { expr: "(for (x of []) (arguments))",       top: null, fun: null, gen: null, genexp: null, desc: "arguments, parenthesized in genexp" },
    1.90 +  { expr: "(for (x of []) 1, (arguments))",    top: BAD_GENERATOR_SYNTAX, fun: BAD_GENERATOR_SYNTAX, gen: BAD_GENERATOR_SYNTAX, genexp: BAD_GENERATOR_SYNTAX, desc: "arguments, parenthesized in list in genexp" },
    1.91 +  { expr: "(for (x of []) (1, arguments))",    top: null, fun: null, gen: null, genexp: null, desc: "arguments in list, parenthesized in genexp" },
    1.92 +  { expr: "(for (x of []) 1, (2, arguments))", top: BAD_GENERATOR_SYNTAX, fun: BAD_GENERATOR_SYNTAX, gen: BAD_GENERATOR_SYNTAX, genexp: BAD_GENERATOR_SYNTAX, desc: "arguments in list, parenthesized in list in genexp" },
    1.93 +
    1.94 +  // deeply nested arguments in generator expressions
    1.95 +  { expr: "(for (x of []) (((1, arguments))))",               top: null, fun: null, gen: null, genexp: null, desc: "deeply nested arguments in genexp" },
    1.96 +  { expr: "(for (y of []) (for (x of []) ((1, arguments))))", top: null, fun: null, gen: null, genexp: null, desc: "deeply nested arguments in multiple genexps" },
    1.97 +
    1.98 +  // legal yield/arguments in nested function
    1.99 +  { expr: "(for (x of []) (function*() { yield 1 }))",         top: null, fun: null, gen: null, genexp: null, desc: "legal yield in nested function" },
   1.100 +  { expr: "(for (x of []) (function() { arguments }))",       top: null, fun: null, gen: null, genexp: null, desc: "legal arguments in nested function" },
   1.101 +  { expr: "(for (x of []) (function() arguments))",           top: null, fun: null, gen: null, genexp: null, desc: "legal arguments in nested expression-closure" }
   1.102 +];
   1.103 +
   1.104 +//-----------------------------------------------------------------------------
   1.105 +test();
   1.106 +//-----------------------------------------------------------------------------
   1.107 +
   1.108 +function splitKeyword(str) {
   1.109 +  return str.
   1.110 +//         replace(/[)] yield/, ')\nyield\n').
   1.111 +         replace(/yield ([0-9])/, '\nyield $1\n').
   1.112 +         replace(/yield([^ ]|$)/, '\nyield\n$1').
   1.113 +         replace(/arguments/, '\narguments\n');
   1.114 +}
   1.115 +
   1.116 +function expectError1(err, ctx, msg) {
   1.117 +  reportCompare('object', typeof err,     'exn for: ' + msg);
   1.118 +  reportCompare(ctx,      err.message,    'exn message for: ' + msg);
   1.119 +  if (ctx !== BAD_GENERATOR_SYNTAX && ctx != PAREN_PAREN && ctx != FOR_OF_PAREN)
   1.120 +      reportCompare(2,    err.lineNumber, 'exn token for: ' + msg);
   1.121 +}
   1.122 +
   1.123 +function expectError(expr, wrapCtx, expect, msg) {
   1.124 +  expectError1(error(wrapCtx(expr)), expect, msg);
   1.125 +}
   1.126 +
   1.127 +function expectSuccess(err, msg) {
   1.128 +  reportCompare(null, err, 'parse: ' + msg);
   1.129 +}
   1.130 +
   1.131 +function atTop(str) { return str }
   1.132 +function inFun(str) { return '(function(){' + str + '})' }
   1.133 +function inGen(str) { return '(function*(){' + str + '})' }
   1.134 +function inGenExp(str) { return '(for (y of ' + str + ') y)' }
   1.135 +
   1.136 +function test()
   1.137 +{
   1.138 +  enterFunc ('test');
   1.139 +  printBugNumber(BUGNUMBER);
   1.140 +  printStatus (summary);
   1.141 +
   1.142 +  for (var i = 0, len = cases.length; i < len; i++) {
   1.143 +    var expr, top, fun, gen, genexp, desc;
   1.144 +    expr = cases[i].expr;
   1.145 +    top = cases[i].top;
   1.146 +    fun = cases[i].fun;
   1.147 +    gen = cases[i].gen;
   1.148 +    genexp = cases[i].genexp;
   1.149 +    desc = cases[i].desc;
   1.150 +
   1.151 +    expr = splitKeyword(expr);
   1.152 +
   1.153 +    if (top)
   1.154 +      expectError(expr, atTop, top, 'top-level context, ' + desc);
   1.155 +    else
   1.156 +      expectSuccess(error(expr), 'top-level context, ' + desc);
   1.157 +
   1.158 +    if (fun)
   1.159 +      expectError(expr, inFun, fun, 'function context, ' + desc);
   1.160 +    else
   1.161 +      expectSuccess(error(inFun(expr)), 'function context, ' + desc);
   1.162 +
   1.163 +    if (gen)
   1.164 +      expectError(expr, inGen, gen, 'generator context, ' + desc);
   1.165 +    else
   1.166 +      expectSuccess(error(inGen(expr)), 'generator context, ' + desc);
   1.167 +
   1.168 +    if (genexp)
   1.169 +      expectError(expr, inGenExp, genexp, 'genexp context, ' + desc);
   1.170 +    else
   1.171 +      expectSuccess(error(inGenExp(expr)), 'genexp context, ' + desc);
   1.172 +  }
   1.173 +
   1.174 +  exitFunc ('test');
   1.175 +}

mercurial