Repository navigation
Expand file tree
/
Copy pathenvironment_test.go
More file actions
411 lines (346 loc) · 10.7 KB
/
Copy pathenvironment_test.go
File metadata and controls
411 lines (346 loc) · 10.7 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
package miya
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/zipreport/miya/loader"
)
func TestEnvironmentCreation(t *testing.T) {
t.Run("DefaultEnvironment", func(t *testing.T) {
env := NewEnvironment()
if env == nil {
t.Fatal("Expected non-nil environment")
}
// Check default delimiters
blockStart, blockEnd, varStart, varEnd, commentStart, commentEnd := env.GetDelimiters()
if blockStart != "{%" || blockEnd != "%}" {
t.Errorf("Expected block delimiters {%% and %%}, got %s and %s", blockStart, blockEnd)
}
if varStart != "{{" || varEnd != "}}" {
t.Errorf("Expected variable delimiters {{ and }}, got %s and %s", varStart, varEnd)
}
if commentStart != "{#" || commentEnd != "#}" {
t.Errorf("Expected comment delimiters {# and #}, got %s and %s", commentStart, commentEnd)
}
// Check default auto-escape
if !env.IsAutoEscape() {
t.Error("Expected auto-escape to be enabled by default")
}
})
t.Run("EnvironmentWithOptions", func(t *testing.T) {
stringLoader := loader.NewStringLoader(nil)
env := NewEnvironment(
WithLoader(stringLoader),
WithAutoEscape(false),
WithTrimBlocks(true),
WithLstripBlocks(true),
)
if env.GetLoader() != stringLoader {
t.Error("Expected loader to be set")
}
if env.IsAutoEscape() {
t.Error("Expected auto-escape to be disabled")
}
if !env.trimBlocks {
t.Error("Expected trim blocks to be enabled")
}
if !env.lstripBlocks {
t.Error("Expected lstrip blocks to be enabled")
}
})
}
func TestEnvironmentDelimiters(t *testing.T) {
env := NewEnvironment()
// Test setting custom delimiters
env.SetDelimiters("<$", "$>", "<%", "%>")
env.SetCommentDelimiters("<!", "!>")
blockStart, blockEnd, varStart, varEnd, commentStart, commentEnd := env.GetDelimiters()
if blockStart != "<%" || blockEnd != "%>" {
t.Errorf("Expected block delimiters <%% and %%>, got %s and %s", blockStart, blockEnd)
}
if varStart != "<$" || varEnd != "$>" {
t.Errorf("Expected variable delimiters <$ and $>, got %s and %s", varStart, varEnd)
}
if commentStart != "<!" || commentEnd != "!>" {
t.Errorf("Expected comment delimiters <! and !>, got %s and %s", commentStart, commentEnd)
}
}
func TestEnvironmentFilters(t *testing.T) {
env := NewEnvironment()
// Test adding custom filter
customFilter := func(value interface{}, args ...interface{}) (interface{}, error) {
return strings.ToUpper(value.(string)), nil
}
err := env.AddFilter("uppercase", customFilter)
if err != nil {
t.Fatalf("Failed to add filter: %v", err)
}
// Test retrieving filter
retrievedFilter, ok := env.GetFilter("uppercase")
if !ok {
t.Error("Expected to find custom filter")
}
if retrievedFilter == nil {
t.Error("Expected non-nil filter")
}
// Test applying filter
result, err := env.ApplyFilter("uppercase", "hello")
if err != nil {
t.Fatalf("Failed to apply filter: %v", err)
}
if result != "HELLO" {
t.Errorf("Expected 'HELLO', got '%v'", result)
}
// Test listing filters (should include built-ins plus our custom one)
filters := env.ListFilters()
found := false
for _, name := range filters {
if name == "uppercase" {
found = true
break
}
}
if !found {
t.Error("Expected to find 'uppercase' filter in list")
}
}
func TestEnvironmentGlobals(t *testing.T) {
env := NewEnvironment()
// Test adding globals
env.AddGlobal("site_name", "My Website")
env.AddGlobal("version", "1.0.0")
// Globals should be accessible in context
ctx := newContextWithEnv(env)
siteName, ok := ctx.Get("site_name")
if !ok {
t.Error("Expected to find site_name global")
}
if siteName != "My Website" {
t.Errorf("Expected 'My Website', got '%v'", siteName)
}
version, ok := ctx.Get("version")
if !ok {
t.Error("Expected to find version global")
}
if version != "1.0.0" {
t.Errorf("Expected '1.0.0', got '%v'", version)
}
}
func TestEnvironmentFromString(t *testing.T) {
env := NewEnvironment()
t.Run("SimpleTemplate", func(t *testing.T) {
// Note: This test will work once we have a working parser
template, err := env.FromString("Hello World")
if err != nil {
t.Fatalf("Failed to create template from string: %v", err)
}
if template.Name() != "<string>" {
t.Errorf("Expected template name '<string>', got '%s'", template.Name())
}
})
t.Run("InvalidTemplate", func(t *testing.T) {
// Test with malformed template syntax
// Note: This will pass for now since we don't have full lexer/parser implementation
_, err := env.FromString("{{ unclosed variable")
if err != nil {
t.Logf("Got expected error for malformed template: %v", err)
} else {
t.Log("No error for malformed template (expected with current simple implementation)")
}
})
}
func TestEnvironmentWithLoader(t *testing.T) {
// Create temporary directory for test templates
tempDir := t.TempDir()
// Create test template file
templateContent := "Hello {{ name }}!"
templatePath := filepath.Join(tempDir, "greeting.html")
err := os.WriteFile(templatePath, []byte(templateContent), 0644)
if err != nil {
t.Fatalf("Failed to create test template: %v", err)
}
// Create environment with filesystem loader
fsLoader := loader.NewFileSystemLoader([]string{tempDir}, nil)
env := NewEnvironment(WithLoader(fsLoader))
t.Run("GetTemplate", func(t *testing.T) {
template, err := env.GetTemplate("greeting.html")
if err != nil {
t.Fatalf("Failed to get template: %v", err)
}
if template.Name() != "greeting.html" {
t.Errorf("Expected template name 'greeting.html', got '%s'", template.Name())
}
})
t.Run("GetNonExistentTemplate", func(t *testing.T) {
_, err := env.GetTemplate("nonexistent.html")
if err == nil {
t.Error("Expected error when getting non-existent template")
}
})
t.Run("ListTemplates", func(t *testing.T) {
templates, err := env.ListTemplates()
if err != nil {
t.Fatalf("Failed to list templates: %v", err)
}
found := false
for _, name := range templates {
if name == "greeting.html" {
found = true
break
}
}
if !found {
t.Error("Expected to find 'greeting.html' in template list")
}
})
}
func TestEnvironmentCache(t *testing.T) {
env := NewEnvironment()
// Create some templates via FromString (auto-cached with hash keys)
template1, _ := env.FromString("Template 1")
template2, _ := env.FromString("Template 2")
// Verify templates are created
_ = template1
_ = template2
t.Run("CacheSize", func(t *testing.T) {
// After Phase 2 optimization, FromString auto-caches with hash keys
// So we should have 2 entries (one per unique template string)
size := env.GetCacheSize()
if size != 2 {
t.Errorf("Expected cache size 2, got %d", size)
}
})
t.Run("ClearCache", func(t *testing.T) {
env.ClearCache()
size := env.GetCacheSize()
if size != 0 {
t.Errorf("Expected cache size 0 after clear, got %d", size)
}
})
}
func TestEnvironmentRenderString(t *testing.T) {
env := NewEnvironment()
ctx := NewContextFrom(map[string]interface{}{
"name": "World",
})
// Note: This test requires working lexer/parser
result, err := env.RenderString("Hello World", ctx)
if err != nil {
t.Fatalf("Failed to render string: %v", err)
}
// For now, without working parser, it should just return the source
if result != "Hello World" {
t.Errorf("Expected 'Hello World', got '%s'", result)
}
}
func TestEnvironmentRenderTemplate(t *testing.T) {
// Create temporary directory for test templates
tempDir := t.TempDir()
// Create test template file
templateContent := "Hello World"
templatePath := filepath.Join(tempDir, "simple.html")
err := os.WriteFile(templatePath, []byte(templateContent), 0644)
if err != nil {
t.Fatalf("Failed to create test template: %v", err)
}
// Create environment with filesystem loader
fsLoader := loader.NewFileSystemLoader([]string{tempDir}, nil)
env := NewEnvironment(WithLoader(fsLoader))
ctx := NewContextFrom(map[string]interface{}{
"name": "World",
})
result, err := env.RenderTemplate("simple.html", ctx)
if err != nil {
t.Fatalf("Failed to render template: %v", err)
}
if result != "Hello World" {
t.Errorf("Expected 'Hello World', got '%s'", result)
}
}
func TestGlobalFunctions(t *testing.T) {
// Test global convenience functions
stringLoader := loader.NewStringLoader(nil)
stringLoader.AddTemplate("global_test.html", "Hello Global")
SetDefaultLoader(stringLoader)
t.Run("FromString", func(t *testing.T) {
template, err := FromString("Global FromString")
if err != nil {
t.Fatalf("Failed to create template from string: %v", err)
}
if template.Name() != "<string>" {
t.Errorf("Expected '<string>', got '%s'", template.Name())
}
})
t.Run("GetTemplate", func(t *testing.T) {
template, err := GetTemplate("global_test.html")
if err != nil {
t.Fatalf("Failed to get template: %v", err)
}
if template.Name() != "global_test.html" {
t.Errorf("Expected 'global_test.html', got '%s'", template.Name())
}
})
t.Run("RenderString", func(t *testing.T) {
ctx := NewContextFrom(map[string]interface{}{
"name": "Global",
})
result, err := RenderString("Hello Global", ctx)
if err != nil {
t.Fatalf("Failed to render string: %v", err)
}
if result != "Hello Global" {
t.Errorf("Expected 'Hello Global', got '%s'", result)
}
})
t.Run("RenderTemplate", func(t *testing.T) {
ctx := NewContextFrom(map[string]interface{}{
"name": "Global",
})
result, err := RenderTemplate("global_test.html", ctx)
if err != nil {
t.Fatalf("Failed to render template: %v", err)
}
if result != "Hello Global" {
t.Errorf("Expected 'Hello Global', got '%s'", result)
}
})
}
func TestEnvironmentAutoEscape(t *testing.T) {
env := NewEnvironment(WithAutoEscape(true))
ctx := NewContextFrom(map[string]interface{}{
"html": "<script>alert('xss')</script>",
})
// Test with auto-escape enabled - using variables
result, err := env.RenderString("{{ html }}", ctx)
if err != nil {
t.Fatalf("Failed to render: %v", err)
}
// Should be HTML escaped
expected := "<script>alert('xss')</script>"
if result != expected {
t.Errorf("Expected HTML escaped output, got '%s'", result)
}
// Test with auto-escape disabled
env.SetAutoEscape(false)
result, err = env.RenderString("{{ html }}", ctx)
if err != nil {
t.Fatalf("Failed to render: %v", err)
}
// Should not be escaped
if result != "<script>alert('xss')</script>" {
t.Errorf("Expected unescaped output, got '%s'", result)
}
}
// Mock template parser implementation
func (env *Environment) GetDelimiters() (blockStart, blockEnd, varStart, varEnd, commentStart, commentEnd string) {
return env.blockStartString, env.blockEndString,
env.varStartString, env.varEndString,
env.commentStartString, env.commentEndString
}
func (env *Environment) IsAutoEscape() bool {
return env.autoEscape
}
func (env *Environment) SetAutoEscape(enabled bool) {
env.autoEscape = enabled
}