-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_condition_test.go
More file actions
1240 lines (1125 loc) · 29.9 KB
/
Copy patheval_condition_test.go
File metadata and controls
1240 lines (1125 loc) · 29.9 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 vuego_test
import (
"bytes"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
)
// TestEvalCondition_SimpleBooleanVariables covers the fallback path
// where expr fails to parse and stack.Resolve is used
func TestEvalCondition_SimpleBooleanVariables(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="show">Visible</div>
<div v-if="hide">Hidden</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
}{
{
"show is true",
map[string]any{"show": true, "hide": false},
"<div>Visible</div>",
},
{
"show is false",
map[string]any{"show": false, "hide": false},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
require.Contains(t, buf.String(), tt.expect)
})
}
}
// TestEvalCondition_NegatedVariables covers the negation operator
// when used with simple variable references (fallback path)
func TestEvalCondition_NegatedVariables(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="!disabled">Enabled</div>
<div v-if="!show">Hidden</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
}{
{
"disabled is false",
map[string]any{"disabled": false, "show": false},
"<div>Enabled</div>\n<div>Hidden</div>",
},
{
"disabled is true",
map[string]any{"disabled": true, "show": true},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
if tt.expect != "" {
require.Contains(t, output, tt.expect)
} else {
require.NotContains(t, output, "Enabled")
require.NotContains(t, output, "Hidden")
}
})
}
}
// TestEvalCondition_UndefinedVariables covers the path where
// stack.Resolve returns false (variable not found)
func TestEvalCondition_UndefinedVariables(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="undefined">Should not appear</div>
<div v-if="defined">Should appear</div>
`)},
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", map[string]any{"defined": true})
require.NoError(t, err)
output := buf.String()
require.NotContains(t, output, "Should not appear")
require.Contains(t, output, "Should appear")
}
// TestEvalCondition_FalsyValues covers different falsy values
// when using simple variable fallback
func TestEvalCondition_FalsyValues(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="bool_false">bool false</div>
<div v-if="int_zero">int zero</div>
<div v-if="empty_string">empty string</div>
<div v-if="nil_value">nil value</div>
`)},
}
data := map[string]any{
"bool_false": false,
"int_zero": 0,
"empty_string": "",
"nil_value": nil,
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
output := buf.String()
// All should be falsy and not rendered
require.NotContains(t, output, "bool false")
require.NotContains(t, output, "int zero")
require.NotContains(t, output, "empty string")
require.NotContains(t, output, "nil value")
}
// TestEvalCondition_TruthyValues covers different truthy values
func TestEvalCondition_TruthyValues(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="bool_true">bool true</div>
<div v-if="int_one">int one</div>
<div v-if="string_text">string text</div>
`)},
}
data := map[string]any{
"bool_true": true,
"int_one": 1,
"string_text": "hello",
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, "bool true")
require.Contains(t, output, "int one")
require.Contains(t, output, "string text")
}
// TestEvalCondition_ExpressionFallback tests the case where expr evaluation
// fails and we fall back to simple variable resolution
func TestEvalCondition_ExpressionFallback(t *testing.T) {
// Create a scenario where the condition would fail expr evaluation
// but work with simple variable fallback
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="message">Message exists</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect bool
}{
{
"variable is truthy string",
map[string]any{"message": "Hello"},
true,
},
{
"variable is empty string",
map[string]any{"message": ""},
false,
},
{
"variable missing",
map[string]any{},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
if tt.expect {
require.Contains(t, output, "Message exists")
} else {
require.NotContains(t, output, "Message exists")
}
})
}
}
// TestEvalCondition_WithStructFields tests fallback resolution with struct fields
func TestEvalCondition_WithStructFields(t *testing.T) {
type User struct {
Active bool `json:"active"`
Name string `json:"name"`
}
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="active">User is active</div>
<div v-if="name">User has name</div>
`)},
}
data := User{
Active: true,
Name: "Alice",
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, "User is active")
require.Contains(t, output, "User has name")
}
// TestEvalCondition_StackLookupPath tests the stack.Lookup function directly
// by testing variable resolution from nested stack scopes
func TestEvalCondition_StackLookupPath(t *testing.T) {
// This tests stack.Lookup which is used in stack.Resolve
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<ul v-for="item in items">
<li v-if="item.name">Item: {{ item.name }}</li>
</ul>
`)},
}
data := map[string]any{
"items": []map[string]any{
{"name": "First"},
{"name": "Second"},
{"name": ""},
},
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, "Item: First")
require.Contains(t, output, "Item: Second")
// Empty string should not render the <li> element
lines := bytes.Split([]byte(output), []byte("<li"))
require.Len(t, lines, 3) // header + 2 items with <li (empty string item doesn't get <li>)
}
// TestEvalCondition_ExprCompilationFailureFallback tests the fallback path
// where expr compilation fails and stack.Resolve is used
func TestEvalCondition_ExprCompilationFailureFallback(t *testing.T) {
// Test with invalid expr syntax that will fail compilation
// and should fall back to variable resolution
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="show | extra">Should render if show is truthy</div>
<div v-if="hide |">Should not render (syntax will fail)</div>
`)},
}
data := map[string]any{
"show": true,
"hide": true,
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
output := buf.String()
// When expr fails, it falls back to simple Resolve which doesn't handle pipes
// "show | extra" will fail expr compilation and then "show | extra" won't resolve
// So the first div should not render
// "hide |" will fail expr compilation and then "hide |" won't resolve as a variable name
require.NotContains(t, output, "Should render")
require.NotContains(t, output, "Should not render")
}
// TestEvalCondition_ResolveFailsFallbackToFalse tests that when both
// expr evaluation and stack.Resolve fail, we return false
func TestEvalCondition_ResolveFailsFallbackToFalse(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="completely_undefined">Should not appear</div>
<div v-if="defined">Should appear</div>
`)},
}
data := map[string]any{
"defined": true,
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
output := buf.String()
// "completely_undefined" is neither in env nor resolvable
require.NotContains(t, output, "Should not appear")
require.Contains(t, output, "Should appear")
}
// TestEvalCondition_SuccessfulResolve tests stack.Resolve fallback behavior
// by testing with function calls that fail in expr but should fall back to variable resolution
func TestEvalCondition_SuccessfulResolve(t *testing.T) {
// Note: The final return statement (line 33 in eval_condition.go) that uses stack.Resolve
// is practically unreachable with the current expr evaluator.
// This is because expr.Compile with AllowUndefinedVariables() accepts almost all valid syntax,
// and expr.Run with undefined variables returns nil rather than error.
// The fallback was left in for backward compatibility but may be dead code.
//
// We still test the paths that ARE reachable:
// 1. Expr evaluation succeeds (line 20-22) - covered by most tests
// 2. Syntax errors cause expr to fail (line 27-31) - covered by TestEvalCondition_ExprCompilationFailureFallback
// Test that when expr succeeds, IsTruthy is applied to the result
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="message">{{ message }}</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
}{
{
"truthy non-empty string",
map[string]any{"message": "Hello"},
"Hello",
},
{
"truthy number",
map[string]any{"message": 42},
"42",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
require.Contains(t, buf.String(), tt.expect)
})
}
}
// TestElse_BasicElseDirective tests basic v-else directive
func TestElse_BasicElseDirective(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="show">Visible</div>
<div v-else>Hidden</div>
`)},
}
tests := []struct {
name string
data map[string]any
expectShow bool
expectElse bool
}{
{
"show is true",
map[string]any{"show": true},
true,
false,
},
{
"show is false",
map[string]any{"show": false},
false,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
if tt.expectShow {
require.Contains(t, output, "Visible")
require.NotContains(t, output, "Hidden")
} else {
require.NotContains(t, output, "Visible")
require.Contains(t, output, "Hidden")
}
})
}
}
// TestElse_MultipleElseIfDirective tests v-else-if chains
func TestElse_MultipleElseIfDirective(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="status == 'active'">Active</div>
<div v-else-if="status == 'pending'">Pending</div>
<div v-else-if="status == 'inactive'">Inactive</div>
<div v-else>Unknown</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
}{
{
"status is active",
map[string]any{"status": "active"},
"Active",
},
{
"status is pending",
map[string]any{"status": "pending"},
"Pending",
},
{
"status is inactive",
map[string]any{"status": "inactive"},
"Inactive",
},
{
"status is unknown",
map[string]any{"status": "unknown"},
"Unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, tt.expect)
// Verify other conditions don't render
if tt.expect != "Active" {
require.NotContains(t, output, "Active")
}
if tt.expect != "Pending" {
require.NotContains(t, output, "Pending")
}
if tt.expect != "Inactive" {
require.NotContains(t, output, "Inactive")
}
if tt.expect != "Unknown" {
require.NotContains(t, output, "Unknown")
}
})
}
}
// TestElse_ElseFollowingVFor tests v-else following v-for
// When v-else follows v-for with no items, it should render
func TestElse_ElseFollowingVFor(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<ul>
<li v-for="item in items">{{ item }}</li>
<li v-else>No items</li>
</ul>
`)},
}
tests := []struct {
name string
data map[string]any
expectItem bool
expectElse bool
}{
{
"items exist",
map[string]any{"items": []string{"a", "b", "c"}},
true,
false,
},
{
"items empty",
map[string]any{"items": []string{}},
false,
true,
},
{
"items missing",
map[string]any{},
false,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
if tt.expectItem {
require.Contains(t, output, "<li>a</li>")
require.Contains(t, output, "<li>b</li>")
require.Contains(t, output, "<li>c</li>")
require.NotContains(t, output, "No items")
} else {
require.Contains(t, output, "No items")
}
})
}
}
// TestElse_VIfAndVElseIfWithoutVElse tests chains that don't have v-else
func TestElse_VIfAndVElseIfWithoutVElse(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="show">Show</div>
<div v-else-if="alternate">Alternate</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
}{
{
"show is true",
map[string]any{"show": true, "alternate": true},
"Show",
},
{
"show is false, alternate is true",
map[string]any{"show": false, "alternate": true},
"Alternate",
},
{
"both false",
map[string]any{"show": false, "alternate": false},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
if tt.expect == "" {
require.NotContains(t, output, "Show")
require.NotContains(t, output, "Alternate")
} else {
require.Contains(t, output, tt.expect)
}
})
}
}
// TestElse_NestedVIfWithVElse tests nested v-if chains
func TestElse_NestedVIfWithVElse(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="outer">
<div v-if="inner">Inner True</div>
<div v-else>Inner False</div>
</div>
<div v-else>Outer False</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect []string
}{
{
"outer true, inner true",
map[string]any{"outer": true, "inner": true},
[]string{"Inner True"},
},
{
"outer true, inner false",
map[string]any{"outer": true, "inner": false},
[]string{"Inner False"},
},
{
"outer false",
map[string]any{"outer": false, "inner": true},
[]string{"Outer False"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
for _, exp := range tt.expect {
require.Contains(t, output, exp)
}
})
}
}
// TestElse_VElseIfWithComplexConditions tests v-else-if with complex expressions
// Note: Comparison operators (>, <, ==, etc) in conditions are not yet implemented
// This test documents the expected behavior when they are implemented.
func TestElse_VElseIfWithComplexConditions(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="$env.count > 10">Large</div>
<div v-else-if="$env.count > 5">Medium</div>
<div v-else-if="$env.count > 0">Small</div>
<div v-else>None</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
}{
{
"count 20",
map[string]any{"count": 20},
"Large",
},
{
"count 8",
map[string]any{"count": 8},
"Medium",
},
{
"count 2",
map[string]any{"count": 2},
"Small",
},
{
"count 0",
map[string]any{"count": 0},
"None",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
require.Contains(t, buf.String(), tt.expect)
})
}
}
// TestElse_VElseWithInterpolation tests that v-else preserves node content
func TestElse_VElseWithInterpolation(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="has_name">Hello {{ name }}</div>
<div v-else>Hello Guest</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
}{
{
"name provided",
map[string]any{"has_name": true, "name": "Alice"},
"Hello Alice",
},
{
"name not provided",
map[string]any{"has_name": false, "name": ""},
"Hello Guest",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
require.Contains(t, buf.String(), tt.expect)
})
}
}
// TestElse_StandaloneVElseIfError tests that v-else-if without v-if is ignored
func TestElse_StandaloneVElseIfError(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-else-if="condition">Should not appear</div>
<div>Should appear</div>
`)},
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", map[string]any{"condition": true})
require.NoError(t, err)
output := buf.String()
// v-else-if without v-if should be skipped
require.NotContains(t, output, "Should not appear")
require.Contains(t, output, "Should appear")
}
// TestElse_StandaloneVElseError tests that v-else without v-if is ignored
func TestElse_StandaloneVElseError(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-else>Should not appear</div>
<div>Should appear</div>
`)},
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", map[string]any{})
require.NoError(t, err)
output := buf.String()
// v-else without v-if should be skipped
require.NotContains(t, output, "Should not appear")
require.Contains(t, output, "Should appear")
}
// TestEvaluateNodeAsElement_WithVFor tests evaluateNodeAsElement with v-for directive
func TestEvaluateNodeAsElement_WithVFor(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="show">
<span v-for="item in items">{{ item }}</span>
</div>
<div v-else>No items</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect []string
notExp string
}{
{
"show is true with items",
map[string]any{"show": true, "items": []string{"a", "b", "c"}},
[]string{"<span>a</span>", "<span>b</span>", "<span>c</span>"},
"No items",
},
{
"show is true with empty items",
map[string]any{"show": true, "items": []string{}},
[]string{},
"No items",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
for _, exp := range tt.expect {
require.Contains(t, output, exp)
}
if tt.notExp != "" {
require.NotContains(t, output, tt.notExp)
}
})
}
}
// TestEvaluateNodeAsElement_WithVHtml tests evaluateNodeAsElement with v-html directive
func TestEvaluateNodeAsElement_WithVHtml(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="show" v-html="content"></div>
<div v-else>No content</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
notExp string
}{
{
"show is true with html content",
map[string]any{"show": true, "content": "<strong>Bold</strong>"},
"<strong>Bold</strong>",
"No content",
},
{
"show is true with empty content",
map[string]any{"show": true, "content": ""},
"",
"No content",
},
{
"show is false",
map[string]any{"show": false, "content": "<strong>Bold</strong>"},
"No content",
"<strong>Bold</strong>",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
if tt.expect != "" {
require.Contains(t, output, tt.expect)
}
if tt.notExp != "" {
require.NotContains(t, output, tt.notExp)
}
})
}
}
// TestEvaluateNodeAsElement_ElseIfWithVFor tests v-else-if followed by v-for
func TestEvaluateNodeAsElement_ElseIfWithVFor(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="status == 'active'">Active status</div>
<div v-else-if="status == 'pending'">
<span v-for="task in tasks">{{ task }}</span>
</div>
<div v-else>Other status</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
notExp string
}{
{
"status is active",
map[string]any{"status": "active", "tasks": []string{"task1", "task2"}},
"Active status",
"task1",
},
{
"status is pending with tasks",
map[string]any{"status": "pending", "tasks": []string{"task1", "task2"}},
"<span>task1</span>",
"Active status",
},
{
"status is other",
map[string]any{"status": "other", "tasks": []string{"task1"}},
"Other status",
"task1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, tt.expect)
require.NotContains(t, output, tt.notExp)
})
}
}
// TestEvaluateNodeAsElement_ElseWithVHtml tests v-else with v-html
func TestEvaluateNodeAsElement_ElseWithVHtml(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="show">Normal content</div>
<div v-else v-html="fallback"></div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
notExp string
}{
{
"show is true",
map[string]any{"show": true, "fallback": "<em>Fallback</em>"},
"Normal content",
"<em>Fallback</em>",
},
{
"show is false",
map[string]any{"show": false, "fallback": "<em>Fallback</em>"},
"<em>Fallback</em>",
"Normal content",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
output := buf.String()
require.Contains(t, output, tt.expect)
require.NotContains(t, output, tt.notExp)
})
}
}
// TestEvaluateNodeAsElement_ChainedElseIfWithAttributes tests v-if/v-else-if chains with various attributes
func TestEvaluateNodeAsElement_ChainedElseIfWithAttributes(t *testing.T) {
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="level == 1" id="one">Level One</div>
<div v-else-if="level == 2" class="two">Level Two</div>
<div v-else-if="level == 3" data-test="three">Level Three</div>
<div v-else>Default</div>
`)},
}
tests := []struct {
name string
data map[string]any
expect string
notExp string
}{
{
"level 1",