]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
1 | |
2 | function testSyntax(script) { | |
3 | try { | |
4 | eval(script); | |
5 | } catch (error) { | |
6 | if (error instanceof SyntaxError) | |
7 | throw new Error("Bad error: " + String(error)); | |
8 | } | |
9 | } | |
10 | ||
11 | function testSyntaxError(script, message) { | |
12 | var error = null; | |
13 | try { | |
14 | eval(script); | |
15 | } catch (e) { | |
16 | error = e; | |
17 | } | |
18 | if (!error) | |
19 | throw new Error("Expected syntax error not thrown"); | |
20 | ||
21 | if (String(error) !== message) | |
22 | throw new Error("Bad error: " + String(error)); | |
23 | } | |
24 | ||
25 | testSyntax("`Hello`"); | |
26 | testSyntax("(`Hello`)"); | |
27 | testSyntax("(`Hello`) + 42"); | |
28 | testSyntax("`\nHello\nWorld`"); | |
29 | testSyntax("`\nHello\n${World}`"); | |
30 | testSyntax("`${World}`"); | |
31 | testSyntax("`${World} Hello`"); | |
32 | testSyntax("`$ {World} Hello`"); | |
33 | testSyntax("`\\uFEFFtest`"); | |
34 | testSyntax("`\r\n`"); | |
35 | testSyntax("`\\r\\n`"); | |
36 | testSyntax("`\n`"); | |
37 | testSyntax("`\\n`"); | |
38 | testSyntax("`\u2028`"); | |
39 | testSyntax("`\\u2028`"); | |
40 | testSyntax("`\u2029`"); | |
41 | testSyntax("`\\u2029`"); | |
42 | testSyntax("`\\0`"); | |
43 | testSyntax("`\0`"); | |
44 | testSyntax("`\\x7f`"); | |
45 | testSyntax("(`Hello`) + 42"); | |
46 | testSyntax("`\\\n`"); | |
47 | testSyntax("`\\\r\n`"); | |
48 | testSyntax("`\\\r`"); | |
49 | ||
50 | testSyntaxError("`Hello", "SyntaxError: Unexpected EOF"); | |
51 | testSyntaxError("`Hello${expr}", "SyntaxError: Unexpected EOF"); | |
52 | testSyntaxError("`Hello${}", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty."); | |
53 | testSyntaxError("`Hello${expr} ${}", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty."); | |
54 | testSyntaxError("`Hello${expr}", "SyntaxError: Unexpected EOF"); | |
55 | testSyntaxError("`Hello${expr} ${expr}", "SyntaxError: Unexpected EOF"); | |
56 | testSyntaxError("`Hello${expr`", "SyntaxError: Unexpected EOF"); | |
57 | testSyntaxError("`Hello${expr} ${expr`", "SyntaxError: Unexpected EOF"); | |
58 | testSyntaxError("`Hello expr${`", "SyntaxError: Unexpected EOF"); | |
59 | testSyntaxError("`Hello${expr} expr${`", "SyntaxError: Unexpected EOF"); | |
60 | testSyntaxError("`Hello ${}`", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty."); | |
61 | testSyntaxError("`Hello${expr} ${}`", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty."); | |
62 | testSyntaxError("`\\07`", "SyntaxError: The only valid numeric escape in strict mode is '\\0'"); | |
63 | for (var i = 1; i < 7; ++i) { | |
64 | testSyntaxError("`\\" + i + "`", "SyntaxError: The only valid numeric escape in strict mode is '\\0'"); | |
65 | testSyntaxError("`${expr}\\" + i + "`", "SyntaxError: The only valid numeric escape in strict mode is '\\0'"); | |
66 | } | |
67 | // \8 and \9 are not allowed. | |
68 | for (var i = 8; i < 9; ++i) { | |
69 | testSyntaxError("`\\" + i + "`", "SyntaxError: The only valid numeric escape in strict mode is '\\0'"); | |
70 | testSyntaxError("`${expr}\\" + i + "`", "SyntaxError: The only valid numeric escape in strict mode is '\\0'"); | |
71 | } | |
72 | testSyntaxError("`\\x7`", "SyntaxError: \\x can only be followed by a hex character sequence"); | |
73 | testSyntaxError("`${expr}\\x7`", "SyntaxError: \\x can only be followed by a hex character sequence"); | |
74 | testSyntaxError("`\\x`", "SyntaxError: \\x can only be followed by a hex character sequence"); | |
75 | testSyntaxError("`${expr}\\x`", "SyntaxError: \\x can only be followed by a hex character sequence"); | |
76 | testSyntaxError("`\\u`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); | |
77 | testSyntaxError("`${expr}\\u`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); | |
78 | testSyntaxError("`\\u2`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); | |
79 | testSyntaxError("`${expr}\\u2`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); | |
80 | testSyntaxError("`\\u20`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); | |
81 | testSyntaxError("`${expr}\\u20`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); | |
82 | testSyntaxError("`\\u202`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); | |
83 | testSyntaxError("`${expr}\\u202`", "SyntaxError: \\u can only be followed by a Unicode character sequence"); |