-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor_loops_test.go
More file actions
313 lines (297 loc) · 8.08 KB
/
Copy pathfor_loops_test.go
File metadata and controls
313 lines (297 loc) · 8.08 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
package jinja
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestForLoop(t *testing.T) {
tests := []struct {
name string
template string
context map[string]interface{}
want string
wantErr bool
}{
{
name: "simple for loop over slice",
template: "{% for item in items %}{{ item }},{% endfor %}",
context: map[string]interface{}{
"items": []interface{}{"a", "b", "c"},
},
want: "a,b,c,",
wantErr: false,
},
{
name: "for loop over empty list",
template: "Items: {% for item in items %}{{ item }},{% endfor %}",
context: map[string]interface{}{
"items": []interface{}{},
},
want: "Items: ",
wantErr: false,
},
{
name: "for loop with loop variable",
template: "{% for i in items %}{{ loop.index }}:{{ i }},{% endfor %}",
context: map[string]interface{}{
"items": []interface{}{10, 20, 30},
},
want: "1:10,2:20,3:30,",
wantErr: false,
},
{
name: "for loop with conditional",
template: "{% for i in items %}{% if loop.index > 1 %},{% endif %}{{ i }}{% endfor %}",
context: map[string]interface{}{
"items": []interface{}{10, 20, 30},
},
want: "10,20,30",
wantErr: false,
},
{
name: "nested for loops",
template: "{% for i in outer %}[{% for j in inner %}{{ i }}-{{ j }}{% if not loop.last %},{% endif %}{% endfor %}]{% endfor %}",
context: map[string]interface{}{
"outer": []interface{}{"a", "b"},
"inner": []interface{}{1, 2, 3},
},
want: "[a-1,a-2,a-3][b-1,b-2,b-3]",
wantErr: false,
},
{
name: "for loop over string",
template: "{% for char in text %}{{ char }}{% endfor %}",
context: map[string]interface{}{
"text": "abc",
},
want: "abc",
wantErr: false,
},
{
name: "for loop over map values",
template: "{% for value in user %}{{ value }}{% if not loop.last %},{% endif %}{% endfor %}",
context: map[string]interface{}{
"user": map[string]interface{}{
"name": "Alice",
"age": 30,
},
},
want: "Alice,30", // Order may vary, so this test is simplified
wantErr: false,
},
{
name: "for loop with object attributes",
template: "{% for user in users %}{{ user.name }} ({{ user.age }}){% if not loop.last %}, {% endif %}{% endfor %}",
context: map[string]interface{}{
"users": []interface{}{
map[string]interface{}{"name": "Alice", "age": 30},
map[string]interface{}{"name": "Bob", "age": 25},
},
},
want: "Alice (30), Bob (25)",
wantErr: false,
},
{
name: "for loop over nil",
template: "{% for item in nil_var %}{{ item }}{% endfor %}",
context: map[string]interface{}{
"nil_var": nil,
},
want: "",
wantErr: false,
},
{
name: "invalid for loop syntax",
template: "{% for item items %}{{ item }}{% endfor %}",
context: map[string]interface{}{},
want: "",
wantErr: true,
},
{
name: "unclosed for loop",
template: "{% for item in items %}{{ item }}",
context: map[string]interface{}{
"items": []interface{}{"a", "b", "c"},
},
want: "",
wantErr: true,
},
{
name: "non-iterable in for loop",
template: "{% for item in number %}{{ item }}{% endfor %}",
context: map[string]interface{}{
"number": 42,
},
want: "",
wantErr: true,
},
{
name: "simple for loop test",
template: "{% for i in [1, 2, 3] %}{{ i }}{% endfor %}",
context: map[string]interface{}{},
want: "123",
wantErr: false,
},
{
name: "basic loop index test",
template: "{% for i in [10, 20, 30] %}i={{ i }} index={{ loop.index }}|{% endfor %}",
context: map[string]interface{}{},
want: "i=10 index=1|i=20 index=2|i=30 index=3|",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := TemplateString(tt.template, tt.context)
if (err != nil) != tt.wantErr {
t.Errorf("TemplateString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
// For map iteration test, the order of values is non-deterministic
// So just check that all expected values are present
if strings.Contains(tt.name, "map values") {
for _, val := range []string{"Alice", "30"} {
if !strings.Contains(got, val) {
t.Errorf("TemplateString() got = %v, expected to contain %v", got, val)
}
}
} else if got != tt.want {
// Debug specific test cases
if tt.name == "for_loop_with_loop_variable" || tt.name == "basic_loop_index_test" {
// Get the loop values directly
testCtx := make(map[string]interface{})
for k, v := range tt.context {
testCtx[k] = v
}
loopVar := map[string]interface{}{
"index": 1,
"last": false,
}
testCtx["loop"] = loopVar
// Try to evaluate loop.index directly
indexVal, err := EvaluateExpression("loop.index", testCtx)
lastVal, err2 := EvaluateExpression("loop.last", testCtx)
t.Logf("Debug - loop.index=%v (err=%v), loop.last=%v (err=%v)",
indexVal, err, lastVal, err2)
// Add some more debugging to check the template rendering
template := "{{ loop.index }}"
result, err := TemplateString(template, testCtx)
t.Logf("Debug - direct template of loop.index = %v (err=%v)", result, err)
}
t.Errorf("TemplateString() got = %v, want %v", got, tt.want)
}
}
})
}
}
func TestForLoopComplex(t *testing.T) {
template := `Users:
{% for user in users %}
Username: {{ user.username }}
Email: {{ user.email }}
Roles: {% for role in user.permissions.roles %}{{ role }}{% if not loop.last %}, {% endif %}{% endfor %}
{% endfor %}`
context := map[string]interface{}{
"users": []interface{}{
map[string]interface{}{
"username": "admin",
"email": "admin@example.com",
"permissions": map[string]interface{}{
"roles": []interface{}{"admin", "user", "editor"},
},
},
map[string]interface{}{
"username": "guest",
"email": "guest@example.com",
"permissions": map[string]interface{}{
"roles": []interface{}{"guest"},
},
},
},
}
expected := `Users:
Username: admin
Email: admin@example.com
Roles: admin, user, editor
Username: guest
Email: guest@example.com
Roles: guest
`
result, err := TemplateString(template, context)
if err != nil {
t.Errorf("TemplateString() failed with error: %v", err)
return
}
if result != expected {
t.Errorf("TemplateString() complex for loop failed:\nGot:\n%s\nWant:\n%s", result, expected)
}
}
func TestLoopIndexEvaluation(t *testing.T) {
testCases := []struct {
name string
expression string
context map[string]interface{}
want interface{}
wantErr bool
}{
{
name: "simple index test",
expression: "loop.index",
context: map[string]interface{}{
"loop": map[string]interface{}{
"index": 1,
},
},
want: 1,
wantErr: false,
},
{
name: "direct integer access",
expression: "val",
context: map[string]interface{}{
"val": 42,
},
want: 42,
wantErr: false,
},
{
name: "direct map value access",
expression: "obj.value",
context: map[string]interface{}{
"obj": map[string]interface{}{
"value": "test",
},
},
want: "test",
wantErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Test EvaluateExpression first
got, err := EvaluateExpression(tc.expression, tc.context)
if (err != nil) != tc.wantErr {
t.Errorf("EvaluateExpression() error = %v, wantErr %v", err, tc.wantErr)
return
}
if !tc.wantErr && !reflect.DeepEqual(got, tc.want) {
t.Errorf("EvaluateExpression() got = %v, want %v", got, tc.want)
}
// Then test TemplateString
template := "{{ " + tc.expression + " }}"
result, err := TemplateString(template, tc.context)
if (err != nil) != tc.wantErr {
t.Errorf("TemplateString() error = %v, wantErr %v", err, tc.wantErr)
return
}
if !tc.wantErr {
expected := fmt.Sprintf("%v", tc.want)
if result != expected {
t.Errorf("TemplateString() got = %v, want %v", result, expected)
}
}
})
}
}