-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinterpolate_test.go
More file actions
109 lines (103 loc) · 2.73 KB
/
Copy pathinterpolate_test.go
File metadata and controls
109 lines (103 loc) · 2.73 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
package vuego_test
import (
"bytes"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
"github.com/titpetric/vuego/diff"
)
func TestVue_Interpolate(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "simple variable",
template: "<div>{{ name }}</div>",
data: map[string]any{"name": "Alice"},
expected: "<div>Alice</div>",
},
{
name: "missing variable",
template: "<div>{{ missing }}</div>",
data: map[string]any{},
expected: "<div></div>",
},
{
name: "no interpolation",
template: "<div>plain text</div>",
data: map[string]any{},
expected: "<div>plain text</div>",
},
{
name: "html escaping",
template: "<div>{{ value }}</div>",
data: map[string]any{"value": "<script>alert('xss')</script>"},
expected: "<div><script>alert('xss')</script></div>",
},
{
name: "multiple interpolations",
template: "<p>{{ first }} {{ last }}</p>",
data: map[string]any{"first": "John", "last": "Doe"},
expected: "<p>John Doe</p>",
},
{
name: "null value",
template: "<div>{{ value }}</div>",
data: map[string]any{"value": nil},
expected: "<div></div>",
},
{
name: "numeric value",
template: "<div>{{ number }}</div>",
data: map[string]any{"number": 42},
expected: "<div>42</div>",
},
{
name: "boolean value",
template: "<div>{{ flag }}</div>",
data: map[string]any{"flag": true},
expected: "<div>true</div>",
},
{
name: "whitespace in interpolation",
template: "<div>{{ value }}</div>",
data: map[string]any{"value": "test"},
expected: "<div>test</div>",
},
{
name: "ternary operator with true condition",
template: "<div>{{ flag ? 'yes' : 'no' }}</div>",
data: map[string]any{"flag": true},
expected: "<div>yes</div>",
},
{
name: "ternary operator with false condition",
template: "<div>{{ flag ? 'yes' : 'no' }}</div>",
data: map[string]any{"flag": false},
expected: "<div>no</div>",
},
{
name: "ternary operator with comparison",
template: "<div>{{ age > 18 ? 'adult' : 'minor' }}</div>",
data: map[string]any{"age": 25},
expected: "<div>adult</div>",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
templateBytes := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: templateBytes},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}