2 function test(actual
, expected
) {
3 if (actual
!== expected
)
4 throw new Error("bad value: " + actual
);
7 function testEval(script
, expected
) {
8 test(eval(script
), expected
);
11 function testEmbedded(value
, expected
) {
12 var template
= `Hello ${value} World`;
13 test(template
, expected
);
19 test(`${``}${``}${``}${""}`, "");
21 test(`Hello World`, "Hello World");
23 World`, "Hello\n World");
25 test(`\uFEFF`, "\uFEFF");
26 testEval("`\uFEFF`", "\uFEFF");
29 test(`\x2020`, "\x2020");
31 for (var ch
of [ '\'', '"', '\\', 'b', 'f', 'n', 'r', 't', 'v' ])
32 testEval("`\\" + ch
+ "`", eval("'\\" + ch
+ "'"));
35 Hello World`, "Hello World");
39 Hello World`, "\nHello World");
41 test(`\u2028\u2029\r\n`, "\u2028\u2029\r\n");
42 test(`\u2028\u2029\n\r\n`, "\u2028\u2029\n\r\n");
43 test(`\u2028200`, "\u2028200");
45 testEmbedded(42, "Hello 42 World");
46 testEmbedded("ISUCA", "Hello ISUCA World");
47 testEmbedded(null, "Hello null World");
48 testEmbedded(undefined, "Hello undefined World");
49 testEmbedded({}, "Hello [object Object] World");
58 test(`Hello ${object.name} and ${array[0]} :D`, "Hello Cocoa and Cappuccino :D");
65 test(`Hello ${ ok() }`, "Hello Cocoa");
74 test(`Hello ${object} :D`, "Hello Cocoa :D");
77 // Immediately adjacent template expressions, with empty intermediate template strings.
78 test(`${"C"}${"o"}${"c"}${"o"}${"a"}`, "Cocoa");
79 test(`${"C"}${"o"}${"c"}${"o"}${" "}`, "Coco ");
82 test(`Hello ${ `Cocoa` }`, "Hello Cocoa");
83 test(`Hello ${ `Co${`c`}oa` }`, "Hello Cocoa");
95 test(`Hello ${ `${ check(0) } ${ ` ${ check(1) } ${ check(2) }
` } ${ check(3) }` } ${ check(4) }
`, "Hello 0 1 2 3 4")
104 throw new Error(num);
111 var template = `${gen(0)} ${gen(1)} ${gen(2)}
`;
116 throw new Error('no error thrown');
117 if (String(error) !== 'Error: 0')
118 throw new Error("bad error: " + String(error));
122 var template = `${52} ${gen(0)} ${gen(1)}
`;
127 throw new Error('no error thrown');
128 if (String(error) !== 'Error: 0')
129 throw new Error("bad error: " + String(error));
145 stat[num].toString = {
149 throw new Error(num);
156 var template = `${gen(0)} ${gen(1)} ${gen(2)}
`;
161 throw new Error('no error thrown');
162 if (String(error) !== 'Error: 0')
163 throw new Error("bad error: " + String(error));
164 test(stat[0].count, 0);
165 test(stat[0].toString.called, true);
166 test(stat[0].toString.count, 1);
167 test(stat[1], undefined);
168 test(stat[2], undefined);
183 stat[num].toString = {
187 throw new Error(num);
194 var template = `${ `${gen(0)}
` } ${ `${gen(1)} ${gen(2)}` }`;
199 throw new Error('no error thrown');
200 if (String(error) !== 'Error: 0')
201 throw new Error("bad error: " + String(error));
202 test(stat[0].count, 0);
203 test(stat[0].toString.called, true);
204 test(stat[0].toString.count, 1);
205 test(stat[1], undefined);
206 test(stat[2], undefined);