-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
339 lines (330 loc) · 9.51 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"use strict";
const razorleaf = require("./");
const Markup = require("./markup");
const parser = require("./parser");
const PARSER_ERROR_MESSAGE = /^(.+) at line \d+, character \d+ in <Razor Leaf template>/;
const tests = [
{
name: "interpolation without identifiers",
template: '"#{5}"',
expected: { output: "5" },
},
{
name: "consecutive expressions",
template: '"#{1 + 2}#{3 | 4}"',
expected: { output: "37" },
},
{
name: "content escaping",
template: '"#{"<>&\\"<>&\\""}"',
expected: { output: '<>&"<>&"' },
},
{
name: "attribute escaping",
template: 'div data-test: "#{"<>&\\"<>&\\""}"',
expected: { output: '<div data-test="<>&"<>&""></div>' },
},
{
name: "unescaped content",
template: '!"<b>test</b>"',
expected: { output: "<b>test</b>" },
},
{
name: "unescaped expression",
template: '!"#{data.unsafe}"',
data: {
unsafe: Markup.unsafe("<b>unsafe</b>"),
},
expected: { output: "<b>unsafe</b>" },
},
{
name: "unescaped string expression",
template: '!"#{"<b>unsafe</b>"}"',
expected: { error: "Unescaped content must be an instance of Markup" },
},
{
name: "force-unescaped string expression",
template: '!!"#{"<b>unsafe</b>"}"',
expected: { output: "<b>unsafe</b>" },
},
{
name: "escaped double-quotes",
template: '"println!(\\"Hello, world!\\")"',
expected: { output: 'println!("Hello, world!")' },
},
{
name: "escaped backslash",
template: '"\\\\"',
expected: { output: "\\" },
},
{
name: "comment after boolean attribute",
template: "div\n\tdata-test: \"\"\n\t# comment",
expected: { output: "<div data-test></div>" },
},
{
name: "non-conflicting output variable",
template: 'do var output;\n"#{typeof output}"',
expected: { output: "undefined" },
},
{
name: "non-conflicting output variable with escaped references",
template: 'do var \\u006futput;\n"#{typeof \\u006futput}"',
expected: { output: "undefined" },
},
{
name: "non-conflicting output variable with extended escaped references",
template: 'do var \\u{6f}utput;\n"#{typeof \\u{6f}utput}"',
expected: { output: "undefined" },
},
{
name: "including attributes",
template: "script include async",
include: {
async: 'async: ""',
},
expected: { output: "<script async></script>" },
},
{
name: "conditional attributes",
template: 'div "Hello, world!" \n\tif true\n\t\t.pass id: "#{data.example}"\n\tif false\n\t\t.fail data-fail: "true"',
data: { example: "example" },
expected: { output: '<div id="example" class="pass">Hello, world!</div>' },
},
{
name: "reordering of mixed conditionals",
template: 'do var x = true;\ndiv "Hello, world!" \n\tif x\n\t\t"#{data.example}"\n\tif x = false\n\t\tdata-fail: "true"',
data: { example: "example" },
expected: { output: "<div>Hello, world!example</div>" },
},
{
name: "nested conditionals",
template: 'div if true\n\tif 1\n\t\t"Good" data-example: ""',
expected: { output: "<div data-example>Good</div>" },
},
{
name: "block appension",
template: 'extends layout\nappend title "two"',
include: {
layout: 'doctype\nhtml\n\thead\n\t\ttitle block title "one, "',
},
expected: { output: "<!DOCTYPE html><html><head><title>one, two</title></head></html>" },
},
{
name: "loop with index",
template: 'for x, y of [1, 2, 3]\n\t"#{x * (y + 1)}"',
expected: { output: "149" },
},
{
name: "loop over non-array iterable",
template: 'for x of new Set([2, 2, 3])\n\t"#{x}"',
expected: { output: "23" },
},
{
name: "non-conflicting variable in loop with index",
template: 'for x, i of [1, 2, 3]\n\tfor y of [4, 5, 6]\n\t\t"#{i}"',
expected: { output: "000111222" },
},
{
name: "non-conflicting variable in nested loops with index",
template: 'for x, i of [1, 2, 3]\n\tfor y, i of [4, 5, 6]\n\t\tfor z, i of [7, 8, 9]\n\t\t\t"#{i}"',
expected: { output: "012012012012012012012012012" },
},
{
name: "modifying blocks in root template",
template: "replace a",
expected: { error: "Unexpected block replacement in a root template" },
},
{
name: "carriage return/newline combination",
template: "hello\r\n\tworld",
expected: { output: "<hello><world></world></hello>" },
},
{
name: "globals",
template: '"#{data.count} red balloon#{s(data.count)}"',
data: { count: 99 },
options: {
globals: {
s: n => n === 1 ? "" : "s",
},
},
expected: { output: "99 red balloons" },
},
{
name: "attributes in else after content in if",
template: 'div\n\tif false\n\t\t"fail"\n\telse\n\t\tdata-status: "pass"',
expected: { output: "<div data-status=pass></div>" },
},
{
name: "elif inside element",
template: 'div\n\tif false\n\t\t"foo"\n\telif true\n\t\t"bar"',
expected: { output: "<div>bar</div>" },
},
{
name: "unexpected character",
template: "$",
expected: { error: "Unexpected $" },
},
{
name: "character with two-byte UTF-16 representation",
template: "𝑎",
expected: { error: "Unexpected U+1D44E" },
},
{
name: "initial multiple-tab indentation",
template: "div\n\t\tdiv",
expected: { error: "Excessive indent of 2 tabs; one tab always represents one indent level" },
},
{
name: "hasOwnProperty as a variable name",
template: 'do var hasOwnProperty;\nfor x of [1, 2, 3]\n\t"#{x}"',
expected: { output: "123" },
},
{
name: "hasOwnProperty as a block name",
template: "extends layout\nreplace hasOwnProperty",
include: {
layout: "block hasOwnProperty",
},
expected: { output: "" },
},
{
name: "block substitution with attributes",
template: "extends layout\nreplace content\n\t.test-pass",
include: {
layout: "body\n\tblock content\n\t\t.test-fail",
},
expected: { output: "<body class=test-pass></body>" },
},
{
name: "extended Unicode escape",
template: '"\\u{1f60a}"',
expected: { output: "😊" },
},
{
name: "invalid extended Unicode escape",
template: '"\\u{110000}"',
expected: { error: "Undefined Unicode code-point" },
},
{
name: "invalid escape",
template: '"\\g"',
expected: { error: "Expected escape sequence" },
},
{
name: "bad interpolation for ()",
template: '"#{0)+(0}"',
expected: { error: "No interpolation is a valid JavaScript expression (of ['0)+(0'])" },
},
{
name: "bad interpolation for () + void combined",
template: '"#{/*/0)}"',
expected: { error: "No interpolation is a valid JavaScript expression (of ['/*/0)'])" },
},
{
name: "included macros",
template: "include macros\ntest()",
include: {
macros: 'macro test()\n\t"pass"',
},
expected: { output: "pass" },
},
{
name: "recursive macros",
template: 'macro countdown(n)\n\tif n === 1\n\t\t"1"\n\telse\n\t\t"#{n}, " countdown(n - 1)\ncountdown(5)\n"; "\ncountdown(5)',
expected: { output: "5, 4, 3, 2, 1; 5, 4, 3, 2, 1" },
},
{
name: "macros with conflicting variable names",
template: 'do var x = 5;\nmacro test(x)\n\t"#{x},"\ntest(12)\n"#{x}"',
expected: { output: "12,5" },
},
{
name: "macro self-calls with different blocks",
template: "macro test() yield\ntest() test()",
expected: { output: "" },
},
{
name: "macro self-calls with different blocks in attribute context",
template: "macro test() yield\na test() test()",
expected: { output: "<a></a>" },
},
{
name: "repeated macros in attribute context",
template: 'macro test(x) "#{x}"\ndiv\n\ttest(1)\n\ttest(2)',
expected: { output: "<div>12</div>" },
},
{
name: "macro with conflicting variable name in void element context",
template: "do var x = 5;\nmacro test(x)\nimg test(12)",
expected: { output: "<img>" },
},
{
name: "macro called multiple times in attribute context",
template: "macro a(x) \"#{x}\"\nmacro b(y) div a(y)\n\ndiv\n\tb(1)\n\tb(2)",
expected: { output: "<div><div>1</div><div>2</div></div>" },
},
{
name: "code blocks",
template: "do let r = `0\n\t`\n\tr += `1\n\t\t`\n\tr += 2\n\"#{r}\"",
expected: { output: "0\n1\n\t2" },
},
{
name: "code blocks with first indentation in template",
template: "do let x;\ndo\n\tif (true)\n\t\tx = 5;\n\n\"#{x}\"",
expected: { output: "5" },
},
{
name: "boolean attributes",
template: "input disabled: \"\"",
expected: { output: "<input disabled>" },
},
{
name: "conditional boolean attribute shorthand",
template: "button disabled: if true\nbutton disabled: if false",
expected: { output: "<button disabled></button><button></button>" },
},
{
name: "attribute split across multiple lines incorrectly",
template: 'html\n\tlang:\n\t"fr"',
expected: { error: "Expected attribute value or “if”" },
},
{
name: "content `if` blocks after code",
template: 'div\n\tdo const x = 5;\n\tif x === 5\n\t\t"pass"',
expected: { output: "<div>pass</div>" },
fails: true,
},
{
name: "attribute `if` blocks after code",
template: 'div\n\tdo const x = 5;\n\tif x === 5\n\t\tdata-pass: ""',
expected: { output: "<div data-pass></div>" },
fails: true,
},
];
const passes = test => {
let output;
let error;
let errorMessage;
const options = {
load: name =>
parser.parse(test.include[name], options),
...test.options,
};
try {
output = razorleaf.compile(test.template, options)(test.data);
} catch (e) {
const m = PARSER_ERROR_MESSAGE.exec(e.message);
error = e;
errorMessage = m ? m[1] : e.message;
}
if (errorMessage === test.expected.error && output === test.expected.output) {
console.log("\x1b[32m✔\x1b[0m \x1b[1m%s\x1b[0m passed", test.name);
return !test.fails;
}
console.log("\x1b[%s\x1b[0m \x1b[1m%s\x1b[0m failed", test.fails ? "33m-" : "31m✘", test.name);
console.log(error ? error.stack.replace(/^/gm, " ") : " Output: " + output);
return test.fails;
};
process.exitCode = !tests.every(passes);