-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_structures_test.go
More file actions
325 lines (308 loc) · 8.88 KB
/
control_structures_test.go
File metadata and controls
325 lines (308 loc) · 8.88 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
package twig
import (
"strings"
"testing"
)
// TestSimpleControlStructures tests simple control structures (if, for)
func TestSimpleControlStructures(t *testing.T) {
engine := New()
tests := []struct {
name string
source string
context map[string]interface{}
expected string
}{
// If statements
{
name: "Simple if (true)",
source: "{% if true %}yes{% endif %}",
context: nil,
expected: "yes",
},
{
name: "Simple if (false)",
source: "{% if false %}yes{% endif %}",
context: nil,
expected: "",
},
{
name: "If-else (true)",
source: "{% if true %}yes{% else %}no{% endif %}",
context: nil,
expected: "yes",
},
{
name: "If-else (false)",
source: "{% if false %}yes{% else %}no{% endif %}",
context: nil,
expected: "no",
},
{
name: "If-elseif-else (first true)",
source: "{% if true %}1{% elseif true %}2{% else %}3{% endif %}",
context: nil,
expected: "1",
},
{
name: "If-elseif-else (second true)",
source: "{% if false %}1{% elseif true %}2{% else %}3{% endif %}",
context: nil,
expected: "2",
},
{
name: "If-elseif-else (all false)",
source: "{% if false %}1{% elseif false %}2{% else %}3{% endif %}",
context: nil,
expected: "3",
},
{
name: "Nested if statements",
source: "{% if true %}outer{% if false %}inner-if{% else %}inner-else{% endif %}{% endif %}",
context: nil,
expected: "outerinner-else",
},
{
name: "If with variable condition",
source: "{% if value %}yes{% else %}no{% endif %}",
context: map[string]interface{}{"value": true},
expected: "yes",
},
{
name: "If with complex condition (and)",
source: "{% if value and otherValue %}yes{% else %}no{% endif %}",
context: map[string]interface{}{"value": true, "otherValue": true},
expected: "yes",
},
{
name: "If with complex condition (or)",
source: "{% if value or otherValue %}yes{% else %}no{% endif %}",
context: map[string]interface{}{"value": false, "otherValue": true},
expected: "yes",
},
{
name: "If with is test",
source: "{% if value is defined %}yes{% else %}no{% endif %}",
context: map[string]interface{}{"value": "something"},
expected: "yes",
},
{
name: "If with complex condition (parentheses)",
source: "{% if (a or b) and c %}yes{% else %}no{% endif %}",
context: map[string]interface{}{"a": true, "b": false, "c": true},
expected: "yes",
},
// For loops
{
name: "For loop with array",
source: "{% for item in items %}{{ item }}{% endfor %}",
context: map[string]interface{}{"items": []string{"a", "b", "c"}},
expected: "abc",
},
{
name: "For loop with array literal",
source: "{% for item in ['a', 'b', 'c'] %}{{ item }}{% endfor %}",
context: nil,
expected: "abc",
},
{
name: "For loop with map",
source: "{% for key, value in data %}{{ key }}:{{ value }};{% endfor %}",
context: map[string]interface{}{"data": map[string]interface{}{"a": 1, "b": 2, "c": 3}},
expected: "a:1;b:2;c:3;", // This is just a reference; actual map iteration order is checked separately
},
{
name: "For loop with loop variable",
source: "{% for item in items %}{{ loop.index }}:{{ item }};{% endfor %}",
context: map[string]interface{}{"items": []string{"a", "b", "c"}},
expected: "1:a;2:b;3:c;",
},
{
name: "Nested for loops",
source: "{% for i in items %}{% for j in items %}({{ i }},{{ j }}){% endfor %}{% endfor %}",
context: map[string]interface{}{"items": []string{"a", "b"}},
expected: "(a,a)(a,b)(b,a)(b,b)",
},
{
name: "For loop with if condition",
source: "{% for item in items %}{% if loop.first %}first:{% endif %}{{ item }}{% endfor %}",
context: map[string]interface{}{"items": []string{"a", "b", "c"}},
expected: "first:abc",
},
{
name: "For loop with else (non-empty)",
source: "{% for item in items %}{{ item }}{% else %}empty{% endfor %}",
context: map[string]interface{}{"items": []string{"a", "b", "c"}},
expected: "abc",
},
{
name: "For loop with else (empty)",
source: "{% for item in items %}{{ item }}{% else %}empty{% endfor %}",
context: map[string]interface{}{"items": []string{}},
expected: "empty",
},
{
name: "For loop with array access",
source: "{% for i in range(0, 2) %}{{ items[i] }}{% endfor %}",
context: map[string]interface{}{"items": []string{"a", "b", "c"}},
expected: "abc",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := engine.RegisterString("test", tt.source)
if err != nil {
t.Fatalf("Error registering template: %v", err)
}
result, err := engine.Render("test", tt.context)
if err != nil {
t.Fatalf("Error rendering template: %v", err)
}
// Special handling for map iteration which is non-deterministic in Go
if tt.name == "For loop with map" {
// Check that the result contains all the expected key-value pairs
expectedPairs := []string{"a:1;", "b:2;", "c:3;"}
for _, pair := range expectedPairs {
if !strings.Contains(result, pair) {
t.Errorf("Expected result to contain %q, but got: %q", pair, result)
}
}
} else if result != tt.expected {
t.Errorf("Expected: %q, Got: %q", tt.expected, result)
}
})
}
}
// TestRangeFunctionInForLoop tests the range function directly in a for loop
func TestRangeFunctionInForLoop(t *testing.T) {
engine := New()
// Test the actual rendering
tests := []struct {
name string
source string
expected string
}{
{
name: "Simple range function in for loop",
source: "{% for i in range(1, 3) %}{{ i }}{% endfor %}",
expected: "123",
},
{
name: "Range function with step in for loop",
source: "{% for i in range(1, 9, 2) %}{{ i }}{% endfor %}",
expected: "13579",
},
{
name: "Range function with loop variable",
source: "{% for i in range(1, 3) %}{{ loop.index }}:{{ i }};{% endfor %}",
expected: "1:1;2:2;3:3;",
},
{
name: "Range function with direct negative start",
source: "{% for i in range(-3, 0) %}{{ i }}{% endfor %}",
expected: "-3-2-10",
},
{
name: "Range function with parenthesized negative start",
source: "{% for i in range((-5), 0) %}{{ i }}{% endfor %}",
expected: "-5-4-3-2-10",
},
{
name: "Range with negative end value",
source: "{% for i in range(0, -3, -1) %}{{ i }}{% endfor %}",
expected: "0-1-2-3",
},
{
name: "Range with complex negative literals",
source: "{% for i in range((-10), (-5)) %}{{ i }}{% endfor %}",
expected: "-10-9-8-7-6-5",
},
{
name: "Range with arithmetic expressions for bounds",
source: "{% for i in range(0-5, 0) %}{{ i }}{% endfor %}",
expected: "-5-4-3-2-10",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := engine.RegisterString("test", tt.source)
if err != nil {
t.Fatalf("Error registering template: %v", err)
}
result, err := engine.Render("test", nil)
if err != nil {
t.Fatalf("Error rendering template: %v", err)
}
if result != tt.expected {
t.Errorf("Expected: %q, Got: %q", tt.expected, result)
}
})
}
}
// TestConditionalExpressions tests conditional expressions (ternary)
func TestConditionalExpressions(t *testing.T) {
engine := New()
tests := []struct {
name string
source string
context map[string]interface{}
expected string
}{
{
name: "Simple ternary (true)",
source: "{{ true ? 'yes' : 'no' }}",
context: nil,
expected: "yes",
},
{
name: "Simple ternary (false)",
source: "{{ false ? 'yes' : 'no' }}",
context: nil,
expected: "no",
},
{
name: "Ternary with variable",
source: "{{ value ? 'yes' : 'no' }}",
context: map[string]interface{}{"value": true},
expected: "yes",
},
{
name: "Ternary with complex condition",
source: "{{ (a or b) and c ? 'yes' : 'no' }}",
context: map[string]interface{}{"a": true, "b": false, "c": true},
expected: "yes",
},
{
name: "Nested ternary (outer true)",
source: "{{ true ? (true ? '1' : '2') : '3' }}",
context: nil,
expected: "1",
},
{
name: "Nested ternary (outer false)",
source: "{{ false ? '1' : (true ? '2' : '3') }}",
context: nil,
expected: "2",
},
{
name: "Ternary with arithmetic",
source: "{{ 5 > 2 ? 5 + 3 : 2 - 1 }}",
context: nil,
expected: "8",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := engine.RegisterString("test", tt.source)
if err != nil {
t.Fatalf("Error registering template: %v", err)
}
result, err := engine.Render("test", tt.context)
if err != nil {
t.Fatalf("Error rendering template: %v", err)
}
if result != tt.expected {
t.Errorf("Expected: %q, Got: %q", tt.expected, result)
}
})
}
}