-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
TemplateRenderLiquidTest.js
1157 lines (986 loc) · 36.8 KB
/
TemplateRenderLiquidTest.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import test from "ava";
import { Liquid, Drop } from "liquidjs";
import Eleventy from "../src/Eleventy.js";
import TemplateRender from "../src/TemplateRender.js";
import EleventyExtensionMap from "../src/EleventyExtensionMap.js";
import { getTemplateConfigInstance } from "./_testHelpers.js";
async function getNewTemplateRender(name, inputDir, userConfig = {}) {
let eleventyConfig = await getTemplateConfigInstance({
dir: {
input: inputDir
}
}, null, userConfig);
let tr = new TemplateRender(name, eleventyConfig);
tr.extensionMap = new EleventyExtensionMap(eleventyConfig);
tr.extensionMap.setFormats([]);
await tr.init();
return tr;
}
async function getPromise(resolveTo) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(resolveTo);
});
});
}
// Liquid
test("Liquid", async (t) => {
let tr = await getNewTemplateRender("liquid");
t.is(tr.getEngineName(), "liquid");
});
test("Liquid Render Addition", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{ number | plus: 1 }}</p>");
t.is(await fn({ number: 1 }), "<p>2</p>");
});
test("Liquid Render Raw", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{% raw %}{{name}}{% endraw %}</p>");
t.is(await fn({ name: "tim" }), "<p>{{name}}</p>");
});
test("Liquid Render Raw Multiline", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate(
`<p>{% raw %}
{{name}}
{% endraw %}</p>`
);
t.is(
await fn({ name: "tim" }),
`<p>
{{name}}
</p>`
);
});
test("Liquid Render (with Helper)", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{name | capitalize}}</p>");
t.is(await fn({ name: "tim" }), "<p>Tim</p>");
});
test("Liquid Render Include", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let fn = await tr2.getCompiledTemplate("<p>{% include included %}</p>");
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Relative Include (dynamicPartials off)", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
// Important note: when inputPath is set to `liquid`, this *only* uses _includes relative paths in Liquid->compile
let fn = await tr2.getCompiledTemplate("<p>{% include ./included %}</p>");
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Relative Include (dynamicPartials on)", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/");
// Important note: when inputPath is set to `liquid`, this *only* uses _includes relative paths in Liquid->compile
let fn = await tr2.getCompiledTemplate("<p>{% include './included' %}</p>");
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Relative (current dir) Include", async (t) => {
let tr = await getNewTemplateRender(
"./test/stubs/relative-liquid/does_not_exist_and_thats_ok.liquid",
"./test/stubs/",
{
liquidOptions: {
dynamicPartials: false,
},
}
);
let fn = await tr.getCompiledTemplate("<p>{% include ./dir/included %}</p>");
t.is(await fn(), "<p>TIME IS RELATIVE.</p>");
});
test("Liquid Render Relative (parent dir) Include", async (t) => {
let tr = await getNewTemplateRender(
"./test/stubs/relative-liquid/dir/does_not_exist_and_thats_ok.liquid",
"./test/stubs/",
{
liquidOptions: {
dynamicPartials: false,
},
}
);
let fn = await tr.getCompiledTemplate("<p>{% include ../dir/included %}</p>");
t.is(await fn(), "<p>TIME IS RELATIVE.</p>");
});
test("Liquid Render Relative (relative include should ignore _includes dir) Include", async (t) => {
let tr = await getNewTemplateRender(
"./test/stubs/does_not_exist_and_thats_ok.liquid",
"./test/stubs/",
{}
);
let fn = await tr.getCompiledTemplate(`<p>{% include './included' %}</p>`);
t.is(await fn(), "<p>This is not in the includes dir.</p>");
});
test("Liquid Render Include with Liquid Suffix", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let fn = await tr2.getCompiledTemplate("<p>{% include included.liquid %}</p>");
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include with HTML Suffix", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let fn = await tr2.getCompiledTemplate("<p>{% include included.html %}</p>");
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include with HTML Suffix and Data Pass in", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let fn = await tr2.getCompiledTemplate("{% include included-data.html, myVariable: 'myValue' %}");
t.is((await fn()).trim(), "This is an include. myValue");
});
test("Liquid Custom Filter", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addFilter("prefixWithZach", function (val) {
return "Zach" + val;
});
t.is(await tr._testRender("{{ 'test' | prefixWithZach }}", {}), "Zachtest");
});
test("Liquid Async Filter", async (t) => {
let tr = await getNewTemplateRender("liquid", "test/stubs");
tr.engine.addFilter("myAsyncFilter", async function (value) {
return new Promise((resolve, reject) => {
setTimeout(function () {
resolve(`HI${value}`);
}, 100);
});
});
let fn = await tr.getCompiledTemplate("{{ 'test' | myAsyncFilter }}");
t.is((await fn()).trim(), "HItest");
});
test("Issue 3206: Strict variables and custom filters in includes", async (t) => {
let tr = await getNewTemplateRender("liquid", "test/stubs", {
liquidOptions: {
strictVariables: true
}
});
tr.engine.addFilter("makeItFoo", function () {
return "foo";
});
let fn = await tr.getCompiledTemplate(`<p>{% render "custom-filter", name: "Zach" %}</p>`);
t.is((await fn()), "<p>foo</p>");
});
test("Liquid Custom Tag prefixWithZach", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addTag("prefixWithZach", function (liquidEngine) {
return {
parse: function (tagToken, remainTokens) {
this.str = tagToken.args; // name
},
render: function (ctx, hash) {
var str = liquidEngine.evalValueSync(this.str, ctx.environments); // 'alice'
return Promise.resolve("Zach" + str); // 'Alice'
},
};
});
t.is(await tr._testRender("{% prefixWithZach name %}", { name: "test" }), "Zachtest");
});
test("Liquid Custom Tag postfixWithZach", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addTag("postfixWithZach", function (liquidEngine) {
return {
parse: function (tagToken, remainTokens) {
this.str = tagToken.args;
},
render: async function (ctx, hash) {
var str = await liquidEngine.evalValue(this.str, ctx.environments);
return Promise.resolve(str + "Zach");
},
};
});
t.is(await tr._testRender("{% postfixWithZach name %}", { name: "test" }), "testZach");
});
test("Liquid Custom Tag Unquoted String", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addTag("testUnquotedStringTag", function (liquidEngine) {
return {
parse: function (tagToken, remainTokens) {
this.str = tagToken.args;
},
render: function (scope, hash) {
return Promise.resolve(this.str + "Zach");
},
};
});
t.is(
await tr._testRender("{% testUnquotedStringTag _posts/2016-07-26-name-of-post.md %}", {
name: "test",
}),
"_posts/2016-07-26-name-of-post.mdZach"
);
});
test("Liquid addTag errors", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
t.throws(() => {
tr.engine.addTag("badSecondArgument", {});
});
});
test("Liquid addTags", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addCustomTags({
postfixWithZach: function (liquidEngine) {
return {
parse: function (tagToken, remainTokens) {
this.str = tagToken.args;
},
render: async function (ctx, hash) {
var str = await liquidEngine.evalValue(this.str, ctx.environments);
return Promise.resolve(str + "Zach");
},
};
},
});
t.is(await tr._testRender("{% postfixWithZach name %}", { name: "test" }), "testZach");
});
test("Liquid Shortcode", async (t) => {
t.plan(3);
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", function (str) {
// Data in context
t.is(this.page.url, "/hi/");
t.not(this.name, "test");
return str + "Zach";
});
t.is(
await tr._testRender("{% postfixWithZach name %}", {
name: "test",
page: {
url: "/hi/",
},
}),
"testZach"
);
});
test("Liquid Shortcode returns promise", async (t) => {
t.plan(2);
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", function (str) {
// Data in context
t.is(this.page.url, "/hi/");
return new Promise(function (resolve) {
setTimeout(function () {
resolve(str + "Zach");
});
});
});
t.is(
await tr._testRender("{% postfixWithZach name %}", {
name: "test",
page: {
url: "/hi/",
},
}),
"testZach"
);
});
test("Liquid Shortcode returns promise (await inside)", async (t) => {
t.plan(2);
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", async function (str) {
// Data in context
t.is(this.page.url, "/hi/");
return await getPromise(str + "Zach");
});
t.is(
await tr._testRender("{% postfixWithZach name %}", {
name: "test",
page: {
url: "/hi/",
},
}),
"testZach"
);
});
test("Liquid Shortcode returns promise (no await inside)", async (t) => {
t.plan(2);
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", async function (str) {
// Data in context
t.is(this.page.url, "/hi/");
return getPromise(str + "Zach");
});
t.is(
await tr._testRender("{% postfixWithZach name %}", {
name: "test",
page: {
url: "/hi/",
},
}),
"testZach"
);
});
test("Liquid Shortcode Safe Output", async (t) => {
t.plan(2);
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", function (str) {
// Data in context
t.is(this.page.url, "/hi/");
return `<span>${str}</span>`;
});
t.is(
await tr._testRender("{% postfixWithZach name %}", {
name: "test",
page: {
url: "/hi/",
},
}),
"<span>test</span>"
);
});
test("Liquid Paired Shortcode", async (t) => {
t.plan(2);
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addPairedShortcode("postfixWithZach", function (content, str) {
// Data in context
t.is(this.page.url, "/hi/");
return str + content + "Zach";
});
t.is(
await tr._testRender("{% postfixWithZach name %}Content{% endpostfixWithZach %}", {
name: "test",
page: {
url: "/hi/",
},
}),
"testContentZach"
);
});
test("Liquid Async Paired Shortcode", async (t) => {
t.plan(2);
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addPairedShortcode("postfixWithZach", function (content, str) {
// Data in context
t.is(this.page.url, "/hi/");
return new Promise(function (resolve) {
setTimeout(function () {
resolve(str + content + "Zach");
});
});
});
t.is(
await tr._testRender("{% postfixWithZach name %}Content{% endpostfixWithZach %}", {
name: "test",
page: {
url: "/hi/",
},
}),
"testContentZach"
);
});
test("Liquid Render Include Subfolder", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let fn = await tr.getCompiledTemplate(`<p>{% include subfolder/included.liquid %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder HTML", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let fn = await tr.getCompiledTemplate(`<p>{% include subfolder/included.html %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder No file extension", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let fn = await tr.getCompiledTemplate(`<p>{% include subfolder/included %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
// Related to https://github.com/harttle/liquidjs/issues/61
// Note that we swapped the dynamicPartials default in Eleventy 1.0 from false to true
test("Liquid Render Include Subfolder Single quotes", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include 'subfolder/included.liquid' %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Double quotes", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include "subfolder/included.liquid" %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Single quotes HTML", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include 'subfolder/included.html' %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Double quotes HTML", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include "subfolder/included.html" %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Single quotes No file extension", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include 'subfolder/included' %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Double quotes No file extension", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include "subfolder/included" %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
/* End tests related to dynamicPartials */
test("Liquid Options Overrides", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
dynamicPartials: false,
},
});
let options = tr.engine.getLiquidOptions();
t.is(options.dynamicPartials, false);
});
test("Liquid Render Include Subfolder Single quotes no extension dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include 'subfolder/included' %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Single quotes (relative include current dir) dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender(
"./test/stubs/does_not_exist_and_thats_ok.liquid",
"./test/stubs/",
{}
);
let fn = await tr.getCompiledTemplate(`<p>{% include './relative-liquid/dir/included' %}</p>`);
t.is(await fn(), "<p>TIME IS RELATIVE.</p>");
});
test("Liquid Render Include Subfolder Single quotes (relative include parent dir) dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender(
"./test/stubs/subfolder/does_not_exist_and_thats_ok.liquid",
"./test/stubs/",
{}
);
let fn = await tr.getCompiledTemplate(`<p>{% include '../relative-liquid/dir/included' %}</p>`);
t.is(await fn(), "<p>TIME IS RELATIVE.</p>");
});
test("Liquid Render Include Subfolder Double quotes no extension dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include "subfolder/included" %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Single quotes dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include 'subfolder/included.liquid' %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Double quotes dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include "subfolder/included.liquid" %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include 'subfolder/included.html' %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(`<p>{% include "subfolder/included.html" %}</p>`);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Single quotes HTML dynamicPartials true, data passed in", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(
`<p>{% include 'subfolder/included.html', myVariable: 'myValue' %}</p>`
);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render Include Subfolder Double quotes HTML dynamicPartials true, data passed in", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr.getCompiledTemplate(
`<p>{% include "subfolder/included.html", myVariable: "myValue" %}</p>`
);
t.is(await fn(), "<p>This is an include.</p>");
});
test("Liquid Render: with Library Override", async (t) => {
const tr = await getNewTemplateRender("liquid");
tr.engine.setLibrary(new Liquid());
const fn = await tr.getCompiledTemplate("<p>{{name | capitalize}}</p>");
t.is(await fn({ name: "tim" }), "<p>Tim</p>");
});
test("Liquid Paired Shortcode with Tag Inside", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addPairedShortcode("postfixWithZach", function (content, str) {
return str + content + "Zach";
});
t.is(
await tr._testRender(
"{% postfixWithZach name %}Content{% if tester %}If{% endif %}{% endpostfixWithZach %}",
{ name: "test", tester: true }
),
"testContentIfZach"
);
});
test("Liquid Nested Paired Shortcode", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addPairedShortcode("postfixWithZach", function (content, str) {
return str + content + "Zach";
});
t.is(
await tr._testRender(
"{% postfixWithZach name %}Content{% postfixWithZach name2 %}Content{% endpostfixWithZach %}{% endpostfixWithZach %}",
{ name: "test", name2: "test2" }
),
"testContenttest2ContentZachZach"
);
});
test("Liquid Shortcode Multiple Args", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", function (str, str2) {
return str + str2 + "Zach";
});
t.is(
await tr._testRender("{% postfixWithZach name other %}", {
name: "test",
other: "howdy",
}),
"testhowdyZach"
);
});
test("Liquid Include Scope Leak", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
// This is by design, `include` assigns value to its parent scope,
// use `{% render %}` for separated, clean scope
// see: https://github.com/harttle/liquidjs/issues/404#issuecomment-955660149
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr2.getCompiledTemplate("<p>{% include 'scopeleak' %}{{ test }}</p>");
t.is(await fn({ test: 1 }), "<p>22</p>");
});
test("Liquid Render Scope Leak", async (t) => {
let tr1 = await getNewTemplateRender("liquid", "./test/stubs/");
t.is(tr1.getEngineName(), "liquid");
let tr2 = await getNewTemplateRender("liquid", "./test/stubs/");
let fn = await tr2.getCompiledTemplate("<p>{% render 'scopeleak' %}{{ test }}</p>");
t.is(await fn({ test: 1 }), "<p>21</p>");
});
// Note: this strictFilters default changed in 1.0 from false to true
test("Liquid Missing Filter Issue #183 (no strictFilters)", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/", {
liquidOptions: {
strictFilters: false,
},
});
try {
await tr._testRender("{{ 'test' | prefixWithZach }}", {});
t.pass("Did not error.");
} catch (e) {
t.fail("Threw an error.");
}
});
// Note: this strictFilters default changed in 1.0 from false to true
test("Liquid Missing Filter Issue #183", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
try {
await tr._testRender("{{ 'test' | prefixWithZach }}", {});
t.fail("Did not error.");
} catch (e) {
t.pass("Threw an error.");
}
});
test("Issue 258: Liquid Render Date", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{ myDate }}</p>");
let dateStr = await fn({ myDate: new Date(Date.UTC(2016, 0, 1, 0, 0, 0)) });
t.is(dateStr.slice(0, 3), "<p>");
t.is(dateStr.slice(-4), "</p>");
t.not(dateStr.slice(2, 1), '"');
});
test("Issue 347: Liquid addTags with space in argument", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addCustomTags({
issue347CustomTag: function (liquidEngine) {
return {
parse: function (tagToken, remainTokens) {
this.str = tagToken.args;
},
render: async function (scope, hash) {
var str = await liquidEngine.evalValue(this.str, scope);
return Promise.resolve(str + "Zach");
},
};
},
});
t.is(
await tr._testRender("{% issue347CustomTag 'te st' %}", {
name: "slkdjflksdjf",
}),
"te stZach"
);
});
test("Issue 347: Liquid Shortcode, string argument", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347", function (str) {
return str + "Zach";
});
t.is(await tr._testRender("{% issue347 'test' %}", { name: "alkdsjfkslja" }), "testZach");
});
test("Issue 347: Liquid Shortcode string argument with space, double quotes", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347b", function (str) {
return str + "Zach";
});
t.is(
await tr._testRender('{% issue347b "test 2" "test 3" %}', {
name: "alkdsjfkslja",
}),
"test 2Zach"
);
});
test("Issue 347: Liquid Shortcode string argument with space, single quotes", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347", function (str) {
return str + "Zach";
});
t.is(await tr._testRender("{% issue347 'test 2' %}", { name: "alkdsjfkslja" }), "test 2Zach");
});
test("Issue 347: Liquid Shortcode string argument with space, combination of quotes", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347", function (str, str2) {
return str + str2 + "Zach";
});
t.is(
await tr._testRender("{% issue347 'test 2' \"test 3\" %}", {
name: "alkdsjfkslja",
}),
"test 2test 3Zach"
);
});
test("Issue 347: Liquid Shortcode multiple arguments, comma separated", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347", function (str, str2) {
return str + str2 + "Zach";
});
t.is(
await tr._testRender("{% issue347 'test 2', \"test 3\" %}", {
name: "alkdsjfkslja",
}),
"test 2test 3Zach"
);
});
test("Issue 347: Liquid Shortcode multiple arguments, comma separated, one is an integer", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347", function (str, str2) {
return str + str2 + "Zach";
});
t.is(
await tr._testRender("{% issue347 'test 2', 3 %}", {
name: "alkdsjfkslja",
}),
"test 23Zach"
);
});
test("Issue 347: Liquid Shortcode multiple arguments, comma separated, one is a float", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347", function (str, str2) {
return str + str2 + "Zach";
});
t.is(
await tr._testRender("{% issue347 'test 2', 3.23 %}", {
name: "alkdsjfkslja",
}),
"test 23.23Zach"
);
});
test("Issue 347: Liquid Shortcode boolean argument", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue347", function (bool) {
return bool ? "Zach" : "Not Zach";
});
t.is(await tr._testRender("{% issue347 true %}", { name: "alkdsjfkslja" }), "Zach");
t.is(await tr._testRender("{% issue347 false %}", { name: "alkdsjfkslja" }), "Not Zach");
});
test("Issue 347: Liquid Paired Shortcode with Spaces", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addPairedShortcode("postfixWithZach", function (content, str1, num, str2) {
return str1 + num + str2 + content + "Zach";
});
t.is(
await tr._testRender(
"{% postfixWithZach 'My Name', 1234, \"Other\" %}Content{% endpostfixWithZach %}",
{ name: "test" }
),
"My Name1234OtherContentZach"
);
});
test("Liquid Render with dash variable Issue #567", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{ my-global-name }}</p>");
t.is(await fn({ "my-global-name": "Zach" }), "<p>Zach</p>");
});
test("Issue 600: Liquid Shortcode argument page.url", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue600", function (str) {
return str + "Zach";
});
t.is(
await tr._testRender("{% issue600 page.url %}", {
page: { url: "alkdsjfkslja" },
}),
"alkdsjfksljaZach"
);
});
test("Issue 600: Liquid Shortcode argument with dashes", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue600b", function (str) {
return str + "Zach";
});
t.is(
await tr._testRender("{% issue600b page-url %}", {
"page-url": "alkdsjfkslja",
}),
"alkdsjfksljaZach"
);
});
test("Issue 600: Liquid Shortcode argument with underscores", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("issue600c", function (str) {
return str + "Zach";
});
t.is(
await tr._testRender("{% issue600c page_url %}", {
page_url: "alkdsjfkslja",
}),
"alkdsjfksljaZach"
);
});
test("Issue 611: Run a function", async (t) => {
// function calls in Nunjucks can be replaced by custom Drops
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
class CustomDrop extends Drop {
valueOf() {
return "alkdsjfksljaZach";
}
}
t.is(
await tr._testRender("{{ test }}", {
test: new CustomDrop(),
}),
"alkdsjfksljaZach"
);
});
test("Liquid Shortcode (with sync function, error throwing)", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", function (str) {
throw new Error("Liquid Shortcode (with sync function, error throwing)");
});
let error = await t.throwsAsync(async () => {
await tr._testRender("{% postfixWithZach name %}", { name: "test" });
});
t.true(error.message.indexOf("Liquid Shortcode (with sync function, error throwing)") > -1);
});
test("Liquid Shortcode (with async function, error throwing)", async (t) => {
let tr = await getNewTemplateRender("liquid", "./test/stubs/");
tr.engine.addShortcode("postfixWithZach", async function (str) {
throw new Error("Liquid Shortcode (with async function, error throwing)");
});
let error = await t.throwsAsync(async () => {
await tr._testRender("{% postfixWithZach name %}", { name: "test" });
});
t.true(error.message.indexOf("Liquid Shortcode (with async function, error throwing)") > -1);
});
test("Liquid Render a false #1069", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("{{ falseValue }}");
t.is(await fn({ falseValue: false }), "false");
});
test("Liquid Render Square Brackets #680 dash single quotes", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{ test['hey-a'] }}</p>");
t.is(await fn({ test: { "hey-a": 1 } }), "<p>1</p>");
});
test("Liquid Render Square Brackets #680 dash single quotes spaces", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{ test[ 'hey-a' ] }}</p>");
t.is(await fn({ test: { "hey-a": 1 } }), "<p>1</p>");
});
test("Liquid Render Square Brackets #680 dash double quotes", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate('<p>{{ test["hey-a"] }}</p>');
t.is(await fn({ test: { "hey-a": 1 } }), "<p>1</p>");
});
test("Liquid Render Square Brackets #680 dash double quotes spaces", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate('<p>{{ test[ "hey-a" ] }}</p>');
t.is(await fn({ test: { "hey-a": 1 } }), "<p>1</p>");
});
test("Liquid Render Square Brackets #680 variable reference", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{ test[ref] }}</p>");
t.is(await fn({ test: { "hey-a": 1 }, ref: "hey-a" }), "<p>1</p>");
});
test("Liquid Render Square Brackets #680 variable reference array", async (t) => {
let tr = await getNewTemplateRender("liquid");
let fn = await tr.getCompiledTemplate("<p>{{ test[ref[0]] }}</p>");
t.is(await fn({ test: { "hey-a": 1 }, ref: ["hey-a"] }), "<p>1</p>");
});