]> git.saurik.com Git - apple/javascriptcore.git/blame - tests/stress/template-literal-syntax.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / template-literal-syntax.js
CommitLineData
ed1e77d3
A
1
2function 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
11function 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
25testSyntax("`Hello`");
26testSyntax("(`Hello`)");
27testSyntax("(`Hello`) + 42");
28testSyntax("`\nHello\nWorld`");
29testSyntax("`\nHello\n${World}`");
30testSyntax("`${World}`");
31testSyntax("`${World} Hello`");
32testSyntax("`$ {World} Hello`");
33testSyntax("`\\uFEFFtest`");
34testSyntax("`\r\n`");
35testSyntax("`\\r\\n`");
36testSyntax("`\n`");
37testSyntax("`\\n`");
38testSyntax("`\u2028`");
39testSyntax("`\\u2028`");
40testSyntax("`\u2029`");
41testSyntax("`\\u2029`");
42testSyntax("`\\0`");
43testSyntax("`\0`");
44testSyntax("`\\x7f`");
45testSyntax("(`Hello`) + 42");
46testSyntax("`\\\n`");
47testSyntax("`\\\r\n`");
48testSyntax("`\\\r`");
49
50testSyntaxError("`Hello", "SyntaxError: Unexpected EOF");
51testSyntaxError("`Hello${expr}", "SyntaxError: Unexpected EOF");
52testSyntaxError("`Hello${}", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty.");
53testSyntaxError("`Hello${expr} ${}", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty.");
54testSyntaxError("`Hello${expr}", "SyntaxError: Unexpected EOF");
55testSyntaxError("`Hello${expr} ${expr}", "SyntaxError: Unexpected EOF");
56testSyntaxError("`Hello${expr`", "SyntaxError: Unexpected EOF");
57testSyntaxError("`Hello${expr} ${expr`", "SyntaxError: Unexpected EOF");
58testSyntaxError("`Hello expr${`", "SyntaxError: Unexpected EOF");
59testSyntaxError("`Hello${expr} expr${`", "SyntaxError: Unexpected EOF");
60testSyntaxError("`Hello ${}`", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty.");
61testSyntaxError("`Hello${expr} ${}`", "SyntaxError: Unexpected token '}'. Template literal expression cannot be empty.");
62testSyntaxError("`\\07`", "SyntaxError: The only valid numeric escape in strict mode is '\\0'");
63for (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.
68for (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}
72testSyntaxError("`\\x7`", "SyntaxError: \\x can only be followed by a hex character sequence");
73testSyntaxError("`${expr}\\x7`", "SyntaxError: \\x can only be followed by a hex character sequence");
74testSyntaxError("`\\x`", "SyntaxError: \\x can only be followed by a hex character sequence");
75testSyntaxError("`${expr}\\x`", "SyntaxError: \\x can only be followed by a hex character sequence");
76testSyntaxError("`\\u`", "SyntaxError: \\u can only be followed by a Unicode character sequence");
77testSyntaxError("`${expr}\\u`", "SyntaxError: \\u can only be followed by a Unicode character sequence");
78testSyntaxError("`\\u2`", "SyntaxError: \\u can only be followed by a Unicode character sequence");
79testSyntaxError("`${expr}\\u2`", "SyntaxError: \\u can only be followed by a Unicode character sequence");
80testSyntaxError("`\\u20`", "SyntaxError: \\u can only be followed by a Unicode character sequence");
81testSyntaxError("`${expr}\\u20`", "SyntaxError: \\u can only be followed by a Unicode character sequence");
82testSyntaxError("`\\u202`", "SyntaxError: \\u can only be followed by a Unicode character sequence");
83testSyntaxError("`${expr}\\u202`", "SyntaxError: \\u can only be followed by a Unicode character sequence");