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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6
michael@0 7 // This file tests contextual restrictions for yield and arguments, and is
michael@0 8 // derived from js1_8/genexps/regress-634472.js.
michael@0 9
michael@0 10 function error(str) {
michael@0 11 var base;
michael@0 12 try {
michael@0 13 // the following line must not be broken up into multiple lines
michael@0 14 base = (function(){try{eval('throw new Error()')}catch(e){return e.lineNumber}})(); eval(str);
michael@0 15 return null;
michael@0 16 } catch (e) {
michael@0 17 e.lineNumber = e.lineNumber - base + 1;
michael@0 18 return e;
michael@0 19 }
michael@0 20 }
michael@0 21
michael@0 22 const YIELD_PAREN = error("(function*(){(for (y of (yield 1, 2)) y)})").message;
michael@0 23 const GENEXP_YIELD = error("(function*(){(for (x of yield 1) x)})").message;
michael@0 24 const TOP_YIELD = error("yield").message;
michael@0 25 const GENERIC = error("(for)").message;
michael@0 26 const BAD_GENERATOR_SYNTAX = error("(for (x of []) x, 1)").message;
michael@0 27 const MISSING_SEMI = error("yield 1").message;
michael@0 28 const MISSING_PAREN = error("(yield 1)").message;
michael@0 29 const PAREN_PAREN = error("(foo").message;
michael@0 30 const FOR_OF_PAREN = error("(for (x of y, z) w)").message;
michael@0 31
michael@0 32 const cases = [
michael@0 33 // Expressions involving yield without a value, not currently implemented. Many
michael@0 34 // of these errors would need to be updated.
michael@0 35 //{ expr: "yield", top: TOP_YIELD, fun: null, gen: GENEXP_YIELD, desc: "simple yield" },
michael@0 36 //{ expr: "1, yield", top: TOP_YIELD, fun: null, gen: GENEXP_YIELD, desc: "simple yield at end of list" },
michael@0 37 //{ expr: "yield, 1", top: TOP_YIELD, fun: YIELD_PAREN, gen: YIELD_PAREN, desc: "simple yield in list" },
michael@0 38 //{ expr: "(yield)", top: TOP_YIELD, fun: null, gen: GENEXP_YIELD, desc: "simple yield, parenthesized" },
michael@0 39 //{ expr: "(1, yield)", top: TOP_YIELD, fun: null, gen: GENEXP_YIELD, desc: "simple yield at end of list, parenthesized" },
michael@0 40 //{ expr: "(yield, 1)", top: TOP_YIELD, fun: YIELD_PAREN, gen: YIELD_PAREN, desc: "simple yield in list, parenthesized" },
michael@0 41 //{ expr: "((((yield))))", top: TOP_YIELD, fun: null, gen: GENEXP_YIELD, desc: "deeply nested yield" },
michael@0 42 //{ expr: "(for (x of []) yield)", top: TOP_YIELD, fun: GENERIC, gen: GENERIC, desc: "simple yield in genexp" },
michael@0 43 //{ expr: "(for (x of []) yield, 1)", top: TOP_YIELD, fun: YIELD_PAREN, gen: YIELD_PAREN, desc: "simple yield in list in genexp" },
michael@0 44 //{ expr: "(for (x of []) 1, yield)", top: TOP_YIELD, fun: GENERIC, gen: GENERIC, desc: "simple yield at end of list in genexp" },
michael@0 45 //{ expr: "(for (x of []) (yield))", top: TOP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, desc: "simple yield, parenthesized in genexp" },
michael@0 46 //{ expr: "(for (x of []) 1, (yield))", top: TOP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, desc: "simple yield, parenthesized in list in genexp" },
michael@0 47 //{ 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" },
michael@0 48 //{ 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" },
michael@0 49 //{ expr: "(for (x of []) (yield, 1))", top: TOP_YIELD, fun: YIELD_PAREN, gen: YIELD_PAREN, desc: "simple yield in list, parenthesized in genexp" },
michael@0 50 //{ 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" },
michael@0 51 //{ expr: "(for (x of []) (function*() { yield }))", top: null, fun: null, gen: null, desc: "legal yield in nested function" },
michael@0 52
michael@0 53 // yield expressions
michael@0 54 { expr: "yield 1", top: MISSING_SEMI, fun: MISSING_SEMI, gen: null, genexp: GENEXP_YIELD, desc: "yield w/ arg" },
michael@0 55 { expr: "1, yield 2", top: MISSING_SEMI, fun: MISSING_SEMI, gen: null, genexp: FOR_OF_PAREN, desc: "yield w/ arg at end of list" },
michael@0 56 { expr: "yield 1, 2", top: MISSING_SEMI, fun: MISSING_SEMI, gen: null, genexp: FOR_OF_PAREN, desc: "yield w/ arg in list" },
michael@0 57 { expr: "(yield 1)", top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: GENEXP_YIELD, desc: "yield w/ arg, parenthesized" },
michael@0 58 { expr: "(1, yield 2)", top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: GENEXP_YIELD, desc: "yield w/ arg at end of list, parenthesized" },
michael@0 59 { expr: "(yield 1, 2)", top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: YIELD_PAREN, desc: "yield w/ arg in list, parenthesized" },
michael@0 60
michael@0 61 // deeply nested yield expressions
michael@0 62 { expr: "((((yield 1))))", top: MISSING_PAREN, fun: MISSING_PAREN, gen: null, genexp: GENEXP_YIELD, desc: "deeply nested yield w/ arg" },
michael@0 63
michael@0 64 // arguments
michael@0 65 { expr: "arguments", top: null, fun: null, gen: null, genexp: null, desc: "arguments in list" },
michael@0 66 { expr: "1, arguments", top: null, fun: null, gen: null, genexp: FOR_OF_PAREN, desc: "arguments in list" },
michael@0 67
michael@0 68 // yield in generator expressions
michael@0 69 { expr: "(for (x of []) yield 1)", top: GENEXP_YIELD, fun: GENEXP_YIELD, gen: GENEXP_YIELD, genexp: GENEXP_YIELD, desc: "yield w/ arg in genexp" },
michael@0 70 { 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" },
michael@0 71 { 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" },
michael@0 72 { 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" },
michael@0 73 { 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" },
michael@0 74 { 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" },
michael@0 75 { 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" },
michael@0 76 { 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" },
michael@0 77 { 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" },
michael@0 78
michael@0 79 // deeply nested yield in generator expressions
michael@0 80 { 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" },
michael@0 81 { 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" },
michael@0 82
michael@0 83 // arguments in generator expressions
michael@0 84 { expr: "(for (x of []) arguments)", top: null, fun: null, gen: null, genexp: null, desc: "simple arguments in genexp" },
michael@0 85 { 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" },
michael@0 86 { expr: "(for (x of []) (arguments))", top: null, fun: null, gen: null, genexp: null, desc: "arguments, parenthesized in genexp" },
michael@0 87 { 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" },
michael@0 88 { expr: "(for (x of []) (1, arguments))", top: null, fun: null, gen: null, genexp: null, desc: "arguments in list, parenthesized in genexp" },
michael@0 89 { 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" },
michael@0 90
michael@0 91 // deeply nested arguments in generator expressions
michael@0 92 { expr: "(for (x of []) (((1, arguments))))", top: null, fun: null, gen: null, genexp: null, desc: "deeply nested arguments in genexp" },
michael@0 93 { expr: "(for (y of []) (for (x of []) ((1, arguments))))", top: null, fun: null, gen: null, genexp: null, desc: "deeply nested arguments in multiple genexps" },
michael@0 94
michael@0 95 // legal yield/arguments in nested function
michael@0 96 { expr: "(for (x of []) (function*() { yield 1 }))", top: null, fun: null, gen: null, genexp: null, desc: "legal yield in nested function" },
michael@0 97 { expr: "(for (x of []) (function() { arguments }))", top: null, fun: null, gen: null, genexp: null, desc: "legal arguments in nested function" },
michael@0 98 { expr: "(for (x of []) (function() arguments))", top: null, fun: null, gen: null, genexp: null, desc: "legal arguments in nested expression-closure" }
michael@0 99 ];
michael@0 100
michael@0 101 //-----------------------------------------------------------------------------
michael@0 102 test();
michael@0 103 //-----------------------------------------------------------------------------
michael@0 104
michael@0 105 function splitKeyword(str) {
michael@0 106 return str.
michael@0 107 // replace(/[)] yield/, ')\nyield\n').
michael@0 108 replace(/yield ([0-9])/, '\nyield $1\n').
michael@0 109 replace(/yield([^ ]|$)/, '\nyield\n$1').
michael@0 110 replace(/arguments/, '\narguments\n');
michael@0 111 }
michael@0 112
michael@0 113 function expectError1(err, ctx, msg) {
michael@0 114 reportCompare('object', typeof err, 'exn for: ' + msg);
michael@0 115 reportCompare(ctx, err.message, 'exn message for: ' + msg);
michael@0 116 if (ctx !== BAD_GENERATOR_SYNTAX && ctx != PAREN_PAREN && ctx != FOR_OF_PAREN)
michael@0 117 reportCompare(2, err.lineNumber, 'exn token for: ' + msg);
michael@0 118 }
michael@0 119
michael@0 120 function expectError(expr, wrapCtx, expect, msg) {
michael@0 121 expectError1(error(wrapCtx(expr)), expect, msg);
michael@0 122 }
michael@0 123
michael@0 124 function expectSuccess(err, msg) {
michael@0 125 reportCompare(null, err, 'parse: ' + msg);
michael@0 126 }
michael@0 127
michael@0 128 function atTop(str) { return str }
michael@0 129 function inFun(str) { return '(function(){' + str + '})' }
michael@0 130 function inGen(str) { return '(function*(){' + str + '})' }
michael@0 131 function inGenExp(str) { return '(for (y of ' + str + ') y)' }
michael@0 132
michael@0 133 function test()
michael@0 134 {
michael@0 135 enterFunc ('test');
michael@0 136 printBugNumber(BUGNUMBER);
michael@0 137 printStatus (summary);
michael@0 138
michael@0 139 for (var i = 0, len = cases.length; i < len; i++) {
michael@0 140 var expr, top, fun, gen, genexp, desc;
michael@0 141 expr = cases[i].expr;
michael@0 142 top = cases[i].top;
michael@0 143 fun = cases[i].fun;
michael@0 144 gen = cases[i].gen;
michael@0 145 genexp = cases[i].genexp;
michael@0 146 desc = cases[i].desc;
michael@0 147
michael@0 148 expr = splitKeyword(expr);
michael@0 149
michael@0 150 if (top)
michael@0 151 expectError(expr, atTop, top, 'top-level context, ' + desc);
michael@0 152 else
michael@0 153 expectSuccess(error(expr), 'top-level context, ' + desc);
michael@0 154
michael@0 155 if (fun)
michael@0 156 expectError(expr, inFun, fun, 'function context, ' + desc);
michael@0 157 else
michael@0 158 expectSuccess(error(inFun(expr)), 'function context, ' + desc);
michael@0 159
michael@0 160 if (gen)
michael@0 161 expectError(expr, inGen, gen, 'generator context, ' + desc);
michael@0 162 else
michael@0 163 expectSuccess(error(inGen(expr)), 'generator context, ' + desc);
michael@0 164
michael@0 165 if (genexp)
michael@0 166 expectError(expr, inGenExp, genexp, 'genexp context, ' + desc);
michael@0 167 else
michael@0 168 expectSuccess(error(inGenExp(expr)), 'genexp context, ' + desc);
michael@0 169 }
michael@0 170
michael@0 171 exitFunc ('test');
michael@0 172 }

mercurial