blob: 2ab5c7bec03e43efe8ac6cbcf8370211b5a16ec4 [file] [log] [blame]
utatane.tea@gmail.comd68d2482015-05-14 16:07:54 +00001function shouldBe(actual, expected) {
2 if (actual !== expected)
3 throw new Error('bad value: ' + JSON.stringify(actual));
4}
5
6var templates = [];
7function tag(siteObject) {
8 templates.push(siteObject);
9}
10
11tag`Hello`;
12tag`World`;
13tag`Hello`;
14shouldBe(templates.length, 3);
15shouldBe(templates[0] !== templates[1], true);
caitp@igalia.coma8b896c2018-02-13 18:10:30 +000016shouldBe(templates[0] !== templates[2], true);
utatane.tea@gmail.comd68d2482015-05-14 16:07:54 +000017
18templates = [];
19tag`Hello\n`;
20tag`Hello\r`;
21tag`Hello\u2028`;
22shouldBe(templates.length, 3);
23shouldBe(templates[0] !== templates[1], true);
24shouldBe(templates[0] !== templates[2], true);
25
26templates = [];
27eval("tag`Hello\n`");
28eval("tag`Hello\r`");
29eval("tag`Hello\u2028`");
30shouldBe(templates.length, 3);
caitp@igalia.coma8b896c2018-02-13 18:10:30 +000031shouldBe(templates[0] !== templates[1], true);
utatane.tea@gmail.comd68d2482015-05-14 16:07:54 +000032shouldBe(templates[0] !== templates[2], true);
33
34templates = [];
35eval("tag`Hello\n`");
36eval("tag`Hello\\n`");
37eval("tag`Hello\r`");
38eval("tag`Hello\\r`");
39shouldBe(templates.length, 4);
40shouldBe(templates[0] !== templates[1], true);
caitp@igalia.coma8b896c2018-02-13 18:10:30 +000041shouldBe(templates[0] !== templates[2], true);
utatane.tea@gmail.comd68d2482015-05-14 16:07:54 +000042shouldBe(templates[0] !== templates[3], true);
43shouldBe(templates[1] !== templates[2], true);
44shouldBe(templates[1] !== templates[3], true);
45shouldBe(templates[2] !== templates[3], true);
46
47var v = 0;
48templates = [];
49eval("tag`Hello\n${v}world`");
50eval("tag`Hello\n${v}world`");
51shouldBe(templates.length, 2);
caitp@igalia.coma8b896c2018-02-13 18:10:30 +000052shouldBe(templates[0] !== templates[1], true);
utatane.tea@gmail.comd68d2482015-05-14 16:07:54 +000053
54var v = 0;
55templates = [];
56eval("tag`Hello${v}\nworld`");
57eval("tag`Hello\n${v}world`");
58shouldBe(templates.length, 2);
59shouldBe(templates[0] !== templates[1], true);
caitp@igalia.coma8b896c2018-02-13 18:10:30 +000060
61var v = 0;
62templates = [];
63for (v = 0; v < 3; ++v) {
64 tag`Hello${v}world`;
65 if (!v) continue;
66 shouldBe(templates[v] === templates[v - 1], true);
67}
68
69templates = [];
70tag`Hello${1}world`;
71tag`Hello${2}world`;
72shouldBe(templates[0] !== templates[1], true);
73
74// Disable eval caching if a tagged template occurs in eval code
75var v = 0;
76templates = [];
77for (v = 0; v < 3; ++v) {
78 eval("tag`Hello${v}world`");
79 if (!v) continue;
80 shouldBe(templates[v] !== templates[v - 1], true);
81}
82
83templates = [];
84eval("tag`Hello${1}world`");
85eval("tag`Hello${2}world`");
86eval("tag`Hello${3}world`");
87shouldBe(templates[0] !== templates[1], true);
88shouldBe(templates[1] !== templates[2], true);
89
90
91// Disable eval caching if a tagged template occurs in a nested function
92var v = 0;
93templates = [];
94for (v = 0; v < 6; v += 2) {
95 eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${v}world` })()");
96 shouldBe(templates[v] === templates[v + 1], true);
97}
98
99shouldBe(templates[0] !== templates[2], true);
100shouldBe(templates[0] !== templates[4], true);
101shouldBe(templates[1] !== templates[3], true);
102shouldBe(templates[1] !== templates[5], true);
103shouldBe(templates[2] !== templates[4], true);
104shouldBe(templates[3] !== templates[5], true);
105
106templates = [];
107eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${1}world` })()");
108eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })()");
109eval("(function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })()");
110shouldBe(templates[0] === templates[1], true);
111shouldBe(templates[0] !== templates[2], true);
112shouldBe(templates[0] !== templates[4], true);
113shouldBe(templates[1] !== templates[3], true);
114shouldBe(templates[1] !== templates[5], true);
115shouldBe(templates[2] === templates[3], true);
116shouldBe(templates[2] !== templates[4], true);
117shouldBe(templates[3] !== templates[5], true);
118shouldBe(templates[4] === templates[5], true);
119
120// Disable eval caching if a tagged template occurs in an even deeper nested function
121var v = 0;
122templates = [];
123for (v = 0; v < 3; ++v) {
124 eval("(function() { (function() { tag`Hello${v}world` })() })()");
125 if (!v) continue;
126 shouldBe(templates[v] !== templates[v - 1], true);
127}
128
129templates = [];
130eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${1}world` })() })()");
131eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })() })()");
132eval("(function() { (function() { for (var i = 0; i < 2; ++i) tag`Hello${2}world` })() })()");
133shouldBe(templates[0] === templates[1], true);
134shouldBe(templates[0] !== templates[2], true);
135shouldBe(templates[0] !== templates[4], true);
136shouldBe(templates[1] !== templates[3], true);
137shouldBe(templates[1] !== templates[5], true);
138shouldBe(templates[2] === templates[3], true);
139shouldBe(templates[2] !== templates[4], true);
140shouldBe(templates[3] !== templates[5], true);
141shouldBe(templates[4] === templates[5], true);