-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate_test.go
129 lines (124 loc) · 2.76 KB
/
template_test.go
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
package template
import (
"testing"
"time"
)
// TestTemplateExecution verifies the correct execution of a template with various node types.
func TestTemplateExecution(t *testing.T) {
ctx := mockUserProfileContext()
// Define test cases
cases := []struct {
name string
nodes []*Node
expected string
}{
{
"TextOnly",
[]*Node{{Type: "text", Text: "Hello, world!"}},
"Hello, world!",
},
{
"SingleVariable",
[]*Node{
{Type: "text", Text: "User: "},
{Type: "variable", Variable: "userName"},
},
"User: JaneDoe",
},
{
"NestedVariable",
[]*Node{
{Type: "variable", Variable: "profile.age"},
},
"29",
},
{
"VariableNotFound",
[]*Node{
{Type: "variable", Variable: "nonexistent", Text: "{{nonexistent}}"},
},
"{{nonexistent}}",
},
{
"MixedContent",
[]*Node{
{Type: "text", Text: "Welcome, "},
{Type: "variable", Variable: "userName"},
{Type: "text", Text: "! Age: "},
{Type: "variable", Variable: "profile.age"},
},
"Welcome, JaneDoe! Age: 29",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Setup template
tmpl := &Template{Nodes: tc.nodes}
// Execute template
result := tmpl.MustExecute(ctx)
if result != tc.expected {
t.Errorf("Expected '%s', got '%s'", tc.expected, result)
}
})
}
}
func TestConvertToString(t *testing.T) {
cases := []struct {
name string
input interface{}
expected string
}{
{
name: "String",
input: "Hello, world!",
expected: "Hello, world!",
},
{
name: "SliceOfString",
input: []string{"apple", "banana", "cherry"},
expected: "[apple, banana, cherry]",
},
{
name: "SliceOfInt",
input: []int{1, 2, 3},
expected: "[1, 2, 3]",
},
{
name: "SliceOfFloat64",
input: []float64{1.1, 2.2, 3.3},
expected: "[1.1, 2.2, 3.3]",
},
{
name: "SliceOfBool",
input: []bool{true, false, true},
expected: "[true, false, true]",
},
{
name: "Time",
input: time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC),
expected: "2020-01-01 12:00:00",
},
{
name: "ComplexTypeWithJSONFallback",
input: map[string]interface{}{"name": "John Doe", "age": 30},
expected: "{\n \"age\": 30,\n \"name\": \"John Doe\"\n}",
},
{
name: "HandleErrorInJSONFallback",
input: make(chan int),
expected: "could not convert value to string: json: unsupported type: chan int",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := convertToString(tc.input)
if err != nil {
if err.Error() != tc.expected {
t.Fatalf("Unexpected error: %v", err)
}
} else if result != tc.expected {
t.Errorf("Expected '%s', got '%s'", tc.expected, result)
}
})
}
}