-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
426 lines (360 loc) · 16.1 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import {describe, it} from "node:test";
import assert from "node:assert";
import m from "./index.js";
describe("templating", () => {
describe("{{ xyz }}", () => {
it("should replace variables", () => {
const data = {name: "Bob"};
assert.equal(m("Hello {{ name }}!", data), "Hello Bob!");
});
it("should escape variables", () => {
const data = {tag: "<div>"};
assert.equal(m("Tag is {{ tag }}", data), "Tag is <div>");
});
it("should allow fallback values", () => {
assert.equal(m(`Hello {{name || "world"}}!`, {}), "Hello world!");
});
});
describe("{{! xyz }}", () => {
it("should not escape variables", () => {
const data = {tag: "<div>"};
assert.equal(m("Tag is {{! tag }}", data), "Tag is <div>");
});
it("should allow fallback values", () => {
assert.equal(m(`Hello {{!name || !"<world>"}}!`, {}), "Hello <world>!");
});
});
describe("{{# xyz }}", () => {
it("should be displayed when truthy", () => {
const data = {visible: true};
assert.equal(m("{{# visible}}Yes!{{/ visible}}", data), "Yes!");
});
it("should not be visible when falsy", () => {
const data = {visible: false};
assert.equal(m("{{# visible}}Yes!{{/ visible}}", data), "");
});
it("should not be visible when null", () => {
const data = {name: null};
assert.equal(m("{{#name}}Hello {{name}}{{/name}}", data), "");
});
it("should not be visible when undefined", () => {
const data = {};
assert.equal(m("{{#name}}Hello {{name}}{{/name}}", data), "");
});
it("should parse variables inside", () => {
const data = {visible: true, name: "Bob"};
assert.equal(m("{{#visible}}Hello {{name}}!{{/visible}}", data), "Hello Bob!");
});
it("should support nested conditionals", () => {
const data = {visible1: true, visible2: true, name: "Bob"};
assert.equal(m("{{#visible1}}{{#visible2}}{{name}}{{/visible2}}{{/visible1}}", data), "Bob");
});
it("should iterate simple arrays", () => {
const data = {
items: ["1", "2", "3"],
};
assert.equal(m("{{#items}}{{.}}{{/items}}", data), "123");
});
it("should iterate arrays of objects", () => {
const data = {
items: [{name: "Susan"},{name: "Bob"}],
name: "Phil",
};
assert.equal(m("{{#items}}{{name}}-{{/items}}", data), "Susan-Bob-");
});
it("should not iterate over an empty array", () => {
const data = {
items: [],
};
assert.equal(m("List of items: {{#items}}{{.}},{{/items}}", data), "List of items: ");
});
it("should throw an error for unmatched end of section", () => {
const data = {name: "Bob"};
try {
m("{{#name}}Hello {{name}}!{{/foo}}", data);
}
catch (error) {
assert.equal(error.message, "Unmatched section end: {{/foo}}");
}
});
});
describe("{{^ xyz }}", () => {
it("should be rendered if variable is falsy", () => {
const data = {visible: false};
assert.equal(m("{{^visible}}Yeah!{{/visible}}", data), "Yeah!");
});
it("should not be rendered if variable is truthy", () => {
const data = {visible: true};
assert.equal(m("{{^visible}}Yeah!{{/visible}}", data), "");
});
});
describe("{{> xyz }}", () => {
it("should render provided partials", () => {
const partials = {
foo: "Hello World!",
};
assert.equal(m("Message: '{{> foo}}'", {}, {partials}), "Message: 'Hello World!'");
});
it("should forward context to partials", () => {
const data = {name: "Bob"};
const partials = {
foo: "Hello {{name}}!",
};
assert.equal(m("Message: '{{> foo}}'", data, {partials}), "Message: 'Hello Bob!'");
});
it("should ignore partial section if partial is not defined", () => {
assert.equal(m("Hello {{> foo}}", {}, {}), "Hello ");
});
it("should allow to provide custom context to partial", () => {
const data = {
author: {
name: "Bob",
},
};
const partials = {
foo: "Hello {{name}}!",
};
assert.equal(m("Message: '{{> foo author}}'", data, {partials}), "Message: 'Hello Bob!'");
});
it("should allow to provide keyword arguments to partials", () => {
const data = {
name: "Bob"
};
const partials = {
foo: "Hello {{userName}}!",
};
assert.equal(m("{{>foo userName=name}}", data, {partials}), "Hello Bob!");
});
});
describe("{{< xyz}}", () => {
const template = `{{<foo}}Hello {{name}}!{{/foo}}{{>foo name="Bob"}}`;
it("should allow to define inline partials", () => {
assert.equal(m(template, {}), "Hello Bob!");
});
it("should not overwrite user partials", () => {
const partials = {
foo: "Hola {{name}}!",
};
assert.equal(m(template, {}, {partials}), "Hola Bob!");
});
});
describe("{{>> xyz}}", () => {
it("should allow providing a block to the partial", () => {
const partials = {
foo: "Hello {{@content}}!",
};
assert.equal(m("{{>>foo}}Bob{{/foo}}", {}, {partials}), "Hello Bob!");
});
});
describe("{{#each }}", () => {
it("should do nothing if value is not an array or object", () => {
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: null}), "xx");
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: []}), "xx");
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: {}}), "xx");
assert.equal(m("x{{#each values}}{{.}}{{/each}}x", {values: "aa"}), "xx");
});
it("should iterate over an array of items", () => {
assert.equal(m("{{#each values}}{{.}}{{/each}}", {values: ["a", "b"]}), "ab");
assert.equal(m("{{#each values}}{{@index}}:{{.}},{{/each}}", {values: ["a", "b"]}), "0:a,1:b,");
});
it("should iterate over an object", () => {
assert.equal(m("{{#each values}}{{.}},{{/each}}", {values: {foo: "bar"}}), "bar,");
assert.equal(m("{{#each values}}{{@key}}:{{@value}},{{/each}}", {values: {foo: "bar"}}), "foo:bar,");
});
it("should register @first variable", () => {
assert.equal(m("{{#each values}}{{.}}:{{@first}};{{/each}}", {values: [0, 1, 2]}), "0:true;1:false;2:false;");
});
it("should register @last variable", () => {
assert.equal(m("{{#each values}}{{.}}:{{@last}};{{/each}}", {values: [0, 1, 2]}), "0:false;1:false;2:true;");
});
it("should allow to limit the number of iterations using the limit option", () => {
assert.equal(m("{{#each values limit=2}}{{.}}{{/each}}", {values: [0, 1, 2]}), "01");
});
it("should allow to change the start index using the skip option", () => {
assert.equal(m("{{#each values skip=2}}{{.}}{{/each}}", {values: [0, 1, 2, 3]}), "23");
});
it("should allow to change the start index and limit the number of iterations", () => {
assert.equal(m("{{#each values skip=1 limit=2}}{{.}}{{/each}}", {values: [0, 1, 2, 3]}), "12");
});
});
describe("{{#if }}", () => {
it("should include content if value is true", () => {
assert.equal(m("_{{#if value}}Yes!{{/if}}_", {value: true}), "_Yes!_");
});
it("should not include content if value is false", () => {
assert.equal(m("_{{#if value}}Yes!{{/if}}_", {value: false}), "__");
});
it("should read value from runtime variables", () => {
assert.equal(m("{{#each items}}{{#if @first}}.{{/if}}{{.}}{{/each}}", {items: [0, 1, 2]}), ".012");
});
});
describe("{{#unless }}", () => {
it("should not include content if value is true", () => {
assert.equal(m("_{{#unless value}}Yes!{{/unless}}_", {value: true}), "__");
});
it("should include content if value is false", () => {
assert.equal(m("_{{#unless value}}Yes!{{/unless}}_", {value: false}), "_Yes!_");
});
it("should read value from runtime variables", () => {
assert.equal(m("{{#each items}}{{.}}{{#unless @last}},{{/unless}}{{/each}}", {items: [0, 1, 2]}), "0,1,2");
});
});
describe("{{#eq }}", () => {
it("should render block if two values are equal", () => {
assert.equal(m(`{{#eq value "a"}}yes!{{/eq}}`, {value: "a"}), "yes!");
assert.equal(m(`{{#eq value true}}yes!{{/eq}}`, {value: true}), "yes!");
assert.equal(m(`{{#eq value 0}}yes!{{/eq}}`, {value: 0}), "yes!");
});
it("should not render block if two values are not equal", () => {
assert.equal(m(`{{#eq value "a"}}yes!{{/eq}}`, {value: "b"}), "");
assert.equal(m(`{{#eq value true}}yes!{{/eq}}`, {value: false}), "");
assert.equal(m(`{{#eq value 0}}yes!{{/eq}}`, {value: 1}), "");
});
});
describe("{{#ne }}", () => {
it("should not render block if two values are equal", () => {
assert.equal(m(`{{#ne value "a"}}yes!{{/ne}}`, {value: "a"}), "");
assert.equal(m(`{{#ne value true}}yes!{{/ne}}`, {value: true}), "");
assert.equal(m(`{{#ne value 0}}yes!{{/ne}}`, {value: 0}), "");
});
it("should render block if two values are not equal", () => {
assert.equal(m(`{{#ne value "a"}}yes!{{/ne}}`, {value: "b"}), "yes!");
assert.equal(m(`{{#ne value true}}yes!{{/ne}}`, {value: false}), "yes!");
assert.equal(m(`{{#ne value 0}}yes!{{/ne}}`, {value: 1}), "yes!");
});
});
describe("{{#with }}", () => {
it("should change the context of the block", () => {
const data = {
value: "no",
subdata: {
value: "yes",
},
};
assert.equal(m("result: {{#with subdata}}{{value}}{{/with}}", data), "result: yes");
});
});
describe("{{#escape}}", () => {
it("should escape provided content", () => {
assert.equal(m("{{#escape}}<b>Hello</b>{{/escape}}"), "<b>Hello</b>");
});
});
describe("{{#customHelper }}", () => {
it("should allow to execute a simple custom helper", () => {
const options = {
helpers: {
hello: params => `Hello ${params.args[0]}!!`,
},
};
assert.equal(m("{{#hello name}}{{/hello}}", {name: "Bob"}, options), "Hello Bob!!");
});
it("should allow to provide multiple values to custom helper", () => {
const options = {
helpers: {
concat: params => params.args.join(" "),
},
};
assert.equal(m("{{#concat a b}}{{/concat}}!", {a: "hello", b: "world"}, options), "hello world!");
});
it("should allow to provide fixed arguments values", () => {
const options = {
helpers: {
customEqual: params => params.args[0] === params.args[1] ? params.fn(params.context) : "",
},
};
assert.equal(m(`{{#customEqual value "yes"}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "Yes!!");
assert.equal(m(`{{#customEqual value "no"}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "");
assert.equal(m(`{{#customEqual value false}}Yes!!{{/customEqual}}`, {value: "yes"}, options), "");
});
it("should allow to provide keyword arguments", () => {
const options = {
helpers: {
concat: params => params.args.join(params.opt.delimiter || " "),
},
};
assert.equal(m(`{{#concat a b delimiter=","}}{{/concat}}`, {a: "hello", b: "world"}, options), "hello,world");
});
});
describe("{{@root}}", () => {
it("should reference the global context", () => {
assert.equal(m("{{#each values}}{{@root.key}}{{/each}}", {values: ["a", "b"], key: "c"}), "cc");
});
});
describe("{{@index}}", () => {
it("should reference current index in the array", () => {
assert.equal(m("{{#each values}}{{@index}}{{/each}}", {values: ["a", "b", "c"]}), "012");
});
});
describe("{{@key}}", () => {
it("should reference current key when looping throug an object", () => {
assert.equal(m("{{#each values}}{{@key}},{{/each}}", {values: {foo: 1, bar: 2}}), "foo,bar,");
});
});
describe("{{@value}}", () => {
it("should reference current value when looping throug an object", () => {
assert.equal(m("{{#each values}}{{@value}},{{/each}}", {values: {foo: 1, bar: 2}}), "1,2,");
});
});
describe("{{=function }}", () => {
const options = {
functions: {
toUpperCase: params => params.args[0].toUpperCase(),
concat: params => params.args.join(params.opt.delimiter || " "),
},
};
it("should allow executing a function to return a value", () => {
assert.equal(m("{{=toUpperCase value}}!!", {value: "Bob"}, options), "BOB!!");
});
it("should support multiple arguments", () => {
assert.equal(m("{{=concat a b}}", {a: "Hello", b: "World"}, options), "Hello World");
});
it("should render nothing if no function is provided", () => {
assert.equal(m("Result: {{=noop value}}", {value: "a"}), "Result: ");
});
it("should allow to provide fixed arguments values", () => {
assert.equal(m(`{{=concat "Hello" "World"}}!`, {}, options), "Hello World!");
});
it("should allow to execute funcions inside helpers blocks", () => {
assert.equal(m(`{{#each names}}{{=toUpperCase .}}, {{/each}}`, {names: ["bob", "susan"]}, options), "BOB, SUSAN, ");
});
it("should support keywods in function arguments", () => {
assert.equal(m(`{{=concat a b delimiter=","}}`, {a: "Hello", b: "World"}, options), "Hello,World");
});
it("should support context variables as keyword arguments", () => {
const data = {
name: "Bob",
surname: "Doe",
};
const options = {
functions: {
sayWelcome: ({args, opt}) => {
return `Welcome, ${[args[0], opt.surname || ""].filter(Boolean).join(" ")}`;
},
},
};
assert.equal(m("{{=sayWelcome name surname=surname}}", data, options), "Welcome, Bob Doe");
});
});
});
describe("utils", () => {
describe("frontmatter", () => {
const frontmatter = lines => m.frontmatter(lines.join("\n"));
it("should return empty data if no frontmatter is present", () => {
const result = frontmatter([
`Hello world`,
]);
assert.equal(result.body, "Hello world");
assert.equal(Object.keys(result.data).length, 0);
});
it("should return parsed frontmatter", () => {
const result = frontmatter([
`---`,
`DATA`,
`---`,
`Hello world`,
]);
assert.equal(result.body, "Hello world");
assert.equal(result.data, "DATA");
});
});
});