]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/stress/template-literal.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / template-literal.js
1
2 function test(actual, expected) {
3 if (actual !== expected)
4 throw new Error("bad value: " + actual);
5 }
6
7 function testEval(script, expected) {
8 test(eval(script), expected);
9 }
10
11 function testEmbedded(value, expected) {
12 var template = `Hello ${value} World`;
13 test(template, expected);
14 }
15
16 test(``, "");
17 test(`${""}`, "");
18 test(`${``}`, "");
19 test(`${``}${``}${``}${""}`, "");
20
21 test(`Hello World`, "Hello World");
22 test(`Hello
23 World`, "Hello\n World");
24
25 test(`\uFEFF`, "\uFEFF");
26 testEval("`\uFEFF`", "\uFEFF");
27
28 test(`\x20`, "\x20");
29 test(`\x2020`, "\x2020");
30
31 for (var ch of [ '\'', '"', '\\', 'b', 'f', 'n', 'r', 't', 'v' ])
32 testEval("`\\" + ch + "`", eval("'\\" + ch + "'"));
33
34 test(`\
35 Hello World`, "Hello World");
36
37 test(`\
38
39 Hello World`, "\nHello World");
40
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");
44
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");
50
51 (function () {
52 var object = {
53 name: "Cocoa",
54 };
55 var array = [
56 "Cappuccino"
57 ]
58 test(`Hello ${object.name} and ${array[0]} :D`, "Hello Cocoa and Cappuccino :D");
59 }());
60
61 (function () {
62 function ok() {
63 return "Cocoa";
64 }
65 test(`Hello ${ ok() }`, "Hello Cocoa");
66 }());
67
68 (function () {
69 var object = {
70 toString() {
71 return 'Cocoa';
72 }
73 };
74 test(`Hello ${object} :D`, "Hello Cocoa :D");
75 }());
76
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 ");
80
81 // Nested templates.
82 test(`Hello ${ `Cocoa` }`, "Hello Cocoa");
83 test(`Hello ${ `Co${`c`}oa` }`, "Hello Cocoa");
84
85 // Evaluation order
86 (function () {
87 var count = 0;
88
89 function check(num) {
90 var now = count++;
91 test(now, num);
92 return now;
93 }
94
95 test(`Hello ${ `${ check(0) } ${ ` ${ check(1) } ${ check(2) }` } ${ check(3) }` } ${ check(4) }`, "Hello 0 1 2 3 4")
96 }());
97
98 // Exceptions
99
100 (function () {
101 function gen(num) {
102 return {
103 toString() {
104 throw new Error(num);
105 }
106 };
107 }
108
109 var error = null;
110 try {
111 var template = `${gen(0)} ${gen(1)} ${gen(2)}`;
112 } catch (e) {
113 error = e;
114 }
115 if (!error)
116 throw new Error('no error thrown');
117 if (String(error) !== 'Error: 0')
118 throw new Error("bad error: " + String(error));
119
120 var error = null;
121 try {
122 var template = `${52} ${gen(0)} ${gen(1)}`;
123 } catch (e) {
124 error = e;
125 }
126 if (!error)
127 throw new Error('no error thrown');
128 if (String(error) !== 'Error: 0')
129 throw new Error("bad error: " + String(error));
130 }());
131
132
133 (function () {
134 var stat= {
135 count: 0
136 };
137 function gen(num) {
138 stat[num] = {
139 called: true,
140 count: stat.count++,
141 toString: null
142 };
143 return {
144 toString() {
145 stat[num].toString = {
146 called: true,
147 count: stat.count++,
148 };
149 throw new Error(num);
150 }
151 };
152 }
153
154 var error = null;
155 try {
156 var template = `${gen(0)} ${gen(1)} ${gen(2)}`;
157 } catch (e) {
158 error = e;
159 }
160 if (!error)
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);
169 }());
170
171 (function () {
172 var stat= {
173 count: 0
174 };
175 function gen(num) {
176 stat[num] = {
177 called: true,
178 count: stat.count++,
179 toString: null
180 };
181 return {
182 toString() {
183 stat[num].toString = {
184 called: true,
185 count: stat.count++,
186 };
187 throw new Error(num);
188 }
189 };
190 }
191
192 var error = null;
193 try {
194 var template = `${ `${gen(0)}` } ${ `${gen(1)} ${gen(2)}` }`;
195 } catch (e) {
196 error = e;
197 }
198 if (!error)
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);
207 }());