-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar_test.dart
1046 lines (894 loc) · 37.4 KB
/
grammar_test.dart
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 'package:liquify/src/ast.dart';
import 'package:test/test.dart';
import 'shared.dart';
void main() {
group('Liquid Grammar Parser', () {
test('Parses liquid tags', () {
testParser('''
{% liquid
assign my_variable = "string"
%}''', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'liquid');
expect((tag.content[0] as Tag).name, 'assign');
});
});
test('Parses empty input', () {
testParser('''''', (document) {
expect(document.children.length, 0);
});
});
test('Parses raw tags', () {
testParser('''
{% raw %}
assign my_variable = "string"
{% endraw %}''', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'raw');
expect(tag.content.length, 1);
});
});
test('Parses empty literal', () {
testParser('{% assign my_variable = empty %}', (document) {
expect(document.children.length, 1);
final assignTag = document.children[0] as Tag;
expect(assignTag.name, 'assign');
final assignment = assignTag.content[0] as Assignment;
expect(assignment.value, isA<Literal>());
expect((assignment.value as Literal).value, Empty());
});
});
test('Parses complex tags', () {
testParser('''
{% if user.logged_in %}
<p>Welcome back, {{ user.name }}!</p>
{% if user.admin %}
<p>You are an admin!</p>
{% endif %}
{% assign my_variable = "string" %}
{% else %}
<p>Please log in.</p>
{% raw %}
{{{ }}} {{f sdfad }}
{{{ }}}}}}
{% endraw %}
{% endif %}
''', (document) {
expect(document.children.length, greaterThan(0));
});
});
test('Parses simple variable expression', () {
testParser('{{ user }}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as Variable;
expect((variable.expression as Identifier).name, 'user');
});
});
group('Numeric Literals', () {
test('Parses positive integers', () {
testParser('{% assign my_int = 25 %}', (document) {
expect(document.children.length, 1);
final assignTag = document.children[0] as Tag;
expect(assignTag.name, 'assign');
final assignment = assignTag.content[0] as Assignment;
expect(assignment.value, isA<Literal>());
expect((assignment.value as Literal).value, 25);
expect((assignment.value as Literal).type, LiteralType.number);
});
});
test('Parses negative integers', () {
testParser('{% assign my_int = -39 %}', (document) {
expect(document.children.length, 1);
final assignTag = document.children[0] as Tag;
expect(assignTag.name, 'assign');
final assignment = assignTag.content[0] as Assignment;
expect(assignment.value, isA<Literal>());
expect((assignment.value as Literal).value, -39);
expect((assignment.value as Literal).type, LiteralType.number);
});
});
test('Parses positive floats', () {
testParser('{% assign my_float = 3.14159 %}', (document) {
expect(document.children.length, 1);
final assignTag = document.children[0] as Tag;
expect(assignTag.name, 'assign');
final assignment = assignTag.content[0] as Assignment;
expect(assignment.value, isA<Literal>());
expect((assignment.value as Literal).value, 3.14159);
expect((assignment.value as Literal).type, LiteralType.number);
});
});
test('Parses negative floats', () {
testParser('{% assign my_float = -39.756 %}', (document) {
expect(document.children.length, 1);
final assignTag = document.children[0] as Tag;
expect(assignTag.name, 'assign');
final assignment = assignTag.content[0] as Assignment;
expect(assignment.value, isA<Literal>());
expect((assignment.value as Literal).value, -39.756);
expect((assignment.value as Literal).type, LiteralType.number);
});
});
});
test('Parses variable expression with filters', () {
testParser('{{ user | filter1 | filter2 }}', (document) {
expect(document.children.length, 1);
final filtered = document.children[0] as FilteredExpression;
expect(filtered.expression, isA<Variable>());
final variable = filtered.expression as Variable;
expect((variable.expression as Identifier).name, 'user');
expect(filtered.filters.length, 2);
expect(filtered.filters[0].name.name, 'filter1');
expect(filtered.filters[1].name.name, 'filter2');
});
});
test('Parses variable with multiple filters and arguments', () {
testParser('{{ price | multiply: 1.1 | round }}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as FilteredExpression;
expect(variable.expression, isA<Variable>());
final identity = variable.expression as Variable;
expect(identity.name, 'price');
expect(variable.filters.length, 2);
// Check first filter (multiply)
final multiplyFilter = variable.filters[0];
expect(multiplyFilter.name.name, 'multiply');
expect(multiplyFilter.arguments.length, 1);
expect(multiplyFilter.arguments[0], isA<Literal>());
expect((multiplyFilter.arguments[0] as Literal).value, 1.1);
// Check second filter (round)
final roundFilter = variable.filters[1];
expect(roundFilter.name.name, 'round');
expect(roundFilter.arguments.isEmpty, true);
});
});
test('Parses variable expression with filter arguments', () {
testParser('{{ user | filter1: var1,var2 | filter2: var3:3, var4:4 }}',
(document) {
expect(document.children.length, 1);
final filtered = document.children[0] as FilteredExpression;
expect(filtered.expression, isA<Variable>());
final variable = filtered.expression as Variable;
expect((variable.expression as Identifier).name, 'user');
expect(filtered.filters.length, 2);
expect(filtered.filters[0].name.name, 'filter1');
expect(filtered.filters[0].arguments.length, 2);
expect((filtered.filters[0].arguments[0] as Identifier).name, 'var1');
expect((filtered.filters[0].arguments[1] as Identifier).name, 'var2');
expect(filtered.filters[1].name.name, 'filter2');
expect(filtered.filters[1].arguments.length, 2);
expect(
(filtered.filters[1].arguments[0] as NamedArgument).identifier.name,
'var3');
expect(
((filtered.filters[1].arguments[0] as NamedArgument).value
as Literal)
.value,
3);
expect(
(filtered.filters[1].arguments[1] as NamedArgument).identifier.name,
'var4');
expect(
((filtered.filters[1].arguments[1] as NamedArgument).value
as Literal)
.value,
4);
});
});
test('Parses variable expression with whitespace control', () {
testParser('{{- user -}}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as Variable;
expect((variable.expression as Identifier).name, 'user');
});
});
test('Parses another variable expression', () {
testParser('{{ user.name }}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as Variable;
expect(variable.name, 'user');
final memberAccess = variable.expression as MemberAccess;
expect((memberAccess.object as Identifier).name, 'user');
expect((memberAccess.members[0] as Identifier).name, equals('name'));
});
});
test('Parses another variable expression with dash character', () {
testParser('{{ user.first-name }}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as Variable;
expect(variable.name, 'user');
final memberAccess = variable.expression as MemberAccess;
expect((memberAccess.object as Identifier).name, 'user');
expect(
(memberAccess.members[0] as Identifier).name, equals('first-name'));
});
});
test('Parses member expression with more depth', () {
testParser('{{- user.name.first -}}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as Variable;
expect(variable.name, 'user');
final memberAccess = variable.expression as MemberAccess;
expect((memberAccess.object as Identifier).name, 'user');
expect((memberAccess.members[0] as Identifier).name, equals('name'));
expect((memberAccess.members[1] as Identifier).name, equals('first'));
});
});
test('Parses another variable expression', () {
testParser('{{ user.name }}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as Variable;
final memberAccess = variable.expression as MemberAccess;
expect((memberAccess.object as Identifier).name, 'user');
expect(memberAccess.members.length, 1);
expect((memberAccess.members[0] as Identifier).name, equals('name'));
});
});
test('Parses member expression with more depth', () {
testParser('{{- user.name.first -}}', (document) {
expect(document.children.length, 1);
final variable = document.children[0] as Variable;
expect(variable.name, 'user');
expect(variable.expression, isA<MemberAccess>());
final memberAccess = variable.expression as MemberAccess;
expect(memberAccess.members.length, 2);
expect((memberAccess.members[0] as Identifier).name, equals('name'));
expect((memberAccess.members[1] as Identifier).name, equals('first'));
});
});
test('Parses basic tag without content', () {
testParser('{% tagname %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
assert(tag.content.isEmpty);
});
});
test('Parses tag with variables', () {
testParser('{% tagname my_string = "Hello World!" %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
assert(tag.content.isNotEmpty);
expect(tag.content[0], isA<Assignment>());
});
});
test('Parses tag with comma separated arguments', () {
testParser('{% tagname var1,var2,var3, var4 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
assert(tag.content.isNotEmpty);
expect(tag.content[0], isA<Identifier>());
expect(tag.content[1], isA<Identifier>());
expect(tag.content[2], isA<Identifier>());
expect(tag.content[3], isA<Identifier>());
});
testParser('{% tagname "var1",var2,var3,"var4" %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
assert(tag.content.isNotEmpty);
expect(tag.content[0], isA<Literal>());
expect(tag.content[1], isA<Identifier>());
expect(tag.content[2], isA<Identifier>());
expect(tag.content[3], isA<Literal>());
});
});
test('Parses tag with spac separated arguments', () {
testParser('{% tagname var1 var2 var3 var4 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
assert(tag.content.isNotEmpty);
expect(tag.content[0], isA<Identifier>());
expect(tag.content[1], isA<Identifier>());
expect(tag.content[2], isA<Identifier>());
expect(tag.content[3], isA<Identifier>());
});
});
test('Parses tag with variable and filter', () {
testParser('{% tagname myvar | filter1 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
expect(tag.content.length, 1);
ASTNode variable = tag.content[0];
expect((variable as Identifier).name, 'myvar');
expect(tag.filters.length, 1);
expect(tag.filters[0].name.name, 'filter1');
expect(tag.filters[0].arguments.isEmpty, true);
});
});
test('Parses tag with multiple filters', () {
testParser('{% tagname | filter1 | filter2 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
expect(tag.filters.length, 2);
expect(tag.filters[0].name.name, 'filter1');
expect(tag.filters[1].name.name, 'filter2');
});
});
test('Parses tag with filter and arguments', () {
testParser('{% tagname | filter1: arg1, arg2, "arg3" %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
expect(tag.filters.length, 1);
expect(tag.filters[0].name.name, 'filter1');
expect(tag.filters[0].arguments.length, 3);
expect((tag.filters[0].arguments[0] as Identifier).name, 'arg1');
expect((tag.filters[0].arguments[1] as Identifier).name, 'arg2');
expect((tag.filters[0].arguments[2] as Literal).value, 'arg3');
});
});
test('Parses tag with filter and named arguments for all literal types',
() {
testParser(
'{% tagname | filter1: arg1:1, arg2:"2", arg3:\'3\', arg4:true, arg5:false %}',
(document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
expect(tag.filters.length, 1);
expect(tag.filters[0].name.name, 'filter1');
expect(tag.filters[0].arguments.length, 5);
// Checking the first argument (number)
expect((tag.filters[0].arguments[0] as NamedArgument).identifier.name,
'arg1');
expect(
((tag.filters[0].arguments[0] as NamedArgument).value as Literal)
.value,
1);
expect(
((tag.filters[0].arguments[0] as NamedArgument).value as Literal)
.type,
LiteralType.number);
// Checking the second argument (double-quoted string)
expect((tag.filters[0].arguments[1] as NamedArgument).identifier.name,
'arg2');
expect(
((tag.filters[0].arguments[1] as NamedArgument).value as Literal)
.value,
'2');
expect(
((tag.filters[0].arguments[1] as NamedArgument).value as Literal)
.type,
LiteralType.string);
// Checking the third argument (single-quoted string)
expect((tag.filters[0].arguments[2] as NamedArgument).identifier.name,
'arg3');
expect(
((tag.filters[0].arguments[2] as NamedArgument).value as Literal)
.value,
'3');
expect(
((tag.filters[0].arguments[2] as NamedArgument).value as Literal)
.type,
LiteralType.string);
// Checking the fourth argument (boolean true)
expect((tag.filters[0].arguments[3] as NamedArgument).identifier.name,
'arg4');
expect(
((tag.filters[0].arguments[3] as NamedArgument).value as Literal)
.value,
true);
expect(
((tag.filters[0].arguments[3] as NamedArgument).value as Literal)
.type,
LiteralType.boolean);
// Checking the fifth argument (boolean false)
expect((tag.filters[0].arguments[4] as NamedArgument).identifier.name,
'arg5');
expect(
((tag.filters[0].arguments[4] as NamedArgument).value as Literal)
.value,
false);
expect(
((tag.filters[0].arguments[4] as NamedArgument).value as Literal)
.type,
LiteralType.boolean);
});
});
test('Parses tag with arguments and filters', () {
testParser('{% tagname arg1 "arg2" | filter1 | filter2: 123 %}',
(document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'tagname');
expect(tag.content.length, 2);
expect((tag.content[0] as Identifier).name, 'arg1');
expect((tag.content[1] as Literal).value, 'arg2');
expect(tag.filters.length, 2);
expect(tag.filters[0].name.name, 'filter1');
expect(tag.filters[1].name.name, 'filter2');
expect(tag.filters[1].arguments.length, 1);
expect((tag.filters[1].arguments[0] as Literal).value, 123);
});
});
test('Parses assignments within tags', () {
testParser('{% if user %}{% assign my_variable = "string" %}{% endif %}',
(document) {
expect(document.children.length, 1);
final ifTag = document.children[0] as Tag;
expect(ifTag.name, 'if');
expect(ifTag.body.length, 1);
expect((ifTag.content[0] as Identifier).name, 'user');
final assignTag = ifTag.body[0] as Tag;
expect(assignTag.name, 'assign');
expect(
((assignTag.content[0] as Assignment).variable as Identifier).name,
'my_variable');
expect((assignTag.content[0] as Assignment).value is Literal, true);
expect(((assignTag.content[0] as Assignment).value as Literal).value,
'string');
});
});
test('Parses comparison operators', () {
final comparisonTests = [
'{% if 1 == 1 %}Equal{% endif %}',
'{% if 1 != 2 %}Not Equal{% endif %}',
'{% if 1 < 2 %}Less than{% endif %}',
'{% if 2 > 1 %}Greater than{% endif %}',
'{% if 1 <= 1 %}Less than or equal{% endif %}',
'{% if 2 >= 1 %}Greater than or equal{% endif %}',
];
for (final testCase in comparisonTests) {
testParser(testCase, (document) {
expect(document.children.length, 1);
final ifTag = document.children[0] as Tag;
expect(ifTag.name, 'if');
expect(ifTag.content[0] is BinaryOperation, true);
expect(ifTag.body.length, 1);
});
}
});
test('Parses unary operations', () {
testParser('{% if not user.is_logged_in %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content[0], isA<UnaryOperation>());
});
testParser('{% if !user.is_logged_in %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content[0], isA<UnaryOperation>());
});
});
test('Parses simple logical AND operation', () {
testParser('{% if true and false %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final operation = tag.content[0] as BinaryOperation;
expect(operation.operator, 'and');
expect((operation.left as Literal).value, true);
expect((operation.right as Literal).value, false);
});
});
test('Parses logical OR operation', () {
testParser('{% if true or false %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final operation = tag.content[0] as BinaryOperation;
expect(operation.operator, 'or');
expect((operation.left as Literal).value, true);
expect((operation.right as Literal).value, false);
});
});
test('Parses mixed logical AND/OR operation', () {
testParser('{% if true and false or true %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
// The root operation should be 'or'
final rootOperation = tag.content[0] as BinaryOperation;
expect(rootOperation.operator, 'or');
// The left-hand side of 'or' should be an 'and' operation
final leftOperation = rootOperation.left as BinaryOperation;
expect(leftOperation.operator, 'and');
expect((leftOperation.left as Literal).value, true);
expect((leftOperation.right as Literal).value, false);
// The right-hand side of 'or' should be a Literal true
expect((rootOperation.right as Literal).value, true);
});
});
test('Parses logical operation with comparison', () {
testParser('{% if 1 == 1 and 2 > 1 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final operation = tag.content[0] as BinaryOperation;
// Expect AND operation as the root
expect(operation.operator, 'and');
// The left side should be a comparison
final leftComparison = operation.left as BinaryOperation;
expect(leftComparison.operator, '==');
expect((leftComparison.left as Literal).value, 1);
expect((leftComparison.right as Literal).value, 1);
// The right side should be a comparison
final rightComparison = operation.right as BinaryOperation;
expect(rightComparison.operator, '>');
expect((rightComparison.left as Literal).value, 2);
expect((rightComparison.right as Literal).value, 1);
});
});
test('Parses logical operation with unary NOT', () {
testParser('{% if not false %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final operation = tag.content[0] as UnaryOperation;
expect(operation.operator, 'not');
expect((operation.expression as Literal).value, false);
});
});
test('Parses complex logical operation with unary and binary operators',
() {
testParser('{% if not false and 1 == 1 or 2 < 3 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final operation = tag.content[0] as BinaryOperation;
// Expect OR operation as the root
expect(operation.operator, 'or');
// The left side should be an AND operation
final leftOperation = operation.left as BinaryOperation;
expect(leftOperation.operator, 'and');
// The left side of the AND should be a NOT operation
final notOperation = leftOperation.left as UnaryOperation;
expect(notOperation.operator, 'not');
expect((notOperation.expression as Literal).value, false);
// The right side of the AND should be a comparison
final comparison = leftOperation.right as BinaryOperation;
expect(comparison.operator, '==');
expect((comparison.left as Literal).value, 1);
expect((comparison.right as Literal).value, 1);
// The right side of the OR should be a comparison
final rightComparison = operation.right as BinaryOperation;
expect(rightComparison.operator, '<');
expect((rightComparison.left as Literal).value, 2);
expect((rightComparison.right as Literal).value, 3);
});
});
test('Parses contains operator', () {
final containsTests = [
'{% if product.title contains "Pack" %}Contains Pack{% endif %}',
'{% if product.tags contains "Hello" %}Contains Hello{% endif %}',
];
for (final testCase in containsTests) {
testParser(testCase, (document) {
expect(document.children.length, 1);
final ifTag = document.children[0] as Tag;
expect(ifTag.name, 'if');
expect(ifTag.content[0] is BinaryOperation, true);
expect(ifTag.body.length, 1);
});
}
});
test('Parses a simple range in a tag', () {
testParser('{% for i in (1..5) %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'for');
expect(tag.content.length, 1);
final inOperator = tag.content[0] as BinaryOperation;
expect(inOperator.operator, 'in');
expect((inOperator.left), isA<Identifier>());
expect((inOperator.right), isA<BinaryOperation>());
expect((inOperator.left as Identifier).name, 'i');
final range = inOperator.right as BinaryOperation;
expect(range.operator, '..');
expect((range.left as Literal).value, 1);
expect((range.right as Literal).value, 5);
});
});
test('Parses a range in an if statement', () {
testParser('{% if (3..7) contains 5 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final containsOperation = tag.content[0] as BinaryOperation;
expect(containsOperation.operator, 'contains');
final range = containsOperation.left as BinaryOperation;
expect(range.operator, '..');
expect((range.left as Literal).value, 3);
expect((range.right as Literal).value, 7);
expect((containsOperation.right as Literal).value, 5);
});
});
test('Parses a complex range with logical operators', () {
testParser('{% if i in (1..5) and j in (6..10) %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final andOperation = tag.content[0] as BinaryOperation;
expect(andOperation.operator, 'and');
final firstInOperation = andOperation.left as BinaryOperation;
expect(firstInOperation.operator, 'in');
final secondInOperation = andOperation.right as BinaryOperation;
expect(secondInOperation.operator, 'in');
// First in operation check
expect((firstInOperation.left as Identifier).name, 'i');
final firstRange = firstInOperation.right as BinaryOperation;
expect(firstRange.operator, '..');
expect((firstRange.left as Literal).value, 1);
expect((firstRange.right as Literal).value, 5);
// Second in operation check
expect((secondInOperation.left as Identifier).name, 'j');
final secondRange = secondInOperation.right as BinaryOperation;
expect(secondRange.operator, '..');
expect((secondRange.left as Literal).value, 6);
expect((secondRange.right as Literal).value, 10);
});
});
test('Parses a simple grouped expression', () {
testParser('{% if (1 + 2) == 3 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final comparison = tag.content[0] as BinaryOperation;
expect(comparison.operator, '==');
final groupedExpression = (comparison.left as GroupedExpression)
.expression as BinaryOperation;
expect(groupedExpression.operator, '+');
expect((groupedExpression.left as Literal).value, 1);
expect((groupedExpression.right as Literal).value, 2);
expect((comparison.right as Literal).value, 3);
});
});
test('Parses nested grouped expressions', () {
testParser('{% if ((1 + 2) * 3) == 9 %}', (document) {
expect(document.children.length, 1);
final tag = document.children[0] as Tag;
expect(tag.name, 'if');
expect(tag.content.length, 1);
final comparison = tag.content[0] as BinaryOperation;
expect(comparison.operator, '==');
final outerGroup = (comparison.left as GroupedExpression);
final groupedExpression = outerGroup.expression as BinaryOperation;
expect(groupedExpression.operator, '*');
final innerGroup = groupedExpression.left as GroupedExpression;
final innerExpression = innerGroup.expression as BinaryOperation;
expect(innerExpression.operator, '+');
expect((innerExpression.left as Literal).value, 1);
expect((innerExpression.right as Literal).value, 2);
expect((groupedExpression.right as Literal).value, 3);
expect((comparison.right as Literal).value, 9);
});
});
group('control flow', () {
group('IfTag', () {
test('basic if statement', () {
testParser(
'{% if true %}'
'Should be True'
'{% mytag %}'
'{% endif %}', (document) {
expect(document.children.length, 1);
final ifTag = document.children[0] as Tag;
expect(ifTag.name, 'if');
expect(ifTag.content.length, 1);
expect((ifTag.content[0] as Literal).value, true);
expect(ifTag.body.length, 2);
expect(ifTag.body[0], isA<TextNode>());
expect(ifTag.body[1], isA<Tag>());
});
});
test('single char braces', () {
testParser('''
{
"{{ config_path }}",
}
''', (document) {
expect(document.children.length, 3);
});
});
test('json structure', () {
testParser('''
{
"app_name": "MyApp",
"config_path": "{{ config_path }}",
"log_path": "/var/log/myapp",
"max_connections": 100
}
''', (document) {
expect(document.children.length, 3);
});
});
test('if-else statement', () {
testParser(
'{% if false %}'
'True'
'{% else %}'
'False'
'{% endif %}', (document) {
expect(document.children.length, 1);
final ifTag = document.children[0] as Tag;
expect(ifTag.name, 'if');
expect(ifTag.content.length, 1);
expect((ifTag.content[0] as Literal).value, false);
expect(ifTag.body.length, 2);
expect(ifTag.body[0], isA<TextNode>());
expect(ifTag.body[1], isA<Tag>());
// expect(ifTag.elseBody.length, 1);
// expect(ifTag.elseBody[0], isA<TextNode>());
});
});
test('nested if statements', () {
testParser(
'{% if true %}'
'{% if false %}'
'Inner False'
'{% else %}'
'Inner True'
'{% endif %}'
'{% endif %}', (document) {
expect(document.children.length, 1);
final outerIfTag = document.children[0] as Tag;
expect(outerIfTag.name, 'if');
expect(outerIfTag.content.length, 1);
expect((outerIfTag.content[0] as Literal).value, true);
expect(outerIfTag.body.length, 1);
final innerIfTag = outerIfTag.body[0] as Tag;
expect(innerIfTag.name, 'if');
expect(innerIfTag.content.length, 1);
expect((innerIfTag.content[0] as Literal).value, false);
expect(innerIfTag.body.length, 2);
expect(innerIfTag.body[0], isA<TextNode>());
expect(innerIfTag.body[1], isA<Tag>());
});
});
test('if statement with break', () {
testParser(
'{% for item in (1..5) %}'
'{% if item == 3 %}'
'{% break %}'
'{% endif %}'
'{{ item }}'
'{% endfor %}', (document) {
expect(document.children.length, 1);
final forTag = document.children[0] as Tag;
expect(forTag.name, 'for');
expect(forTag.body.length, 2);
final ifTag = forTag.body[0] as Tag;
expect(ifTag.name, 'if');
expect(ifTag.content.length, 1);
expect(ifTag.content[0], isA<BinaryOperation>());
expect(ifTag.body.length, 1);
expect((ifTag.body[0] as Tag).name, 'break');
expect(forTag.body[1], isA<Variable>());
});
});
test('if statement with continue', () {
testParser(
'{% for item in (1..5) %}'
'{% if item == 3 %}'
'{% continue %}'
'{% endif %}'
'{{ item }}'
'{% endfor %}', (document) {
expect(document.children.length, 1);
final forTag = document.children[0] as Tag;
expect(forTag.name, 'for');
expect(forTag.body.length, 2);
final ifTag = forTag.body[0] as Tag;
expect(ifTag.name, 'if');
expect(ifTag.content.length, 1);
expect(ifTag.content[0], isA<BinaryOperation>());
expect(ifTag.body.length, 1);
expect((ifTag.body[0] as Tag).name, 'continue');
expect(forTag.body[1], isA<Variable>());
});
});
});
});
});
group('Array', () {
test('Parses arrays', () {
testParser('''
{% assign page_1 = name[0] %}
''', (document) {
final tag = document.children.whereType<Tag>().first;
final assignment = tag.content[0] as Assignment;
final variable = assignment.variable as Identifier;
final arrayAccess = assignment.value as ArrayAccess;
final array = arrayAccess.array as Identifier;
final key = arrayAccess.key as Literal;
expect(variable.name, 'page_1');
expect(array.name, 'name');
expect(key, isA<Literal>());
expect(key.type, LiteralType.number);
expect(key.value, 0);
});
});
test('simple with string keys', () {
testParser('''
{% assign page_2 = pages["does-not-exist"] %}
''', (document) {
final tag = document.children.whereType<Tag>().first;
final assignment = tag.content[0] as Assignment;
final variable = assignment.variable as Identifier;
final arrayAccess = assignment.value as ArrayAccess;
final array = arrayAccess.array as Identifier;
final key = arrayAccess.key as Literal;
expect(variable.name, 'page_2');
expect(array.name, 'pages');
expect(key, isA<Literal>());
expect(key.type, LiteralType.string);
expect(key.value, 'does-not-exist');
});
});
test('member access with array access', () {
testParser('''
{% assign posts = pages.categories[0] %}
''', (document) {
final tags = document.children.whereType<Tag>().toList();
final tag3 = tags[0];
expect(((tag3.content[0] as Assignment).variable as Identifier).name,
'posts');
expect(((tag3.content[0] as Assignment).value), isA<MemberAccess>());
expect(((tag3.content[0] as Assignment).value as MemberAccess).object,
isA<Identifier>());
expect(
(((tag3.content[0] as Assignment).value as MemberAccess).object
as Identifier)
.name,
equals('pages'));
final members =
((tag3.content[0] as Assignment).value as MemberAccess).members;
expect(members.length, 1);
expect(members[0], isA<ArrayAccess>());