From 3c759848141956063f258811223214ec06aedcd1 Mon Sep 17 00:00:00 2001 From: Eddy Bruel Date: Wed, 6 Nov 2013 20:05:23 +0100 Subject: [PATCH] Bug 927116 - Test reflect support for import declarations; r=jorendorff --- js/src/jit-test/lib/match.js | 1 + .../tests/modules/import-declaration.js | 186 ++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 js/src/jit-test/lib/match.js create mode 100644 js/src/jit-test/tests/modules/import-declaration.js diff --git a/js/src/jit-test/lib/match.js b/js/src/jit-test/lib/match.js new file mode 100644 index 00000000000..87d84688ecd --- /dev/null +++ b/js/src/jit-test/lib/match.js @@ -0,0 +1 @@ +loadRelativeToScript("../../tests/js1_8_5/extensions/shell.js"); diff --git a/js/src/jit-test/tests/modules/import-declaration.js b/js/src/jit-test/tests/modules/import-declaration.js new file mode 100644 index 00000000000..54596359a92 --- /dev/null +++ b/js/src/jit-test/tests/modules/import-declaration.js @@ -0,0 +1,186 @@ +load(libdir + "match.js"); +load(libdir + "asserts.js"); + +var { Pattern, MatchError } = Match; + +program = (elts) => Pattern({ + type: "Program", + body: elts +}) +importDeclaration = (specifiers, source) => Pattern({ + type: "ImportDeclaration", + specifiers: specifiers, + source: source +}); +importSpecifier = (id, name) => Pattern({ + type: "ImportSpecifier", + id: id, + name: name +}); +ident = (name) => Pattern({ + type: "Identifier", + name: name +}) +lit = (val) => Pattern({ + type: "Literal", + value: val +}) + +program([ + importDeclaration( + [ + importSpecifier( + ident("default"), + ident("a") + ) + ], + lit("b") + ) +]).assert(Reflect.parse("import a from 'b'")); + +program([ + importDeclaration( + [], + lit("a") + ) +]).assert(Reflect.parse("import {} from 'a'")); + +program([ + importDeclaration( + [ + importSpecifier( + ident("a"), + ident("a") + ) + ], + lit("b") + ) +]).assert(Reflect.parse("import { a } from 'b'")); + +program([ + importDeclaration( + [ + importSpecifier( + ident("a"), + ident("a") + ) + ], + lit("b") + ) +]).assert(Reflect.parse("import { a, } from 'b'")); + +program([ + importDeclaration( + [ + importSpecifier( + ident("a"), + ident("b") + ) + ], + lit("c") + ) +]).assert(Reflect.parse("import { a as b } from 'c'")); + +program([ + importDeclaration( + [ + importSpecifier( + ident("as"), + ident("as") + ) + ], + lit("a") + ) +]).assert(Reflect.parse("import { as as as } from 'a'")); + +program([ + importDeclaration( + [ + importSpecifier( + ident("true"), + ident("a") + ) + ], + lit("b") + ) +]).assert(Reflect.parse("import { true as a } from 'b'")); + +program([ + importDeclaration( + [ + importSpecifier( + ident("a"), + ident("a") + ), + importSpecifier( + ident("b"), + ident("b") + ), + ], + lit("c") + ) +]).assert(Reflect.parse("import { a, b } from 'c'")); + +program([ + importDeclaration( + [ + importSpecifier( + ident("a"), + ident("b") + ), + importSpecifier( + ident("c"), + ident("d") + ), + ], + lit("e") + ) +]).assert(Reflect.parse("import { a as b, c as d } from 'e'")); + +program([ + importDeclaration( + [], + lit("a") + ) +]).assert(Reflect.parse("import 'a'")); + +var loc = Reflect.parse("import { a as b } from 'c'", { + loc: true +}).body[0].loc; + +assertEq(loc.start.line, 1); +assertEq(loc.start.column, 0); +assertEq(loc.start.line, 1); +assertEq(loc.end.column, 26); + +assertThrowsInstanceOf(function () { + Reflect.parse("function f() { import a from 'b' }"); +}, SyntaxError); + +assertThrowsInstanceOf(function () { + Reflect.parse("if (true) import a from 'b'"); +}, SyntaxError); + +assertThrowsInstanceOf(function() { + Reflect.parse("import {"); +}, SyntaxError); + +assertThrowsInstanceOf(function() { + Reflect.parse("import {}"); +}, SyntaxError); + +assertThrowsInstanceOf(function() { + Reflect.parse("import {} from"); +}, SyntaxError); + +assertThrowsInstanceOf(function() { + Reflect.parse("import {,} from 'a'"); +}, SyntaxError); + +assertThrowsInstanceOf(function() { + Reflect.parse("import { a as true } from 'b'"); +}, SyntaxError); + +assertThrowsInstanceOf(function() { + Reflect.parse("import { true } from 'a'"); +}, SyntaxError);