/
compiler.js
2265 lines (1939 loc) · 61.7 KB
/
compiler.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
(function() {
'use strict';
var expect;
var util;
var Template;
var Loader;
var Environment;
var fs;
var render;
var equal;
var finish;
var isSlim;
if (typeof require !== 'undefined') {
expect = require('expect.js');
util = require('./util');
Template = require('../nunjucks/src/environment').Template;
Environment = require('../nunjucks/src/environment').Environment;
fs = require('fs');
} else {
expect = window.expect;
util = window.util;
Template = nunjucks.Template;
Environment = nunjucks.Environment;
}
render = util.render;
equal = util.equal;
finish = util.finish;
isSlim = util.isSlim;
Loader = util.Loader;
describe('compiler', function() {
it('should compile templates', function(done) {
equal('Hello world', 'Hello world');
equal('Hello world, {{ name }}',
{
name: 'James'
},
'Hello world, James');
equal('Hello world, {{name}}{{suffix}}, how are you',
{
name: 'James',
suffix: ' Long'
},
'Hello world, James Long, how are you');
finish(done);
});
it('should escape newlines', function(done) {
equal('foo\\nbar', 'foo\\nbar');
finish(done);
});
it('should escape Unicode line seperators', function(done) {
equal('\u2028', '\u2028');
finish(done);
});
it('should compile references', function(done) {
equal('{{ foo.bar }}',
{
foo: {
bar: 'baz'
}
},
'baz');
equal('{{ foo["bar"] }}',
{
foo: {
bar: 'baz'
}
},
'baz');
finish(done);
});
it('should compile references - object without prototype', function(done) {
var context = Object.create(null);
context.foo = Object.create(null);
context.foo.bar = 'baz';
equal('{{ foo.bar }}',
context,
'baz');
equal('{{ foo["bar"] }}',
context,
'baz');
finish(done);
});
it('should fail silently on undefined values', function(done) {
equal('{{ foo }}', '');
equal('{{ foo.bar }}', '');
equal('{{ foo.bar.baz }}', '');
equal('{{ foo.bar.baz["biz"].mumble }}', '');
finish(done);
});
it('should not treat falsy values the same as undefined', function(done) {
equal('{{ foo }}', {
foo: 0
}, '0');
equal('{{ foo }}', {
foo: false
}, 'false');
finish(done);
});
it('should display none as empty string', function(done) {
equal('{{ none }}', '');
finish(done);
});
it('should compile none as falsy', function(done) {
equal('{% if not none %}yes{% endif %}', 'yes');
finish(done);
});
it('should compile none as null, not undefined', function(done) {
equal('{{ none|default("d", false) }}', '');
finish(done);
});
it('should compile function calls', function(done) {
equal('{{ foo("msg") }}',
{
foo: function(str) {
return str + 'hi';
}
},
'msghi');
finish(done);
});
it('should compile function calls with correct scope', function(done) {
equal('{{ foo.bar() }}', {
foo: {
bar: function() {
return this.baz;
},
baz: 'hello'
}
}, 'hello');
finish(done);
});
it('should compile switch statements', function() {
// standard switches
var tpl1 = '{% switch foo %}{% case "bar" %}BAR{% case "baz" %}BAZ{% default %}NEITHER FOO NOR BAR{% endswitch %}';
// test no-default switches
var tpl2 = '{% switch foo %}{% case "bar" %}BAR{% case "baz" %}BAZ{% endswitch %}';
// test fall-through cases
var tpl3 = '{% switch foo %}{% case "bar" %}{% case "baz" %}BAR{% endswitch %}';
equal(tpl1, 'NEITHER FOO NOR BAR');
equal(tpl1, {
foo: 'bar'
}, 'BAR');
equal(tpl1, {
foo: 'baz'
}, 'BAZ');
equal(tpl2, '');
equal(tpl3, {
foo: 'bar'
}, 'BAR');
equal(tpl3, {
foo: 'baz'
}, 'BAR');
});
it('should compile if blocks', function(done) {
var tmpl = ('Give me some {% if hungry %}pizza' +
'{% else %}water{% endif %}');
equal(tmpl, {
hungry: true
}, 'Give me some pizza');
equal(tmpl, {
hungry: false
}, 'Give me some water');
equal('{% if not hungry %}good{% endif %}',
{
hungry: false
},
'good');
equal('{% if hungry and like_pizza %}good{% endif %}',
{
hungry: true,
like_pizza: true
},
'good');
equal('{% if hungry or like_pizza %}good{% endif %}',
{
hungry: false,
like_pizza: true
},
'good');
equal('{% if (hungry or like_pizza) and anchovies %}good{% endif %}',
{
hungry: false,
like_pizza: true,
anchovies: true
},
'good');
equal(
'{% if food == "pizza" %}pizza{% endif %}' +
'{% if food =="beer" %}beer{% endif %}',
{
food: 'beer'
},
'beer');
equal('{% if "pizza" in food %}yum{% endif %}',
{
food: {
pizza: true
}
},
'yum');
equal('{% if pizza %}yum{% elif anchovies %}yuck{% endif %}',
{
pizza: true
},
'yum');
equal('{% if pizza %}yum{% elseif anchovies %}yuck{% endif %}',
{
pizza: true
},
'yum');
equal('{% if pizza %}yum{% elif anchovies %}yuck{% endif %}',
{
anchovies: true
},
'yuck');
equal('{% if pizza %}yum{% elseif anchovies %}yuck{% endif %}',
{
anchovies: true
},
'yuck');
equal(
'{% if topping == "pepperoni" %}yum{% elseif topping == "anchovies" %}' +
'yuck{% else %}hmmm{% endif %}',
{
topping: 'sausage'
},
'hmmm');
finish(done);
});
it('should compile the ternary operator', function(done) {
equal('{{ "foo" if bar else "baz" }}', 'baz');
equal('{{ "foo" if bar else "baz" }}', {
bar: true
}, 'foo');
finish(done);
});
it('should compile inline conditionals', function(done) {
var tmpl = 'Give me some {{ "pizza" if hungry else "water" }}';
equal(tmpl, {
hungry: true
}, 'Give me some pizza');
equal(tmpl, {
hungry: false
}, 'Give me some water');
equal('{{ "good" if not hungry }}',
{
hungry: false
}, 'good');
equal('{{ "good" if hungry and like_pizza }}',
{
hungry: true,
like_pizza: true
}, 'good');
equal('{{ "good" if hungry or like_pizza }}',
{
hungry: false,
like_pizza: true
}, 'good');
equal('{{ "good" if (hungry or like_pizza) and anchovies }}',
{
hungry: false,
like_pizza: true,
anchovies: true
}, 'good');
equal(
'{{ "pizza" if food == "pizza" }}' +
'{{ "beer" if food == "beer" }}',
{
food: 'beer'
}, 'beer');
finish(done);
});
function runLoopTests(block) {
var end = {
asyncAll: 'endall',
asyncEach: 'endeach',
for: 'endfor'
}[block];
describe('the ' + block + ' tag', function() {
it('should loop over simple arrays', function() {
equal(
'{% ' + block + ' i in arr %}{{ i }}{% ' + end + ' %}',
{ arr: [1, 2, 3, 4, 5] },
'12345');
});
it('should loop normally with an {% else %} tag and non-empty array', function() {
equal(
'{% ' + block + ' i in arr %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ arr: [1, 2, 3, 4, 5] },
'12345');
});
it('should execute the {% else %} block when looping over an empty array', function() {
equal(
'{% ' + block + ' i in arr %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ arr: [] },
'empty');
});
it('should support destructured looping', function() {
equal(
'{% ' + block + ' a, b, c in arr %}' +
'{{ a }},{{ b }},{{ c }}.{% ' + end + ' %}',
{ arr: [['x', 'y', 'z'], ['1', '2', '3']] },
'x,y,z.1,2,3.');
});
it('should do loop over key-values of a literal in-template Object', function() {
equal(
'{% ' + block + ' k, v in { one: 1, two: 2 } %}' +
'-{{ k }}:{{ v }}-{% ' + end + ' %}', '-one:1--two:2-');
});
it('should support loop.index', function() {
equal('{% ' + block + ' i in [7,3,6] %}{{ loop.index }}{% ' + end + ' %}', '123');
});
it('should support loop.index0', function() {
equal('{% ' + block + ' i in [7,3,6] %}{{ loop.index0 }}{% ' + end + ' %}', '012');
});
it('should support loop.revindex', function() {
equal('{% ' + block + ' i in [7,3,6] %}{{ loop.revindex }}{% ' + end + ' %}', '321');
});
it('should support loop.revindex0', function() {
equal('{% ' + block + ' i in [7,3,6] %}{{ loop.revindex0 }}{% ' + end + ' %}', '210');
});
it('should support loop.first', function() {
equal(
'{% ' + block + ' i in [7,3,6] %}' +
'{% if loop.first %}{{ i }}{% endif %}' +
'{% ' + end + ' %}',
'7');
});
it('should support loop.last', function() {
equal(
'{% ' + block + ' i in [7,3,6] %}' +
'{% if loop.last %}{{ i }}{% endif %}' +
'{% ' + end + ' %}',
'6');
});
it('should support loop.length', function() {
equal('{% ' + block + ' i in [7,3,6] %}{{ loop.length }}{% ' + end + ' %}', '333');
});
it('should fail silently when looping over an undefined variable', function() {
equal('{% ' + block + ' i in foo %}{{ i }}{% ' + end + ' %}', '');
});
it('should fail silently when looping over an undefined property', function() {
equal(
'{% ' + block + ' i in foo.bar %}{{ i }}{% ' + end + ' %}',
{ foo: {} },
'');
});
// TODO: this behavior differs from jinja2
it('should fail silently when looping over a null variable', function() {
equal(
'{% ' + block + ' i in foo %}{{ i }}{% ' + end + ' %}',
{ foo: null },
'');
});
it('should loop over two-dimensional arrays', function() {
equal('{% ' + block + ' x, y in points %}[{{ x }},{{ y }}]{% ' + end + ' %}',
{ points: [[1, 2], [3, 4], [5, 6]] },
'[1,2][3,4][5,6]');
});
it('should loop over four-dimensional arrays', function() {
equal(
'{% ' + block + ' a, b, c, d in arr %}[{{ a }},{{ b }},{{ c }},{{ d }}]{% ' + end + '%}',
{ arr: [[1, 2, 3, 4], [5, 6, 7, 8]] },
'[1,2,3,4][5,6,7,8]');
});
it('should support loop.index with two-dimensional loops', function() {
equal('{% ' + block + ' x, y in points %}{{ loop.index }}{% ' + end + ' %}',
{
points: [[1, 2], [3, 4], [5, 6]]
},
'123');
});
it('should support loop.revindex with two-dimensional loops', function() {
equal('{% ' + block + ' x, y in points %}{{ loop.revindex }}{% ' + end + ' %}',
{
points: [[1, 2], [3, 4], [5, 6]]
},
'321');
});
it('should support key-value looping over an Object variable', function() {
equal('{% ' + block + ' k, v in items %}({{ k }},{{ v }}){% ' + end + ' %}',
{
items: {
foo: 1,
bar: 2
}
},
'(foo,1)(bar,2)');
});
it('should support loop.index when looping over an Object\'s key-value pairs', function() {
equal('{% ' + block + ' k, v in items %}{{ loop.index }}{% ' + end + ' %}',
{
items: {
foo: 1,
bar: 2
}
},
'12');
});
it('should support loop.revindex when looping over an Object\'s key-value pairs', function() {
equal('{% ' + block + ' k, v in items %}{{ loop.revindex }}{% ' + end + ' %}',
{
items: {
foo: 1,
bar: 2
}
},
'21');
});
it('should support loop.length when looping over an Object\'s key-value pairs', function() {
equal('{% ' + block + ' k, v in items %}{{ loop.length }}{% ' + end + ' %}',
{
items: {
foo: 1,
bar: 2
}
},
'22');
});
it('should support include tags in the body of the loop', function() {
equal('{% ' + block + ' item, v in items %}{% include "item.njk" %}{% ' + end + ' %}',
{
items: {
foo: 1,
bar: 2
}
},
'showing fooshowing bar');
});
it('should work with {% set %} and {% include %} tags', function() {
equal(
'{% set item = passed_var %}' +
'{% include "item.njk" %}\n' +
'{% ' + block + ' i in passed_iter %}' +
'{% set item = i %}' +
'{% include "item.njk" %}\n' +
'{% ' + end + ' %}',
{
passed_var: 'test',
passed_iter: ['1', '2', '3']
},
'showing test\nshowing 1\nshowing 2\nshowing 3\n');
});
/* global Set */
it('should work with Set builtin', function() {
if (typeof Set === 'undefined') {
this.skip();
} else {
equal('{% ' + block + ' i in set %}{{ i }}{% ' + end + ' %}',
{ set: new Set([1, 2, 3, 4, 5]) },
'12345');
equal('{% ' + block + ' i in set %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ set: new Set([1, 2, 3, 4, 5]) },
'12345');
equal('{% ' + block + ' i in set %}{{ i }}{% else %}empty{% ' + end + ' %}',
{ set: new Set() },
'empty');
}
});
/* global Map */
it('should work with Map builtin', function() {
if (typeof Map === 'undefined') {
this.skip();
} else {
equal('{% ' + block + ' k, v in map %}[{{ k }},{{ v }}]{% ' + end + ' %}',
{ map: new Map([[1, 2], [3, 4], [5, 6]]) },
'[1,2][3,4][5,6]');
equal('{% ' + block + ' k, v in map %}[{{ k }},{{ v }}]{% else %}empty{% ' + end + ' %}',
{ map: new Map([[1, 2], [3, 4], [5, 6]]) },
'[1,2][3,4][5,6]');
equal('{% ' + block + ' k, v in map %}[{{ k }},{{ v }}]{% else %}empty{% ' + end + ' %}',
{ map: new Map() },
'empty');
}
});
});
}
runLoopTests('for');
runLoopTests('asyncEach');
runLoopTests('asyncAll');
it('should allow overriding var with none inside nested scope', function(done) {
equal(
'{% set var = "foo" %}' +
'{% for i in [1] %}{% set var = none %}{{ var }}{% endfor %}',
'');
finish(done);
});
it('should compile async control', function(done) {
var opts;
if (!fs) {
this.skip();
} else {
opts = {
asyncFilters: {
getContents: function(tmpl, cb) {
fs.readFile(tmpl, cb);
},
getContentsArr: function(arr, cb) {
fs.readFile(arr[0], function(err, res) {
cb(err, [res]);
});
}
}
};
render('{{ tmpl | getContents }}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere');
});
render('{% if tmpl %}{{ tmpl | getContents }}{% endif %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere');
});
render('{% if tmpl | getContents %}yes{% endif %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('yes');
});
render('{% for t in [tmpl, tmpl] %}{{ t | getContents }}*{% endfor %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere*somecontenthere*');
});
render('{% for t in [tmpl, tmpl] | getContentsArr %}{{ t }}{% endfor %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere');
});
render('{% if test %}{{ tmpl | getContents }}{% endif %}oof',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('oof');
});
render(
'{% if tmpl %}' +
'{% for i in [0, 1] %}{{ tmpl | getContents }}*{% endfor %}' +
'{% endif %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere*somecontenthere*');
});
render('{% block content %}{{ tmpl | getContents }}{% endblock %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere');
});
render('{% block content %}hello{% endblock %} {{ tmpl | getContents }}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('hello somecontenthere');
});
render('{% block content %}{% set foo = tmpl | getContents %}{{ foo }}{% endblock %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere');
});
render('{% block content %}{% include "async.njk" %}{% endblock %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere\n');
});
render('{% asyncEach i in [0, 1] %}{% include "async.njk" %}{% endeach %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('somecontenthere\nsomecontenthere\n');
});
render('{% asyncAll i in [0, 1, 2, 3, 4] %}-{{ i }}:{% include "async.njk" %}-{% endall %}',
{
tmpl: 'tests/templates/for-async-content.njk'
},
opts,
function(err, res) {
expect(res).to.be('-0:somecontenthere\n-' +
'-1:somecontenthere\n-' +
'-2:somecontenthere\n-' +
'-3:somecontenthere\n-' +
'-4:somecontenthere\n-');
});
}
finish(done);
});
it('should compile basic arithmetic operators', function() {
equal('{{ 3 + 4 - 5 * 6 / 10 }}', '4');
});
it('should compile the exponentiation (**) operator', function() {
equal('{{ 4**5 }}', '1024');
});
it('should compile the integer division (//) operator', function() {
equal('{{ 9//5 }}', '1');
});
it('should compile the modulus operator', function() {
equal('{{ 9%5 }}', '4');
});
it('should compile numeric negation operator', function() {
equal('{{ -5 }}', '-5');
});
it('should compile comparison operators', function() {
equal('{% if 3 < 4 %}yes{% endif %}', 'yes');
equal('{% if 3 > 4 %}yes{% endif %}', '');
equal('{% if 9 >= 10 %}yes{% endif %}', '');
equal('{% if 10 >= 10 %}yes{% endif %}', 'yes');
equal('{% if 9 <= 10 %}yes{% endif %}', 'yes');
equal('{% if 10 <= 10 %}yes{% endif %}', 'yes');
equal('{% if 11 <= 10 %}yes{% endif %}', '');
equal('{% if 10 != 10 %}yes{% endif %}', '');
equal('{% if 10 == 10 %}yes{% endif %}', 'yes');
equal('{% if "0" == 0 %}yes{% endif %}', 'yes');
equal('{% if "0" === 0 %}yes{% endif %}', '');
equal('{% if "0" !== 0 %}yes{% endif %}', 'yes');
equal('{% if 0 == false %}yes{% endif %}', 'yes');
equal('{% if 0 === false %}yes{% endif %}', '');
equal('{% if foo(20) > bar %}yes{% endif %}',
{
foo: function(n) {
return n - 1;
},
bar: 15
},
'yes');
});
it('should compile python-style ternary operators', function() {
equal('{{ "yes" if 1 is odd else "no" }}', 'yes');
equal('{{ "yes" if 2 is even else "no" }}', 'yes');
equal('{{ "yes" if 2 is odd else "no" }}', 'no');
equal('{{ "yes" if 1 is even else "no" }}', 'no');
});
it('should compile the "in" operator for Arrays', function() {
equal('{% if 1 in [1, 2] %}yes{% endif %}', 'yes');
equal('{% if 1 in [2, 3] %}yes{% endif %}', '');
equal('{% if 1 not in [1, 2] %}yes{% endif %}', '');
equal('{% if 1 not in [2, 3] %}yes{% endif %}', 'yes');
equal('{% if "a" in vals %}yes{% endif %}',
{ vals: ['a', 'b'] },
'yes');
});
it('should compile the "in" operator for objects', function() {
equal('{% if "a" in obj %}yes{% endif %}',
{ obj: { a: true } },
'yes');
equal('{% if "a" in obj %}yes{% endif %}',
{ obj: { b: true } },
'');
});
it('should compile the "in" operator for strings', function() {
equal('{% if "foo" in "foobar" %}yes{% endif %}', 'yes');
});
it('should throw an error when using the "in" operator on unexpected types', function(done) {
render(
'{% if "a" in 1 %}yes{% endif %}',
{},
{
noThrow: true
},
function(err, res) {
expect(res).to.be(undefined);
expect(err).to.match(
/Cannot use "in" operator to search for "a" in unexpected types\./
);
}
);
render(
'{% if "a" in obj %}yes{% endif %}',
{},
{
noThrow: true
},
function(err, res) {
expect(res).to.be(undefined);
expect(err).to.match(
/Cannot use "in" operator to search for "a" in unexpected types\./
);
}
);
finish(done);
});
if (!isSlim) {
it('should include error line in raised TemplateError', function(done) {
var tmplStr = [
'{% set items = ["a", "b",, "c"] %}',
'{{ items | join(",") }}',
].join('\n');
var loader = new Loader('tests/templates');
var env = new Environment(loader);
var tmpl = new Template(tmplStr, env, 'parse-error.njk');
tmpl.render({}, function(err, res) {
expect(res).to.be(undefined);
expect(err.toString()).to.be([
'Template render error: (parse-error.njk) [Line 1, Column 24]',
' unexpected token: ,',
].join('\n'));
done();
});
});
}
it('should compile string concatenations with tilde', function(done) {
equal('{{ 4 ~ \'hello\' }}', '4hello');
equal('{{ 4 ~ 5 }}', '45');
equal('{{ \'a\' ~ \'b\' ~ 5 }}', 'ab5');
finish(done);
});
it('should compile macros', function(done) {
equal(
'{% macro foo() %}This is a macro{% endmacro %}' +
'{{ foo() }}',
'This is a macro');
finish(done);
});
it('should compile macros with optional args', function(done) {
equal(
'{% macro foo(x, y) %}{{ y }}{% endmacro %}' +
'{{ foo(1) }}',
'');
finish(done);
});
it('should compile macros with args that can be passed to filters', function(done) {
equal(
'{% macro foo(x) %}{{ x|title }}{% endmacro %}' +
'{{ foo("foo") }}',
'Foo');
finish(done);
});
it('should compile macros with positional args', function(done) {
equal(
'{% macro foo(x, y) %}{{ y }}{% endmacro %}' +
'{{ foo(1, 2) }}',
'2');
finish(done);
});
it('should compile macros with arg defaults', function(done) {
equal(
'{% macro foo(x, y, z=5) %}{{ y }}{% endmacro %}' +
'{{ foo(1, 2) }}',
'2');
equal(
'{% macro foo(x, y, z=5) %}{{ z }}{% endmacro %}' +
'{{ foo(1, 2) }}',
'5');
finish(done);
});
it('should compile macros with keyword args', function(done) {
equal(
'{% macro foo(x, y, z=5) %}{{ y }}{% endmacro %}' +
'{{ foo(1, y=2) }}',
'2');
finish(done);
});
it('should compile macros with only keyword args', function(done) {
equal(
'{% macro foo(x, y, z=5) %}{{ x }}{{ y }}{{ z }}' +
'{% endmacro %}' +
'{{ foo(x=1, y=2) }}',
'125');
finish(done);
});
it('should compile macros with keyword args overriding defaults', function(done) {
equal(
'{% macro foo(x, y, z=5) %}{{ x }}{{ y }}{{ z }}' +
'{% endmacro %}' +
'{{ foo(x=1, y=2, z=3) }}',
'123');
finish(done);
});
it('should compile macros with out-of-order keyword args', function(done) {
equal(
'{% macro foo(x, y=2, z=5) %}{{ x }}{{ y }}{{ z }}' +
'{% endmacro %}' +
'{{ foo(1, z=3) }}',
'123');
finish(done);
});
it('should compile macros', function(done) {
equal(
'{% macro foo(x, y=2, z=5) %}{{ x }}{{ y }}{{ z }}' +
'{% endmacro %}' +
'{{ foo(1) }}',
'125');
finish(done);
});
it('should compile macros with multiple overridden arg defaults', function(done) {
equal(
'{% macro foo(x, y=2, z=5) %}{{ x }}{{ y }}{{ z }}' +
'{% endmacro %}' +
'{{ foo(1, 10, 20) }}',
'11020');
finish(done);
});
it('should compile macro calls inside blocks', function(done) {
equal(
'{% extends "base.njk" %}' +
'{% macro foo(x, y=2, z=5) %}{{ x }}{{ y }}{{ z }}' +
'{% endmacro %}' +
'{% block block1 %}' +
'{{ foo(1) }}' +
'{% endblock %}',
'Foo125BazFizzle');
finish(done);
});
it('should compile macros defined in one block and called in another', function(done) {
equal(
'{% block bar %}' +
'{% macro foo(x, y=2, z=5) %}{{ x }}{{ y }}{{ z }}' +
'{% endmacro %}' +
'{% endblock %}' +
'{% block baz %}' +
'{{ foo(1) }}' +
'{% endblock %}',
'125');
finish(done);
});
it('should compile macros that include other templates', function(done) {
equal(
'{% macro foo() %}{% include "include.njk" %}{% endmacro %}' +
'{{ foo() }}',
{
name: 'james'
},
'FooInclude james');
finish(done);
});
it('should compile macros that set vars', function(done) {
equal(
'{% macro foo() %}{% set x = "foo"%}{{ x }}{% endmacro %}' +
'{% set x = "bar" %}' +
'{{ x }}' +
'{{ foo() }}' +
'{{ x }}',
'barfoobar');
finish(done);
});
it('should not leak variables set in macro to calling scope', function(done) {
equal(
'{% macro setFoo() %}' +
'{% set x = "foo" %}' +
'{{ x }}' +
'{% endmacro %}' +
'{% macro display() %}' +
'{% set x = "bar" %}' +
'{{ setFoo() }}' +
'{{ x }}' +
'{% endmacro %}' +
'{{ display() }}',
'foobar');
finish(done);
});
it('should not leak variables set in nested scope within macro out to calling scope', function(done) {
equal(
'{% macro setFoo() %}' +
'{% for y in [1] %}{% set x = "foo" %}{{ x }}{% endfor %}' +
'{% endmacro %}' +
'{% macro display() %}' +
'{% set x = "bar" %}' +
'{{ setFoo() }}' +
'{{ x }}' +
'{% endmacro %}' +
'{{ display() }}',
'foobar');