-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_component_test.go
More file actions
1222 lines (1058 loc) · 41.5 KB
/
node_component_test.go
File metadata and controls
1222 lines (1058 loc) · 41.5 KB
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
package gotmx
import (
"bytes"
"context"
"errors"
"strings"
"testing"
stringutils "github.com/authentic-devel/gotmx/utils"
)
type nodeComponentTestModel struct {
BooleanValue bool
BooleanValue2 bool
StringValue string
StringValue2 string
StringValue3 string
StringValue4 string
StringSlice []string
StringMap map[string]string
NestedModel nodeComponentNestedModel
}
type nodeComponentNestedModel struct {
BooleanValue bool
BooleanValue2 bool
StringValue string
StringValue2 string
StringSlice []string
}
func TestBasicComponentRender(t *testing.T) {
parseRenderAndCompareTemplate(
nil,
stringutils.TrimMargin(
`<div data-g-define="my-template">
| <span>Hello</span>
| <span>World</span>
|</div>`),
"my-template",
nil,
stringutils.TrimMargin(
`<div>
| <span>Hello</span>
| <span>World</span>
|</div>`),
t)
}
func TestRenderFullDocument(t *testing.T) {
template := stringutils.TrimMargin(
`<!DOCTYPE html>
|<html g-define="my-template">
|<head>Should be placed in the body</head>
|<body>
| <div><span>Hello</span><span>World</span></div>
|</body>
|</html>`)
expected := stringutils.TrimMargin(
`<!DOCTYPE html>
|<html><head></head><body>Should be placed in the body
|
| <div><span>Hello</span><span>World</span></div>
|
|</body></html>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expected, t)
}
func TestVoidElementsAreRenderedCorrectly(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <area>
| <base>
| <br>
| <table><colgroup><col></colgroup></table>
| <embed>
| <hr>
| <img>
| <input type="text">
| <link>
| <meta>
| <param>
| <source>
| <track>
| <wbr>
|</div>`)
expected := stringutils.TrimMargin(
`<div>
| <area />
| <base />
| <br />
| <table><colgroup><col /></colgroup></table>
| <embed />
| <hr />
| <img />
| <input type="text" />
| <link />
| <meta />
| <param />
| <source />
| <track />
| <wbr />
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expected, t)
}
// Tests that when using nested templates, the nested template is also rendered correctly when rendering the outer
// template.
func TestNestedTemplateIsRendered(t *testing.T) {
parseRenderAndCompareTemplate(
nil,
stringutils.TrimMargin(
`<div data-g-define="my-template">
| <span>Hello</span>
| <div data-g-define="nested-template">
| <span>World</span>
| </div>
|</div>`),
"my-template",
nil,
stringutils.TrimMargin(
`<div>
| <span>Hello</span>
| <div>
| <span>World</span>
| </div>
|</div>`),
t)
}
// Tests, that a template is still rendered (when using g-define) when called directly, even if the same html node has
// a g-ignore attribute on it. We explicitly define the behavior like that so that users can define nested templates
// that aren't necessarily always rendered in that spot, but still can be rendered directly.
func TestTemplateIgnoresIgnoreIfRenderedDirectly(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template" data-g-ignore>
| <span>Hello World</span>
|</div>`)
expected := stringutils.TrimMargin(
`<div>
| <span>Hello World</span>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expected, t)
// same again but with short versions of the attribute
templateShort := stringutils.TrimMargin(
`<div g-define="my-template2" g-ignore>
| <span>Hello World</span>
|</div>`)
parseRenderAndCompareTemplate(nil, templateShort, "my-template2", nil, expected, t)
}
func TestBooleanAttributesAreRenderedCorrectly(t *testing.T) {
template := `<button data-g-define="my-template" enabled>Click me!</button>`
expected := `<button enabled="">Click me!</button>`
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expected, t)
}
// Tests, that a template reference [[ :templateName ]] in an attribute works when referencing a Gotmx
// template
func TestGotmxTemplateReferencedInAttributeWorks(t *testing.T) {
template := stringutils.TrimMargin(
`<div g-define="my-template">
|<div my-attribute="[[ :other-template ]]"></div>
|</div>
|<div data-g-define="other-template" g-outer-text="Hello world with special chars < > & '">
|</div>`)
expected := stringutils.TrimMargin(
`<div>
|<div my-attribute="Hello world with special chars < > & '"></div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expected, t)
}
// todo: re-implement support for golang templates
//// Tests, that a template reference [[ :templateName ]] in an attribute works when referencing a Golang
//// HTML template
//func TestGolangTemplateReferencedInAttributeWorks(t *testing.T) {
// data := nodeComponentTestModel{
// StringValue: "special chars < > & '",
// }
// template := stringutils.TrimMargin(
// `<div g-define="my-template">
// |<div my-attribute="[[ :golang-template ]]"></div>
// |<!-- Even if the called template is actually an unsafe template, the resulting string should be escaped when called in an attribute -->
// |<div my-attribute="[[ :golang-unsafe-template ]]"></div>
// |</div>
// |<div g-as-template="golang-template">Hello world with {{ .StringValue }}</div>
// |<div g-as-unsafe-template="golang-unsafe-template">Hello world with {{ .StringValue }}</div>
// `)
// expected := stringutils.TrimMargin(
// `<div>
// |<div my-attribute="Hello world with special chars < > & '"></div>
// |
// |<div my-attribute="Hello world with special chars < > & '"></div>
// |</div>`)
// parseRenderAndCompareTemplate(nil, template, "my-template", data, expected, t)
//}
// Tests, that an empty string is returned when referencing a template that does not exist
func TestUnknownTemplateReferenceReturnsEmptyString(t *testing.T) {
template := stringutils.TrimMargin(
`<div g-define="my-template">
|<div my-attribute="[[ :golang-template ]]"></div>
|</div>`)
expected := stringutils.TrimMargin(
`<div>
|<div my-attribute=""></div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expected, t)
}
//********************************* g-if *******************************************************************************
//**********************************************************************************************************************
// Tests, that the g-if attribute of a component is ignored, in case the component is programmatically added
// as a slotted child when rendering.
func TestTemplateIgnoresIfIfRenderedAsSlotted(t *testing.T) {
// todo: implement
// todo: adapt documentation
}
func TestGIfLongVersionHasHigherPriority(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <div g-if="true" data-g-if="false">Hello World</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
// Tests, that a template with a g-define is still rendered when called directly, even if the same html node has
// a g-if attribute on it that evaluates to false. We explicitly define the behavior like that so that users can define
// nested templates that aren't necessarily always rendered in that spot, but still can be rendered directly.
func TestTemplateIgnoresIfIfRenderedDirectly(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="outer-template" g-if="false">
| <div g-if="true">Hello World</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
| <div>Hello World</div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "outer-template", nil, expectation, t)
}
func TestIfWorksWithBooleanGoTemplate(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="{{ .StringValue }}">Hello World</div>
|</div>`)
expectationForFalse := stringutils.TrimMargin(
`<div>
|
|</div>`)
expectationForTrue := stringutils.TrimMargin(
`<div>
| <div>Hello World</div>
|</div>`)
data := nodeComponentTestModel{
StringValue: "true",
}
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForTrue, t)
data.StringValue = "false"
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForFalse, t)
data.StringValue = "invalid"
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForFalse, t)
data.StringValue = ""
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForFalse, t)
}
// Tests that an if ignores any invalid (non-boolean) expression and considers the expression to be false.
// But if should still work with string values, that are not "true" or "false"
func TestIfWorksWithNonBooleanValues(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="[[ .StringValue ]]">Hello World</div>
|</div>`)
expectationForFalse := stringutils.TrimMargin(
`<div>
|
|</div>`)
expectationForTrue := stringutils.TrimMargin(
`<div>
| <div>Hello World</div>
|</div>`)
data := nodeComponentTestModel{
StringValue: "true",
}
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForTrue, t)
data.StringValue = "false"
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForFalse, t)
data.StringValue = "invalid"
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForFalse, t)
data.StringValue = ""
parseRenderAndCompareTemplate(nil, template, "outer-template", data, expectationForFalse, t)
}
func TestIfWorksWithBooleanModelPath(t *testing.T) {
data := nodeComponentTestModel{
BooleanValue: true,
}
// if true, should be rendered
parseRenderAndCompareTemplate(nil,
stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="[[ .BooleanValue ]]">Hello World</div>
|</div>`),
"outer-template",
data,
stringutils.TrimMargin(
`<div>
| <div>Hello World</div>
|</div>`),
t)
// if false, should not be rendered
parseRenderAndCompareTemplate(nil,
stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="[[ !.BooleanValue ]] ">Hello World</div>
|</div>`),
"outer-template",
data,
stringutils.TrimMargin(
`<div>
|
|</div>`),
t)
// if false, should not be rendered
data.BooleanValue = false
parseRenderAndCompareTemplate(nil,
stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="[[ .BooleanValue ]] ">Hello World</div>
|</div>`),
"outer-template",
data,
stringutils.TrimMargin(
`<div>
|
|</div>`),
t)
}
func TestIfWorksWithBooleanLiteral(t *testing.T) {
// if true, should be rendered
parseRenderAndCompareTemplate(nil,
stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="true">Hello World</div>
|</div>`),
"outer-template",
nil,
stringutils.TrimMargin(
`<div>
| <div>Hello World</div>
|</div>`),
t)
// if false, should not be rendered
parseRenderAndCompareTemplate(nil,
stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="false">Hello World</div>
|</div>`),
"outer-template",
nil,
stringutils.TrimMargin(
`<div>
|
|</div>`),
t)
// if with invalid value, should not be rendered
parseRenderAndCompareTemplate(nil,
stringutils.TrimMargin(
`<div data-g-define="outer-template">
| <div g-if="invalid">Hello World</div>
|</div>`),
"outer-template",
nil,
stringutils.TrimMargin(
`<div>
|
|</div>`),
t)
}
//********************************* g-ignore ***************************************************************************
//**********************************************************************************************************************
func TestIgnoreWorks(t *testing.T) {
data := nodeComponentTestModel{
StringValue: "outer",
StringValue2: "inner",
StringValue3: "outer-only",
StringValue4: "something invalid",
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| 1: <div g-ignore="outer">outer ignored, should produce empty line</div>
| 2: <div g-ignore="inner">Inner ignored. Should produce an empty div tag</div>
| 3: <div g-ignore="outer-only">Outer-only, only this text should be shown</div>
| 4: <div g-ignore="[[ .StringValue ]]">Outer, should be an empty line</div>
| 5: <div g-ignore="[[ .StringValue2 ]]">Inner, should be an empty div</div>
| 6: <div g-ignore="[[ .StringValue3 ]]">Outer-only, only this text should be shown</div>
| 7: <div g-ignore="">Same as outer, should be an empty line Hello World</div>
| 8: <div g-ignore="[[ .StringValue4 ]]">When invalid value is given, everything should stay as it is</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
| 1:
| 2: <div></div>
| 3: Outer-only, only this text should be shown
| 4:
| 5: <div></div>
| 6: Outer-only, only this text should be shown
| 7:
| 8: <div>When invalid value is given, everything should stay as it is</div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
//********************************* g-override-att *********************************************************************
//**********************************************************************************************************************
func TestOverrideAttWorks(t *testing.T) {
data := nodeComponentTestModel{
StringValue: "style",
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| 1: <div g-use="my-template2" g-override-att="class" class="blue">Only class attribute should be overridden</div>
| 2: <div g-use="my-template2" g-override-att="style" style="">Only style attribute should be overridden</div>
| 3: <div g-use="my-template2" g-override-att="style,class">Class and style attribute should be overridden</div>
| 4: <div g-use="my-template2" g-override-att="class,style">Class and style attribute should be overridden</div>
| 5: <div g-use="my-template2" g-override-att="[[ .StringValue ]]">Should even work with model path</div>
| 6: <div g-use="my-template3" g-override-att="g-inner-text" g-inner-text="Alternate text">Should even work with g-inner-text</div>
| 7: <div g-use="my-template2" data-g-override-att="style">Should work with long version</div>
| 8: <div g-use="my-template2" g-override-att="style" g-att-style="from-g-att">Should work with g-att</div>
|</div>
|<div data-g-define="my-template2" class="red" style="background-color:blue"></div>
|<div data-g-define="my-template3" g-inner-text="Dummy"></div>`)
expectation := stringutils.TrimMargin(
`<div>
| 1: <div class="blue" style="background-color:blue"></div>
| 2: <div class="red" style=""></div>
| 3: <div class="red" style="background-color:blue"></div>
| 4: <div class="red" style="background-color:blue"></div>
| 5: <div class="red" style="background-color:blue"></div>
| 6: <div>Alternate text</div>
| 7: <div class="red" style="background-color:blue"></div>
| 8: <div class="red" style="from-g-att"></div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
// Tests that when using g-use and g-attif and ag-override-att, that the target component will not have the
// attribute, if the condition evaluates to false.
// In this test the target template has a style attribute.
// When calling the template with g-use, we actually override the style attribute, but since we use g-attif-style with "false",
// we expect the rendered target to not have that style anymore. Because we decided to override it.
func TestOverrideAttWorksWithGAttId(t *testing.T) {
data := nodeComponentTestModel{
BooleanValue: false,
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <div g-use="my-template2" g-override-att="style" style="ignored" g-attif-style="[[ .BooleanValue ]]">Should work with g-attif</div>
|</div>
|<div data-g-define="my-template2" class="red" style="background-color:blue">Should not have a style attribute</div>`)
expectation := stringutils.TrimMargin(
`<div>
| <div class="red">Should not have a style attribute</div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
//********************************* g-inner-repeat **********************************************************************
//**********************************************************************************************************************
func TestGInnerRepeatWorks(t *testing.T) {
data := nodeComponentTestModel{
StringSlice: []string{"1", "2", "Special chars should be escaped & < > ' \""},
}
template := stringutils.TrimMargin(
`<ul data-g-define="my-template" g-inner-repeat="[[ .StringSlice ]]">
|<li g-inner-text="[[ . ]]"></li>
|</ul>`)
expectation := stringutils.TrimMargin(
`<ul>
|<li>1</li>
|
|<li>2</li>
|
|<li>Special chars should be escaped & < > ' "</li>
|</ul>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
//********************************* g-outer-repeat **********************************************************************
//**********************************************************************************************************************
func TestGOuterRepeatWorks(t *testing.T) {
data := nodeComponentTestModel{
StringSlice: []string{"1", "2", "Special chars should be escaped & < > ' \""},
}
template := stringutils.TrimMargin(
`<ul data-g-define="my-template">
|<li g-outer-repeat="[[ .StringSlice ]]" g-inner-text="[[ . ]]"></li>
|</ul>`)
expectation := stringutils.TrimMargin(
`<ul>
|<li>1</li><li>2</li><li>Special chars should be escaped & < > ' "</li>
|</ul>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
func TestGOuterRepeatWorksWithEmptySlice(t *testing.T) {
data := nodeComponentTestModel{
StringSlice: []string{},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<ul>
|<li g-outer-repeat="[[ .StringSlice ]]">[[ . ]]</li>
|</ul>
|<ul>
|<li data-g-outer-repeat="[[ .StringSlice ]]">[[ . ]]</li>
|</ul>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|<ul>
|
|</ul>
|<ul>
|
|</ul>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
func TestGOuterRepeatWorksWithGUse(t *testing.T) {
data := nodeComponentTestModel{
StringSlice: []string{"1", "2", "Special chars should be escaped & < > ' \""},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-outer-repeat="[[ .StringSlice ]]" data-g-use="my-template2" g-inner-text="[[ . ]]"
| g-override-att="g-inner-text"></div>
|</div>
|<li data-g-define="my-template2"></li>`)
expectation := stringutils.TrimMargin(
`<div>
|<li>1</li><li>2</li><li>Special chars should be escaped & < > ' "</li>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
func TestGOuterRepeatWorksWithGOuterText(t *testing.T) {
data := nodeComponentTestModel{
StringSlice: []string{"1", "2", "Special chars should be escaped & < > ' \""},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-outer-repeat="[[ .StringSlice ]]" g-outer-text="[[ . ]]"></div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|12Special chars should be escaped & < > ' "
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
func TestGOuterRepeatWorksWithNestedModel(t *testing.T) {
data := nodeComponentTestModel{
NestedModel: nodeComponentNestedModel{
StringSlice: []string{"1", "2", "Special chars should be escaped & < > ' \""},
},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<li g-outer-repeat="[[ .NestedModel.StringSlice ]]" g-inner-text="[[ . ]]"></li>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|<li>1</li><li>2</li><li>Special chars should be escaped & < > ' "</li>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
// imp: the problem with this test is "order". Maps are not ordered so there could be different results
//func TestGOuterRepeatWorksWithMap(t *testing.T) {
// data := nodeComponentTestModel{
// StringMap: map[string]string{
// "Key 1": "Value 1",
// "Key 2": "Value 2",
// },
// }
//
// template := stringutils.TrimMargin(
// `<div data-g-define="my-template">
// |<li g-outer-repeat="[[ .StringMap ]]" g-inner-text="Key: {{ .Key }}, Value: {{ .Value }}"></li>
// |</div>`)
// expectation := stringutils.TrimMargin(
// `<div>
// |<li>Key: Key 1, Value: Value 1</li><li>Key: Key 2, Value: Value 2</li>
// |</div>`)
// parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
//}
// Tests that when using g-outer-repeat and g-if the g-if is evaluated first
func TestGOuterRepeatWorksWithIf(t *testing.T) {
data := nodeComponentTestModel{
BooleanValue: true,
BooleanValue2: false,
StringSlice: []string{"1", "2", "Special chars should be escaped & < > ' \""},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<li g-outer-repeat="[[ .StringSlice ]]" g-inner-text="[[ . ]]"
| g-if="[[ .BooleanValue]]"></li>
|<div g-outer-repeat="[[ .StringSlice ]]" g-inner-text="[[ . ]]"
| g-if="[[ .BooleanValue2]]"></div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|<li>1</li><li>2</li><li>Special chars should be escaped & < > ' "</li>
|
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
//********************************* g-use ******************************************************************************
//**********************************************************************************************************************
func TestGUse(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-use="my-image">Hello World</div>
|<div data-g-use="my-image">Hello World</div>
|</div>
|<img src="dummy" g-define="my-image" />
|`)
expectation := stringutils.TrimMargin(
`<div>
|<img src="dummy" />
|<img src="dummy" />
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
// todo: describe that the outer has higher priority here than the g-use
func TestGUseWorksWithOuterIgnore(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-use="my-image" g-ignore="outer">Hello World</div>
|</div>
|<img src="dummy" g-define="my-image" />
|`)
expectation := stringutils.TrimMargin(
`<div>
|
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
//********************************* g-with *****************************************************************************
//**********************************************************************************************************************
func TestThatGTransWorks(t *testing.T) {
data := nodeComponentTestModel{
StringValue: "p",
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-trans="p">Hello World</div>
|<div data-g-trans="p">Hello World</div>
|<div data-g-trans="[[ .StringValue ]]">Hello World</div>
|<div data-g-trans="{{ .StringValue }}">Hello World</div>
|`)
expectation := stringutils.TrimMargin(
`<div>
|<p>Hello World</p>
|<p>Hello World</p>
|<p>Hello World</p>
|<p>Hello World</p>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
//********************************* g-with *****************************************************************************
//**********************************************************************************************************************
func TestThatGWithIsEvaluatedBeforeGText(t *testing.T) {
data := nodeComponentTestModel{
StringValue: "Not the correct one",
NestedModel: nodeComponentNestedModel{
StringValue: "This should be used",
},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-inner-text="[[ .StringValue ]]" g-with="[[ .NestedModel ]]"></div>
|<div g-inner-text="[[ .StringValue ]]" data-g-with="[[ .NestedModel ]]"></div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|<div>This should be used</div>
|<div>This should be used</div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
func TestThatGWithWorksWithGUse(t *testing.T) {
data := nodeComponentTestModel{
StringValue: "Not the correct one",
NestedModel: nodeComponentNestedModel{
StringValue: "/image.png",
},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-use="my-image" g-with="[[ .NestedModel ]]"></div>
|</div>
|<img src="[[ .StringValue ]]" g-define="my-image" />`)
expectation := stringutils.TrimMargin(
`<div>
|<img src="/image.png" />
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
// Tests, that g-with works with g-outer-repeat. G width must be evaluated first.
func TestThatGWithWorksWithGOuterRepeat(t *testing.T) {
data := nodeComponentTestModel{
StringValue: "Not the correct one",
StringSlice: []string{"wrong", "wrong", "wrong"},
NestedModel: nodeComponentNestedModel{
StringSlice: []string{"/image.png", "/image2.png", "/image3.png"},
},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-use="my-image" g-with="[[ .NestedModel ]]" g-outer-repeat="[[ .StringSlice ]]"></div>
|</div>
|<img src="[[ . ]]" g-define="my-image" />`)
expectation := stringutils.TrimMargin(
`<div>
|<img src="/image.png" /><img src="/image2.png" /><img src="/image3.png" />
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}
//********************************* Support functions*******************************************************************
//**********************************************************************************************************************
func parseRenderAndCompareTemplate(e *Engine, template string, templateRef TemplateRef,
data any, expected string, t *testing.T) {
var tl *templateLoaderString
if e == nil {
e, _, tl = initTestEngine()
} else {
// In our tests we always use the default loader and registry
tr := e.registry.(*TemplateRegistryDefault)
tl = tr.lazyTemplateLoader.(*templateLoaderString)
}
if err := tl.LoadFromString(template, ""); err != nil {
t.Error("Error parsing template: ", err)
return
}
renderAndCompareTemplate(e, templateRef, data, true, expected, t)
}
func parseRenderAndCompareTemplateUnescaped(e *Engine, template string, templateRef TemplateRef,
data any, expected string, t *testing.T) {
var tl *templateLoaderString
if e == nil {
e, _, tl = initTestEngine()
} else {
// In our tests we always use the default loader and registry
tr := e.registry.(*TemplateRegistryDefault)
tl = tr.lazyTemplateLoader.(*templateLoaderString)
}
if err := tl.LoadFromString(template, "dummy"); err != nil {
t.Error("Error parsing template: ", err)
return
}
renderAndCompareTemplate(e, templateRef, data, false, expected, t)
}
func renderAndCompareTemplate(e *Engine, templateRef TemplateRef, data any, escaped bool, expected string, t *testing.T) {
tpl, err := e.registry.GetTemplate(templateRef)
if err != nil {
t.Errorf("Expected template '%s' to exist, got error: %v", templateRef, err)
return
}
c, err := tpl.NewRenderable(data)
if err != nil {
t.Error("Error creating component", err)
}
renderAndCompare(e, c, escaped, expected, t)
}
func renderAndCompare(e *Engine, c Renderable, escaped bool, expected string, t *testing.T) {
buf := new(bytes.Buffer)
// Create a RenderContext from the Engine instance for rendering
// Uses context.Background() since tests don't need request cancellation
renderCtx := e.NewRenderContext(context.Background())
renderCtx.Escaped = escaped
err := c.Render(renderCtx, buf, RenderOuter)
if err != nil {
t.Error("Error rendering node component", err)
}
result := buf.String()
compareStrings(result, expected, t)
}
func initTestEngine() (*Engine, *TemplateRegistryDefault, *templateLoaderString) {
tr := NewTemplateRegistryDefault()
tl := newTemplateLoaderString(tr)
tr.SetLazyTemplateLoader(tl)
e, _ := New(
WithCustomRegistry(tr),
WithDeterministicOutput(true),
)
return e, tr, tl
}
func initTestEngineWithMaxNestingDepth(maxDepth int) (*Engine, *TemplateRegistryDefault, *templateLoaderString) {
tr := NewTemplateRegistryDefault()
tl := newTemplateLoaderString(tr)
tr.SetLazyTemplateLoader(tl)
e, _ := New(
WithCustomRegistry(tr),
WithMaxNestingDepth(maxDepth),
WithDeterministicOutput(true),
)
return e, tr, tl
}
// ********************************* Max Nesting Depth Tests **********************************************************************
// TestCircularTemplateReferenceDetection tests that circular template references
// are detected and result in a MaxNestingDepthExceededError instead of stack overflow.
func TestCircularTemplateReferenceDetection(t *testing.T) {
// Template A uses B, and B uses A - this creates a circular reference
template := stringutils.TrimMargin(
`<div data-g-define="template-a">
| <span>A uses B:</span>
| <div g-use="template-b"></div>
|</div>
|<div data-g-define="template-b">
| <span>B uses A:</span>
| <div g-use="template-a"></div>
|</div>`)
g, _, tl := initTestEngineWithMaxNestingDepth(10) // Use low limit for faster test
if err := tl.LoadFromString(template, ""); err != nil {
t.Error("Error parsing template: ", err)
return
}
buf := new(bytes.Buffer)
err := g.renderInternal(context.Background(), buf, "template-a", nil, nil, true)
if err == nil {
t.Error("Expected MaxNestingDepthExceededError for circular reference, but got no error")
return
}
// Check that we got the right error type
var maxDepthErr *MaxNestingDepthExceededError
if !errors.As(err, &maxDepthErr) {
t.Errorf("Expected MaxNestingDepthExceededError, got: %T - %v", err, err)
return
}
if maxDepthErr.MaxDepth != 10 {
t.Errorf("Expected MaxDepth to be 10, got %d", maxDepthErr.MaxDepth)
}
}
// TestSelfReferenceDetection tests that a template referencing itself is detected.
func TestSelfReferenceDetection(t *testing.T) {
// Template A uses itself - immediate circular reference
template := stringutils.TrimMargin(
`<div data-g-define="self-ref">
| <span>I use myself:</span>
| <div g-use="self-ref"></div>
|</div>`)
g, _, tl := initTestEngineWithMaxNestingDepth(5)
if err := tl.LoadFromString(template, ""); err != nil {
t.Error("Error parsing template: ", err)
return
}
buf := new(bytes.Buffer)
err := g.renderInternal(context.Background(), buf, "self-ref", nil, nil, true)
if err == nil {
t.Error("Expected MaxNestingDepthExceededError for self-reference, but got no error")
return
}
var maxDepthErr *MaxNestingDepthExceededError
if !errors.As(err, &maxDepthErr) {
t.Errorf("Expected MaxNestingDepthExceededError, got: %T - %v", err, err)
}
}
// TestDeepButValidNesting tests that deep but non-circular nesting works correctly.
func TestDeepButValidNesting(t *testing.T) {
// Chain: level1 -> level2 -> level3 (no circular reference)
template := stringutils.TrimMargin(
`<div data-g-define="level1">
| <span>Level 1</span>
| <div g-use="level2"></div>
|</div>
|<div data-g-define="level2">
| <span>Level 2</span>
| <div g-use="level3"></div>
|</div>
|<div data-g-define="level3">
| <span>Level 3</span>
|</div>`)
expected := stringutils.TrimMargin(
`<div>
| <span>Level 1</span>
| <div>
| <span>Level 2</span>
| <div>
| <span>Level 3</span>
|</div>
|</div>
|</div>`)
g, _, tl := initTestEngineWithMaxNestingDepth(10)