forked from linkedin/dustjs
/
coreTests.js
executable file
·1889 lines (1882 loc) · 72.8 KB
/
coreTests.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
var coreTests = [
/**
* CORE TESTS
*/
{
name: "core tests",
tests: [
{
name: "streaming render",
source: "{#stream}{#delay}{.}{/delay}{/stream}",
context: (function(){
var d = 1;
return {
stream: function() {
return "asynchronous templates for the browser and node.js".split('');
},
delay: function(chunk, context, bodies) {
return chunk.map(function(chunk) {
setTimeout(function() {
chunk.render(bodies.block, context).end();
}, d++ * 15);
});
}
};
}),
expected: '',
message: "should test the stream rendering"
},
{
name: undefined,
source: "compilation_failure",
error: "Template name parameter cannot be undefined when calling dust.compile",
message: "if the name is not there compilation should be failed, unless it is called from renderSource"
},
{
name: "hello_world",
source: "Hello World!",
context: {},
expected: "Hello World!",
message: "should test basic text rendering"
},
{
name: "global_template",
source: '{#helper foo="bar" boo="boo"} {/helper}',
context: { "helper": function(chunk, context, bodies, params)
{
currentTemplateName = context.getTemplateName();
return chunk.write(currentTemplateName);
}
},
expected: "global_template",
message: "should render the template name"
},
{
name: "apps/test/foo.tl&v=0.1",
source: '{#helper foo="bar" boo="boo" template="tl/apps/test"} {/helper}',
context: { "helper": function(chunk, context, bodies, params)
{
// top of the current stack
currentTemplateName = context.getTemplateName();
return chunk.write(currentTemplateName);
}
},
expected: "apps/test/foo.tl&v=0.1",
message: "should render the template name with paths"
},
{
name: "makeBase_missing_global",
source: '{#helper}{/helper}',
context: { "helper": function(chunk, context, bodies, params)
{
var newContext = {};
return bodies.block(chunk, dust.makeBase(newContext));
}
},
expected: "",
message: "should render the helper with missing global context"
},
{
name: "reference",
source: "{?one}{one}{/one}",
context: {"one": 0 },
expected: "0",
message: "should test a basic reference"
},
{
name: "implicit array",
source: "{#names}{.}{~n}{/names}",
context: { names: ["Moe", "Larry", "Curly"] },
expected: "Moe\nLarry\nCurly\n",
message: "should test an implicit array"
},
{
name: "inline param from outer scope",
source: "{#person foo=root}{foo}: {name}, {age}{/person}",
context: { root: "Subject", person: { name: "Larry", age: 45 } },
expected: "Subject: Larry, 45",
message: "should test renaming a key"
},
{
name: "force local",
source: "{#person}{.root}: {name}, {age}{/person}",
context: { root: "Subject", person: { name: "Larry", age: 45 } },
expected: ": Larry, 45",
message: "should test force a key"
},
{
name: "filter un-escape",
source: "{safe|s}{~n}{unsafe}",
context: { safe: "<script>alert('Hello!')</script>", unsafe: "<script>alert('Goodbye!')</script>" },
expected: "<script>alert('Hello!')</script>\n<script>alert('Goodbye!')</script>",
message: "should test escaped characters"
},
{
name: "escape pragma",
source: "{%esc:s}\n {unsafe}{~n}\n {%esc:h}\n {unsafe}\n {/esc}\n{/esc}",
context: { unsafe: "<script>alert('Goodbye!')</script>" },
expected: "<script>alert('Goodbye!')</script>\n<script>alert('Goodbye!')</script>",
message: "should test escape_pragma"
},
{
name: "use . for creating a block and set params",
source: "{#. test=\"you\"}{name} {test}{/.}",
context: { name: "me"},
expected: "me you",
message: ". creating a block"
},
{
name: "functions in context",
source: "Hello {type} World!",
context: {
type: function(chunk) {
return "Sync";
}
},
expected: "Hello Sync World!",
message: "should functions in context"
},
{
name: "async functions in context",
source: "Hello {type} World!",
context: {
type: function(chunk) {
return chunk.map(function(chunk) {
dust.nextTick(function() {
chunk.end("Async");
});
});
}
},
expected: "Hello Async World!",
message: "should test functions in context"
},
{
name: "sync chunk write test",
source: "Hello {type} World!",
context: {
type: function(chunk) {
return chunk.write("Chunky");
}
},
expected: "Hello Chunky World!",
message: "should test sync chunk write"
},
{
name: "base_template",
source: "Start{~n}" +
"{+title}" +
"Base Title" +
"{/title}" +
"{~n}" +
"{+main}" +
"Base Content" +
"{/main}" +
"{~n}" +
"End",
context: {},
expected: "Start\nBase Title\nBase Content\nEnd",
message: "should test base template"
},
{
name: "child_template",
source: "{^xhr}" +
"{>base_template/}" +
"{:else}" +
"{+main/}" +
"{/xhr}" +
"{<title}" +
"Child Title" +
"{/title}" +
"{<main}" +
"Child Content" +
"{/main}",
context: {xhr: false},
expected: "Start\nChild Title\nChild Content\nEnd",
message: "should test child template"
},
{
name: "issue322",
source: 'hi{+"{name}"/}',
context: {},
expected: "hi",
message: "should setup base template for next test. hi should not be part of base block name"
},
{
name: "issue322 use base template picks up prefix chunk data",
source: '{>issue322 name="abc"/}' +
"{<abc}ABC{/abc}",
context: {},
expected: "hiABC",
message: "should use base template and honor name passed in"
},
{
name: "recursion",
source: "{name}{~n}{#kids}{>recursion:./}{/kids}",
context: {
name: '1',
kids: [
{
name: '1.1',
kids: [
{name: '1.1.1'}
]
}
]
},
expected: "1\n1.1\n1.1.1\n",
message: "should test recursion"
},
{
name: "comments",
source: "{!\n" +
" Multiline\n" +
" {#foo}{bar}{/foo}\n" +
"!}\n" +
"{!before!}Hello{!after!}",
context: {},
expected: "Hello",
message: "should test comments"
},
{
name: "context",
source: "{#list:projects}{name}{:else}No Projects!{/list}",
context: {
list: function(chunk, context, bodies) {
var items = context.current(),
len = items.length;
if (len) {
chunk.write("<ul>\n");
for (var i=0; i<len; i++) {
chunk = chunk.write("<li>")
.render(bodies.block, context.push(items[i]))
.write("</li>\n");
}
return chunk.write("</ul>");
} else if (bodies['else']) {
return chunk.render(bodies['else'], context);
}
return chunk;
},
projects: [
{name: "Mayhem"},
{name: "Flash"},
{name: "Thunder"}
]
},
expected: "<ul>\n" +
"<li>Mayhem</li>\n" +
"<li>Flash</li>\n" +
"<li>Thunder</li>\n" +
"</ul>",
message: "should test the context"
}
]
},
/**
* TRUTH/FALSY TESTS
*/
{
name: "truth/falsy tests",
tests: [
{
name: "false value in context is treated as empty, same as undefined",
source: "{false}",
context: { "false": false },
expected: "",
message: "should test for false in the context, evaluated and prints nothing"
},
{
name: "numeric 0 value in context is treated as non empty",
source: "{zero}",
context: { "zero": 0 },
expected: "0",
message: "should test for numeric zero in the context, prints the numeric zero"
},
{
name: "empty string context is treated as empty",
source: "{emptyString}",
context: { "emptyString": "" },
expected: "",
message: "should test emptyString, prints nothing"
},
{
name: "empty string, single quoted in context is treated as empty",
source: "{emptyString}",
context: { "emptyString": '' },
expected: "",
message: "should test emptyString single quoted, prints nothing"
},
{
name: "null in the context treated as empty",
source: "{NULL}",
context: { "NULL": null },
expected: "",
message: "should test null in the context treated as empty"
},
{
name: "undefined in the context treated as empty",
source: "{UNDEFINED}",
context: {"UNDEFINED": undefined },
expected: "",
message: "should test undefined in the context treated as empty"
},
{
name: "undefined string in the context treated as non empty",
source: "{UNDEFINED}",
context: {"UNDEFINED": "undefined"},
expected: "undefined",
message: "should test string undefined in the context as non empty"
},
{
name: "null is treated as empty in exists",
source: "{?scalar}true{:else}false{/scalar}",
context: {"scalar": null},
expected: "false",
message: "should test null as empty in exists section"
},
{
name: "undefined is treated as empty in exists",
source: "{?scalar}true{:else}false{/scalar}",
context: {"scalar": undefined},
expected: "false",
message: "should test null treated as empty in exists"
},
{
name: "null is treated as truthy in not exists",
source: "{^scalar}true{:else}false{/scalar}",
context: {"scalar": null},
expected: "true",
message: "should test null as truthy in not exists"
},
{
name: "undefined is treated as truthy in not exists",
source: "{^scalar}true{:else}false{/scalar}",
context: {"scalar": undefined},
expected: "true",
message: "should test undefined as truthy in not exists"
},
{
name: "undefined is treated as empty in exists",
source: "{?scalar}true{:else}false{/scalar}",
context: {"scalar": undefined},
expected: "false",
message: "should test null treated as empty in exists"
}
]
},
/**
* SCALAR DATA TESTS
*/
{
name: "scalar data tests",
tests: [
{
name: "scalar null in a # section",
source: "{#scalar}true{:else}false{/scalar}",
context: {"scalar": null},
expected: "false",
message: "should test for a scalar null in a # section"
},
{
name: "scalar numeric 0 in a # section",
source: "{#scalar}true{:else}false{/scalar}",
context: {"scalar": 0},
expected: "true",
message: "should test for a scalar numeric 0 in a # section"
},
{
name: "scalar numeric non-zero in a # section",
source: "{#scalar}true{:else}false{/scalar}",
context: {"scalar": 42},
expected: "true",
message: "should test for a scalar numeric non-zero in a # section"
},
{
name: "scalar non empty string in a # section",
source: "{#scalar}true{:else}false{/scalar}",
context: {"scalar": 'abcde'},
expected: "true",
message: "should test for a scalar string in a # section"
},
{
name: "scalar non empty string in a # section",
source: "{#scalar}{.}{:else}false{/scalar}",
context: {"scalar": 'abcde'},
expected: "abcde",
message: "should test for a scalar string in a # section"
},
{
name: "missing scalar value",
source: "{#scalar}true{:else}false{/scalar}",
context: {"foo": 0},
expected: "false",
message: "should test a missing/undefined scalar value"
},
{
name: "scalar true value in the # section",
source: "{#scalar}true{:else}false{/scalar}",
context: {"scalar": true},
expected: "true",
message: "shoud test for scalar true value in the # section"
},
{
name: "scalar false value in the # section",
source: "{#scalar}true{:else}false{/scalar}",
context: {"scalar": false},
expected: "false",
message: "shoud test for scalar false value in the # section"
},
{
name: "scalar values true and false are supported in # nor else blocks ",
source: "{#foo}" +
"foo,{~s}" +
"{:else}" +
"not foo,{~s}" +
"{/foo}" +
"{#bar}" +
"bar!" +
"{:else}" +
"not bar!" +
"{/bar}",
context: { foo: true, bar: false },
expected: "foo, not bar!",
message:"should test scalar values true and false are supported in # nor else blocks"
}
]
},
/**
* EMPTY DATA TESTS
*/
{
name: "empty data tests",
tests: [
{
name: "empty array is treated as empty in exists",
source: "{?array}true{:else}false{/array}",
context: {"array": []},
expected: "false",
message: "empty array is treated as empty in exists"
},
{
name: "empty {} is treated as non empty in exists",
source: "{?object}true{:else}false{/object}",
context: {"object": {}},
expected: "true",
message: "empty {} is treated as non empty in exists"
},
{
name: "empty array is treated as empty in a section",
source: "{#array}true{:else}false{/array}",
context: {"array": []},
expected: "false",
message: "empty array is treated as empty in a section"
},
{
name: "empty {} is treated as non empty in a section",
source: "{#object}true{:else}false{/object}",
context: {"object": {}},
expected: "true",
message: "empty {} is treated as non empty"
},
{
name: "non-empty array in a reference",
source: "{array}",
context: {"array": ['1','2']},
expected: "1,2",
message: "non-empty array in a reference"
},
{
name: "null string in the context treated as non empty",
source: "{NULL}",
context: {"NULL": "null" },
expected: "null",
message: "should test null string in the context treated as non empty"
},
{
name: "string 0 value in context is treated as non empty",
source: "{zero}",
context: {"zero": "0" },
expected: "0",
message: "should test for string zero in the context, prints zero"
},
{
name: "empty array",
source: "{#names}{title} {name}{~n}{/names}",
context: { title: "Sir", names: [] },
expected: "",
message: "should test an empty array"
},
{
name: "empty params in helper",
source: "{#emptyParamHelper}{/emptyParamHelper}",
context: { "emptyParamHelper" : function(chunk, context, bodies, params) {
return chunk.write(params.foo);
}
},
expected: "",
message: "should output nothing, but no error should fire"
}
]
},
/**
* ARRAY/INDEX ACCESS TESTS
*/
{
name: "array/index-access tests",
tests: [
{
name: "array",
source: "{#names}{title} {name}{~n}{/names}",
context: { title: "Sir", names: [ { name: "Moe" }, { name: "Larry" }, { name: "Curly" } ] },
expected: "Sir Moe\nSir Larry\nSir Curly\n",
message: "should test an array"
},
{
name: "accessing array element by index when element value is a primitive",
source: '{do.re[0]}',
context: { "do": { "re" : ["hello!","bye!"] } },
expected: "hello!",
message: "should return a specific array element by index when element value is a primitive"
},
{
name: "accessing array by index when element value is a object",
source: '{do.re[0].mi}',
context: { "do": { "re" : [{"mi" : "hello!"},"bye!"] } },
expected: "hello!",
message: "should return a specific array element by index when element value is a object"
},
{
name: "accessing array by index when element is a nested object",
source: '{do.re[0].mi[1].fa}',
context: { "do": { "re" : [{"mi" : ["one", {"fa" : "hello!"}]},"bye!"] } },
expected: "hello!",
message: "should return a specific array element by index when element is a nested object"
},
{
name: "accessing array by index when element is list of primitives",
source: '{do[0]}',
context: { "do": ["lala", "lele"] },
expected: "lala",
message: "should return a specific array element by index when element is list of primitives"
},
{
name: "accessing array inside a loop using the current context",
source: '{#list3}{.[0].biz}{/list3}',
context: { "list3": [[ { "biz" : "123" } ], [ { "biz" : "345" } ]]},
expected: "123345",
message: "should return a specific array element using the current context"
},
{
name: "array: reference $idx in iteration on objects",
source: "{#names}({$idx}).{title} {name}{~n}{/names}",
context: { title: "Sir", names: [ { name: "Moe" }, { name: "Larry" }, { name: "Curly" } ] },
expected: "(0).Sir Moe\n(1).Sir Larry\n(2).Sir Curly\n",
message: "array: reference $idx in iteration on objects"
},
{
name: "array: reference $len in iteration on objects",
source: "{#names}Size=({$len}).{title} {name}{~n}{/names}",
context: { title: "Sir", names: [ { name: "Moe" }, { name: "Larry" }, { name: "Curly" } ] },
expected: "Size=(3).Sir Moe\nSize=(3).Sir Larry\nSize=(3).Sir Curly\n",
message: "test array: reference $len in iteration on objects"
},
{
name: "array reference $idx in iteration on simple type",
source: "{#names}({$idx}).{title} {.}{~n}{/names}",
context: { title: "Sir", names: [ "Moe", "Larry", "Curly" ] },
expected: "(0).Sir Moe\n(1).Sir Larry\n(2).Sir Curly\n",
message: "test array reference $idx in iteration on simple types"
},
{
name: "array reference $len in iteration on simple type",
source: "{#names}Size=({$len}).{title} {.}{~n}{/names}",
context: { title: "Sir", names: [ "Moe", "Larry", "Curly" ] },
expected: "Size=(3).Sir Moe\nSize=(3).Sir Larry\nSize=(3).Sir Curly\n",
message: "test array reference $len in iteration on simple types"
},
{
name: "array reference $idx/$len on empty array case",
source: "{#names}Idx={$idx} Size=({$len}).{title} {.}{~n}{/names}",
context: { title: "Sir", names: [ ] },
expected: "",
message: "test array reference $idx/$len on empty array case"
},
{
name: "array reference $idx/$len on single element case (scalar case)",
source: "{#name}Idx={$idx} Size={$len} {.}{/name}",
context: { name: "Just one name" },
expected: "Idx= Size= Just one name",
message: "test array reference $idx/$len on single element case"
},
{
name: "array reference $idx/$len {#.} section case",
source: "{#names}{#.}{$idx}{.} {/.}{/names}",
context: { names: ["Moe", "Larry", "Curly"] },
expected: "0Moe 1Larry 2Curly ",
message: "test array reference $idx/$len {#.} section case"
},
{
name: "array reference $idx/$len not changed in nested object",
source: "{#results}{#info}{$idx}{name}-{$len} {/info}{/results}",
context: {results: [{info: {name: "Steven"}},{info: {name: "Richard"}}]},
expected: "0Steven-2 1Richard-2 ",
message: "test array reference $idx/$len not changed in nested object"
},
{
name: "array reference $idx/$len nested loops",
source: "{#A}A loop:{$idx}-{$len},{#B}B loop:{$idx}-{$len}C[0]={.C[0]} {/B}A loop trailing: {$idx}-{$len}{/A}",
context: {"A": [ {"B": [ {"C": ["Ca1", "C2"]}, {"C": ["Ca2", "Ca22"]} ] }, {"B": [ {"C": ["Cb1", "C2"]}, {"C": ["Cb2", "Ca2"]} ] } ] },
expected: "A loop:0-2,B loop:0-2C[0]=Ca1 B loop:1-2C[0]=Ca2 A loop trailing: 0-2A loop:1-2,B loop:0-2C[0]=Cb1 B loop:1-2C[0]=Cb2 A loop trailing: 1-2",
message: "test array reference $idx/$len nested loops"
},
{
name: "using idx in array reference Accessing",
source: "{#list4} {name} {number[$idx]} {$idx}{/list4}",
context: { list4: [ {name:"Dog", number: [1,2,3]}, {name:"Cat", number: [4,5,6]}] },
expected: " Dog 1 0 Cat 5 1",
message: "should test the array reference access with idx"
},
{
name: "using len in array reference Accessing",
source: "{#list4} {name} {number[$len]}{/list4}",
context: { list4: [ {name:"Dog", number: [1,2,3]}, {name:"Cat", number: [4,5,6]}] },
expected: " Dog 3 Cat 6",
message: "should test the array reference access with len"
},
{
name: "using idx in array reference Accessing",
source: "{#list3}{.[$idx].biz}{/list3}",
context: { "list3": [[ { "biz" : "123" } ], [ { "biz" : "345" }, { "biz" : "456" } ]]},
expected: "123456",
message: "should test the array reference access with idx and current context"
},
{
name: "using len in array reference Accessing",
source: "{#list3}{.[$len].idx}{/list3}",
context: { "list3": [
[{"idx": "0"},
{"idx": "1"},
{"idx": "2"}],
[{"idx": "0"},
{"idx": "1"},
{"idx": "2"}]
]
},
expected: "22",
message: "should test the array reference access with len and current context"
},
{
name: "using idx in double nested array",
source: "{#test}{#.}{.}i:{$idx}l:{$len},{/.}{/test}",
context: { "test": [[ 1,2,3 ]]},
expected: "1i:0l:3,2i:1l:3,3i:2l:3,",
message: "should test double nested array and . reference: issue #340"
},
{
name: "using a nested key as a reference for array index access",
source: "{#loop.array[key.foo].sub}{.}{/loop.array[key.foo].sub}",
context: {
"loop": {
"array": {"thing": {sub: 1, sap: 2}, "thing2": "bar"}
},
"key": { "foo": "thing" }
},
expected: "1",
message: "should test using a multilevel reference as a key in array access"
}
]
},
/**
* OBJECT TESTS
*/
{
name: "object tests",
tests: [
{
name: "object",
source: "{#person}{root}: {name}, {age}{/person}",
context: { root: "Subject", person: { name: "Larry", age: 45 } },
expected: "Subject: Larry, 45",
message: "should test an object"
},
{
name: "path",
source: "{foo.bar}",
context: { foo: {bar: "Hello!"} },
expected: "Hello!",
message: "should test an object path"
}
]
},
/**
* CONDITINOAL TESTS
*/
{
name: "conditional tests",
tests: [
{
name: "conditional",
source: "{?tags}" +
"<ul>{~n}" +
"{#tags}" +
"{~s} <li>{.}</li>{~n}" +
"{/tags}" +
"</ul>" +
"{:else}" +
"No Tags!" +
"{/tags}" +
"{~n}" +
"{^likes}" +
"No Likes!" +
"{:else}" +
"<ul>{~n}" +
"{#likes}" +
"{~s} <li>{.}</li>{~n}" +
"{/likes}" +
"</ul>" +
"{/likes}",
context: { tags: [], likes: ["moe", "larry", "curly", "shemp"] },
expected: "No Tags!\n" +
"<ul>\n" +
" <li>moe</li>\n" +
" <li>larry</li>\n" +
" <li>curly</li>\n" +
" <li>shemp</li>\n" +
"</ul>",
message: "should test conditional tags"
},
{
name: "empty else block",
source: "{#foo}full foo{:else}empty foo{/foo}",
context: { foo: []},
expected: "empty foo",
message: "should test else block when array empty"
}
]
},
/**
* NESTED PATH TESTS
*/
{
name: "nested path tests",
tests: [
{
name: "Verify local mode leading dot path in local mode",
source: "{#people}{.name} is {?.age}{.age} years old.{:else}not telling us their age.{/age}{/people}",
context: {
"name": "List of people",
"age": "8 hours",
"people": [
{ "name": "Alice" },
{ "name": "Bob", "age": 42 },
]
},
expected: "Alice is not telling us their age.Bob is 42 years old.",
message: "should test the leading dot behavior in local mode"
},
{
name: "check nested ref in global does not work in local mode",
source: "{glob.globChild}",
context: { },
expected: "",
message: "Should not find glob.globChild which is in context.global"
},
{
name: "Verify leading dot path not affected in global mode",
source: "{#people}{.name} is {?.age}{.age} years old.{:else}not telling us their age.{/age}{/people}",
options: {pathScope: "global"},
context: {
"name": "List of people",
"age": "8 hours",
"people": [
{ "name": "Alice" },
{ "name": "Bob", "age": 42 },
]
},
expected: "Alice is not telling us their age.Bob is 42 years old.",
message: "should test the leading dot behavior preserved"
},
{
name: "Standard dotted path with falsey value. Issue 317",
source: "{foo.bar}",
options: {pathScope: "global"},
context: { foo: {bar: 0} },
expected: "0",
message: "should work when value at end of path is falsey"
},
{
name: "dotted path resolution up context",
source: "{#data.A.list}Aname{data.A.name}{/data.A.list}",
options: {pathScope: "global"},
context: { "data":{"A":{"name":"Al","list":[{"name": "Joe"},{"name": "Mary"}],"B":{"name":"Bob","Blist":["BB1","BB2"]}}} },
expected: "AnameAlAnameAl",
message: "should test usage of dotted path resolution up context"
},
{
name: "dotted path resolution up context 2",
source: "{#data.A.B.Blist}Aname{data.A.name}{/data.A.B.Blist}",
options: {pathScope: "global"},
context: { "data":{"A":{"name":"Al","list":[{"name": "Joe"},{"name": "Mary"}],"B":{"name":"Bob","Blist":["BB1","BB2"]}}} },
expected: "AnameAlAnameAl",
message: "should test usage of dotted path resolution up context"
},
{
name: "dotted path resolution without explicit context",
source: "{#data.A}Aname{name}{data.C.name}{/data.A}",
options: {pathScope: "global"},
context: { "data":{"A":{"name":"Al","list":[{"name": "Joe"},{"name": "Mary"}],"B":{"name":"Bob","Blist":["BB1","BB2"]}},C:{name:"cname"}} },
expected: "AnameAlcname",
message: "should test usage of dotted path resolution up context"
},
{
name: "same as previous test but with explicit context",
source: "{#data.A:B}Aname{name}{data.C.name}{/data.A}",
options: {pathScope: "global"},
context: { "data":{"A":{"name":"Al","list":[{"name": "Joe"},{"name": "Mary"}],"B":{"name":"Bob","Blist":["BB1","BB2"]}},C:{name:"cname"}} },
expected: "AnameAl",
message: "should test explicit context blocks looking further up stack"
},
{
name: "explicit context but gets value from global",
source: "{#data.A:B}Aname{name}{glob.globChild}{/data.A}",
options: {pathScope: "global"},
base: { glob: { globChild: "testGlobal"} },
context: { "data":{"A":{"name":"Al","list":[{"name": "Joe"},{"name": "Mary"}],"B":{"name":"Bob","Blist":["BB1","BB2"]}},C:{name:"cname"}} },
expected: "AnameAltestGlobal",
message: "should test access global despite explicit context"
},
{
name: "nested dotted path resolution",
source: "{#data.A.list}{#data.A.B.Blist}{.}Aname{data.A.name}{/data.A.B.Blist}{/data.A.list}",
options: {pathScope: "global"},
context: { "data":{"A":{"name":"Al","list":[{"name": "Joe"},{"name": "Mary"}],"B":{"name":"Bob","Blist":["BB1"]}}} },
expected: "BB1AnameAlBB1AnameAl",
message: "should test nested usage of dotted path resolution"
},
{
name: "check nested ref in global works in global mode",
source: "{glob.globChild}",
options: {pathScope: "global"},
base: { glob: { globChild: "testGlobal"} },
context: { },
expected: "testGlobal",
message: "Should find glob.globChild which is in context.global"
},
{
name: "dotted path resolution up context with partial match in current context",
source: "{#data}{#A}{C.name}{/A}{/data}",
options: {pathScope: "global"},
context: { "data":{ "A":{ "name":"Al", "B": "Ben", "C": { namex: "Charlie"} }, C: {name: "Charlie Sr."} } },
expected: "",
message: "should test usage of dotted path resolution up context"
},
{
name: "check nested ref not found in global if partial match",
source: "{#data}{#A}{C.name}{/A}{/data}",
options: {pathScope: "global"},
base: { C: {name: "Big Charlie"} },
context: { "data":{ "A":{ "name":"Al", "B": "Ben", "C": { namex: "Charlie"} }, C: {namey: "Charlie Sr."} } },
expected: "",
message: "Should find glob.globChild which is in context.global"
},
{
name: "method invocation",
source: "Hello {person.fullName}",
options: {pathScope: "global"},
context: {
person: {
firstName: "Peter",
lastName: "Jones",
fullName: function() {
return this.firstName + ' ' + this.lastName;
}
}
},
expected: "Hello Peter Jones",
message: "should test resolve correct 'this' when invoking method"
},
{
name: "check null values in section iteration don't break path resolution",
source: "{#nulls}{names[0].name}{/nulls}",
options: {pathScope: "global"},
context: { "nulls": [1, null, null, 2],"names": [{"name": "Moe"}, {"name": "Curly"}] },
expected: "MoeMoeMoeMoe",
message: "Should resolve path correctly"
},
{
name: "check falsey value in section iteration don't break path resolution",
source: "{#list}{a.b}{/list}",
options: {pathScope: "global"},
context: { "list": ['', 2, ''],"a": {"b": "B"} },
expected: "BBB",
message: "Should resolve path correctly"
},
{
name: "check true value in section iteration are also OK",
source: "{#list}{a.b}{/list}",
options: {pathScope: "global"},
context: { "list": [true, 2, true],"a": {"b": "B"} },
expected: "BBB",
message: "Should resolve path correctly"
}
]
},
/**
* FILTER TESTS
*/
{
name: "filter tests",
tests: [
{
name: "filter",
source: "{#filter}foo {bar}{/filter}",
context: {
filter: function(chunk, context, bodies) {
return chunk.tap(function(data) {
return data.toUpperCase();
}).render(bodies.block, context).untap();
},
bar: "bar"
},
expected: "FOO BAR",
message: "should test the filter tag"
},
{
name: "escapeJs filter without DQ",
source: "{obj|j|s}",
context: { obj: "<script>\\testBS\\ \rtestCR\r \u2028testLS\u2028 \u2029testPS\u2029 \ntestNL\n \ftestLF\f 'testSQ' \ttestTB\t /testFS/</script>" },
expected: "<script>\\\\testBS\\\\ \\rtestCR\\r \\u2028testLS\\u2028 \\u2029testPS\\u2029 \\ntestNL\\n \\ftestLF\\f \\'testSQ\\' \\ttestTB\\t \\/testFS\\/<\\/script>",
message: "should escapeJs a string when using the j filter"
},
{
name: "escapeJs filter with only DQ",
source: "{obj|j|s}",
context: { obj: '"testDQ"' },
expected: '\\"testDQ\\"',
message: "should escapeJs a string with double quotes when using the j filter"
},
{
name: "JSON.stringify filter",
source: "{obj|js|s}",
context: { obj: { id: 1, name: "bob", occupation: "construction" } },
expected: JSON.stringify({ id: 1, name: "bob", occupation: "construction" }),
message: "should stringify a JSON literal when using the js filter"
},
{
name: "JSON.parse filter",
source: "{obj|jp}",
context: { obj: JSON.stringify({ id: 1, name: "bob", occupation: "construction" }) },
expected: JSON.parse(JSON.stringify({ id: 1, name: "bob", occupation: "construction" })).toString(),
message: "should objectify a JSON string when using the jp filter"
}
]
},
/**
* PARTIAL DEFINITIONS TESTS
*/
{
name: "partial definitions",
tests: [
{
name: "partial",
source: "Hello {name}! You have {count} new messages.",
context: { name: "Mick", count: 30 },
expected: "Hello Mick! You have 30 new messages.",
message: "should test a basic replace in a template"
},
{
name: "partial_with_blocks",
source: "{+header}default header {/header}Hello {name}! You have {count} new messages.",
context: { name: "Mick", count: 30 },
expected: "default header Hello Mick! You have 30 new messages.",
message: "should test a block with defaults"
},
{