Debian Codegraph @

js/src/tests/non262/template-strings/templLit.js

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 // This test case is weird in the sense the actual work happens at the eval
6 // at the end. If template strings are not enabled, the test cases would throw
7 // a syntax error and we'd have failure reported. To avoid that, the entire
8 // test case is commented out and is part of a function. We use toString to
9 // get the string version, obtain the actual lines to run, and then use eval to
10 // do the actual evaluation.
11
12 function syntaxError (script) {
13 try {
14 Function(script);
15 } catch (e) {
16 if (e.name === "SyntaxError") {
17 return;
18 }
19 }
20 throw new Error('Expected syntax error: ' + script);
21 }
22
23 // TEST BEGIN
24
25
26
27 // combinations of substitutions
28 assertEq("abcdef", `ab${"cd"}ef`);
29 assertEq("ab9ef", `ab${4+5}ef`);
30 assertEq("cdef", `${"cd"}ef`);
31 assertEq("abcd", `ab${"cd"}`);
32 assertEq("cd", `${"cd"}`);
33 assertEq("", `${""}`);
34 assertEq("4", `${4}`);
35
36 // multiple substitutions
37 assertEq("abcdef", `ab${"cd"}e${"f"}`);
38 assertEq("abcdef", `ab${"cd"}${"e"}f`);
39 assertEq("abcdef", `a${"b"}${"cd"}e${"f"}`);
40 assertEq("abcdef", `${"ab"}${"cd"}${"ef"}`);
41
42 // inception
43 assertEq("abcdef", `a${`b${"cd"}e${"f"}`}`);
44
45 syntaxError("`${}`");
46 syntaxError("`${`");
47 syntaxError("`${\\n}`");
48 syntaxError("`${yield 0}`");
49
50 // Extra whitespace inside a template substitution is ignored.
51 assertEq(`${
52 0
53 }`, "0");
54
55 assertEq(`${ // Comments work in template substitutions.
56 // Even comments that look like code:
57 // 0}`, "FAIL"); /* NOTE: This whole line is a comment.
58 0}`, "0");
59
60 // Template substitutions are expressions, not statements.
61 syntaxError("`${0;}`");
62 assertEq(`${{}}`, "[object Object]");
63 assertEq(`${
64 function f() {
65 return "ok";
66 }()
67 }`, "ok");
68
69 // Template substitutions can have side effects.
70 var x = 0;
71 assertEq(`= ${x += 1}`, "= 1");
72 assertEq(x, 1);
73
74 // The production for a template substitution is Expression, not
75 // AssignmentExpression.
76 x = 0;
77 assertEq(`${++x, "o"}k`, "ok");
78 assertEq(x, 1);
79
80 // --> is not a comment inside a template.
81 assertEq(`
82 --> this is text
83 `, "\n--> this is text\n");
84
85 // reentrancy
86 function f(n) {
87 if (n === 0)
88 return "";
89 return `${n}${f(n - 1)}`;
90 }
91 assertEq(f(9), "987654321");
92
93 // Template string substitutions in generator functions can yield.
94 function* g() {
95 return `${yield 1} ${yield 2}`;
96 }
97
98 var it = g();
99 var next = it.next();
100 assertEq(next.done, false);
101 assertEq(next.value, 1);
102 next = it.next("hello");
103 assertEq(next.done, false);
104 assertEq(next.value, 2);
105 next = it.next("world");
106 assertEq(next.done, true);
107 assertEq(next.value, "hello world");
108
109 // undefined
110 assertEq(`${void 0}`, "undefined");
111 assertEq(`${Object.doesNotHaveThisProperty}`, "undefined");
112
113 // toString behavior
114 assertEq("<toString>", `${{valueOf: () => "<valueOf>", toString: () => "<toString>"}}`);
115 assertEq("Hi 42", Function("try {`${{toString: () => { throw 42;}}}`} catch(e) {return \"Hi \" + e;}")());
116
117
118 reportCompare(0, 0, "ok");