js/src/jit-test/tests/modules/import-declaration.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit-test/tests/modules/import-declaration.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,186 @@
     1.4 +load(libdir + "match.js");
     1.5 +load(libdir + "asserts.js");
     1.6 +
     1.7 +var { Pattern, MatchError } = Match;
     1.8 +
     1.9 +program = (elts) => Pattern({
    1.10 +    type: "Program",
    1.11 +    body: elts
    1.12 +})
    1.13 +importDeclaration = (specifiers, source) => Pattern({
    1.14 +    type: "ImportDeclaration",
    1.15 +    specifiers: specifiers,
    1.16 +    source: source
    1.17 +});
    1.18 +importSpecifier = (id, name) => Pattern({
    1.19 +    type: "ImportSpecifier",
    1.20 +    id: id,
    1.21 +    name: name
    1.22 +});
    1.23 +ident = (name) => Pattern({
    1.24 +    type: "Identifier",
    1.25 +    name: name
    1.26 +})
    1.27 +lit = (val) => Pattern({
    1.28 +    type: "Literal",
    1.29 +    value: val
    1.30 +})
    1.31 +
    1.32 +program([
    1.33 +    importDeclaration(
    1.34 +        [
    1.35 +            importSpecifier(
    1.36 +                ident("default"),
    1.37 +                ident("a")
    1.38 +            )
    1.39 +        ],
    1.40 +        lit("b")
    1.41 +    )
    1.42 +]).assert(Reflect.parse("import a from 'b'"));
    1.43 +
    1.44 +program([
    1.45 +    importDeclaration(
    1.46 +        [],
    1.47 +        lit("a")
    1.48 +    )
    1.49 +]).assert(Reflect.parse("import {} from 'a'"));
    1.50 +
    1.51 +program([
    1.52 +    importDeclaration(
    1.53 +        [
    1.54 +            importSpecifier(
    1.55 +                ident("a"),
    1.56 +                ident("a")
    1.57 +            )
    1.58 +        ],
    1.59 +        lit("b")
    1.60 +    )
    1.61 +]).assert(Reflect.parse("import { a } from 'b'"));
    1.62 +
    1.63 +program([
    1.64 +    importDeclaration(
    1.65 +        [
    1.66 +            importSpecifier(
    1.67 +                ident("a"),
    1.68 +                ident("a")
    1.69 +            )
    1.70 +        ],
    1.71 +        lit("b")
    1.72 +    )
    1.73 +]).assert(Reflect.parse("import { a, } from 'b'"));
    1.74 +
    1.75 +program([
    1.76 +    importDeclaration(
    1.77 +        [
    1.78 +            importSpecifier(
    1.79 +                ident("a"),
    1.80 +                ident("b")
    1.81 +            )
    1.82 +        ],
    1.83 +        lit("c")
    1.84 +    )
    1.85 +]).assert(Reflect.parse("import { a as b } from 'c'"));
    1.86 +
    1.87 +program([
    1.88 +    importDeclaration(
    1.89 +        [
    1.90 +            importSpecifier(
    1.91 +                ident("as"),
    1.92 +                ident("as")
    1.93 +            )
    1.94 +        ],
    1.95 +        lit("a")
    1.96 +    )
    1.97 +]).assert(Reflect.parse("import { as as as } from 'a'"));
    1.98 +
    1.99 +program([
   1.100 +    importDeclaration(
   1.101 +        [
   1.102 +            importSpecifier(
   1.103 +                ident("true"),
   1.104 +                ident("a")
   1.105 +            )
   1.106 +        ],
   1.107 +        lit("b")
   1.108 +    )
   1.109 +]).assert(Reflect.parse("import { true as a } from 'b'"));
   1.110 +
   1.111 +program([
   1.112 +    importDeclaration(
   1.113 +        [
   1.114 +            importSpecifier(
   1.115 +                ident("a"),
   1.116 +                ident("a")
   1.117 +            ),
   1.118 +            importSpecifier(
   1.119 +                ident("b"),
   1.120 +                ident("b")
   1.121 +            ),
   1.122 +        ],
   1.123 +        lit("c")
   1.124 +    )
   1.125 +]).assert(Reflect.parse("import { a, b } from 'c'"));
   1.126 +
   1.127 +program([
   1.128 +    importDeclaration(
   1.129 +        [
   1.130 +            importSpecifier(
   1.131 +                ident("a"),
   1.132 +                ident("b")
   1.133 +            ),
   1.134 +            importSpecifier(
   1.135 +                ident("c"),
   1.136 +                ident("d")
   1.137 +            ),
   1.138 +        ],
   1.139 +        lit("e")
   1.140 +    )
   1.141 +]).assert(Reflect.parse("import { a as b, c as d } from 'e'"));
   1.142 +
   1.143 +program([
   1.144 +    importDeclaration(
   1.145 +        [],
   1.146 +        lit("a")
   1.147 +    )
   1.148 +]).assert(Reflect.parse("import 'a'"));
   1.149 +
   1.150 +var loc = Reflect.parse("import { a as b } from 'c'", {
   1.151 +    loc: true
   1.152 +}).body[0].loc;
   1.153 +
   1.154 +assertEq(loc.start.line, 1);
   1.155 +assertEq(loc.start.column, 0);
   1.156 +assertEq(loc.start.line, 1);
   1.157 +assertEq(loc.end.column, 26);
   1.158 +
   1.159 +assertThrowsInstanceOf(function () {
   1.160 +   Reflect.parse("function f() { import a from 'b' }");
   1.161 +}, SyntaxError);
   1.162 +
   1.163 +assertThrowsInstanceOf(function () {
   1.164 +   Reflect.parse("if (true) import a from 'b'");
   1.165 +}, SyntaxError);
   1.166 +
   1.167 +assertThrowsInstanceOf(function() {
   1.168 +    Reflect.parse("import {");
   1.169 +}, SyntaxError);
   1.170 +
   1.171 +assertThrowsInstanceOf(function() {
   1.172 +    Reflect.parse("import {}");
   1.173 +}, SyntaxError);
   1.174 +
   1.175 +assertThrowsInstanceOf(function() {
   1.176 +    Reflect.parse("import {} from");
   1.177 +}, SyntaxError);
   1.178 +
   1.179 +assertThrowsInstanceOf(function() {
   1.180 +    Reflect.parse("import {,} from 'a'");
   1.181 +}, SyntaxError);
   1.182 +
   1.183 +assertThrowsInstanceOf(function() {
   1.184 +    Reflect.parse("import { a as true } from 'b'");
   1.185 +}, SyntaxError);
   1.186 +
   1.187 +assertThrowsInstanceOf(function() {
   1.188 +    Reflect.parse("import { true } from 'a'");
   1.189 +}, SyntaxError);

mercurial