Repository navigation
Expand file tree
/
Copy pathmiya_coverage_test.go
More file actions
566 lines (471 loc) · 13.4 KB
/
Copy pathmiya_coverage_test.go
File metadata and controls
566 lines (471 loc) · 13.4 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package miya
import (
"reflect"
"testing"
"github.com/zipreport/miya/parser"
"github.com/zipreport/miya/runtime"
)
// TestContextFunctions tests context-related functions
func TestContextFunctions(t *testing.T) {
t.Run("Push and Pop", func(t *testing.T) {
ctx := NewContext()
ctx.Set("outer", "value1")
// Push a new scope - returns new context
innerCtx := ctx.Push()
innerCtx.Set("inner", "value2")
// Inner scope should see both
inner, ok := innerCtx.Get("inner")
if !ok || inner != "value2" {
t.Error("Expected to find inner variable")
}
outer, ok := innerCtx.Get("outer")
if !ok || outer != "value1" {
t.Error("Expected to find outer variable in inner scope")
}
// Pop the scope - returns parent context
outerCtx := innerCtx.Pop()
// Should still find outer in parent
outer, ok = outerCtx.Get("outer")
if !ok || outer != "value1" {
t.Error("Expected to still find outer variable")
}
})
t.Run("GetEnv", func(t *testing.T) {
env := NewEnvironment()
ctx := newContextWithEnv(env)
retrievedEnv := ctx.GetEnv()
if retrievedEnv != env {
t.Error("Expected GetEnv to return the environment")
}
})
t.Run("String representation on concrete type", func(t *testing.T) {
ctx := newContextWithEnv(nil)
ctx.Set("name", "test")
ctx.Set("count", 42)
// Access concrete type for String() method
if concreteCtx, ok := ctx.(*context); ok {
str := concreteCtx.String()
if str == "" {
t.Error("Expected non-empty string representation")
}
}
})
t.Run("Clone preserves data", func(t *testing.T) {
ctx := NewContext()
ctx.Set("key", "value")
cloned := ctx.Clone()
val, ok := cloned.Get("key")
if !ok || val != "value" {
t.Error("Expected clone to have the same data")
}
// Modifying clone should not affect original
cloned.Set("key", "modified")
original, _ := ctx.Get("key")
if original != "value" {
t.Error("Modifying clone should not affect original")
}
})
}
// TestTemplateContextAdapter tests the TemplateContextAdapter
func TestTemplateContextAdapter(t *testing.T) {
t.Run("NewTemplateContextAdapter", func(t *testing.T) {
ctx := NewContext()
env := NewEnvironment()
adapter := NewTemplateContextAdapter(ctx, env)
if adapter == nil {
t.Fatal("Expected non-nil adapter")
}
})
t.Run("All method", func(t *testing.T) {
ctx := NewContext()
ctx.Set("key1", "value1")
ctx.Set("key2", "value2")
env := NewEnvironment()
adapter := NewTemplateContextAdapter(ctx, env)
all := adapter.All()
if all == nil {
t.Fatal("Expected non-nil map from All()")
}
if all["key1"] != "value1" || all["key2"] != "value2" {
t.Error("All() should return all context variables")
}
})
t.Run("ApplyTest", func(t *testing.T) {
ctx := NewContext()
env := NewEnvironment()
adapter := NewTemplateContextAdapter(ctx, env)
// Test a simple test
result, err := adapter.ApplyTest("defined", "hello")
if err != nil {
t.Fatalf("ApplyTest failed: %v", err)
}
if result != true {
t.Error("Expected 'defined' test to return true for non-nil value")
}
})
}
// TestTemplateMethods tests Template methods
func TestTemplateMethods(t *testing.T) {
env := NewEnvironment()
tmpl, err := env.FromString("Hello {{ name }}!")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
t.Run("Source", func(t *testing.T) {
source := tmpl.Source()
if source != "Hello {{ name }}!" {
t.Errorf("Expected source 'Hello {{ name }}!', got %q", source)
}
})
t.Run("AST", func(t *testing.T) {
ast := tmpl.AST()
if ast == nil {
t.Error("Expected non-nil AST")
}
})
t.Run("GetASTAsTemplateNode", func(t *testing.T) {
templateNode := tmpl.GetASTAsTemplateNode()
if templateNode == nil {
t.Error("Expected non-nil TemplateNode")
}
})
t.Run("SetAST", func(t *testing.T) {
newAST := &parser.TemplateNode{
Name: "test",
Children: []parser.Node{},
}
tmpl.SetAST(newAST)
if tmpl.AST() != newAST {
t.Error("Expected AST to be updated")
}
})
}
// TestEnvironmentMethods tests additional Environment methods
func TestEnvironmentMethods(t *testing.T) {
t.Run("AddTest and GetTest", func(t *testing.T) {
env := NewEnvironment()
customTest := func(value interface{}, args ...interface{}) (bool, error) {
return value == "custom", nil
}
err := env.AddTest("mycustomtest", customTest)
if err != nil {
t.Fatalf("AddTest failed: %v", err)
}
test, ok := env.GetTest("mycustomtest")
if !ok {
t.Error("Expected to find custom test")
}
if test == nil {
t.Error("Expected non-nil test function")
}
// Test functionality
result, err := test("custom")
if err != nil {
t.Fatalf("Test function failed: %v", err)
}
if result != true {
t.Error("Expected test to return true for 'custom'")
}
})
t.Run("ListTests", func(t *testing.T) {
env := NewEnvironment()
tests := env.ListTests()
if len(tests) == 0 {
t.Error("Expected at least some built-in tests")
}
// Check for common tests
found := make(map[string]bool)
for _, name := range tests {
found[name] = true
}
if !found["defined"] {
t.Error("Expected 'defined' test to be listed")
}
})
t.Run("ApplyTest", func(t *testing.T) {
env := NewEnvironment()
result, err := env.ApplyTest("defined", "hello")
if err != nil {
t.Fatalf("ApplyTest failed: %v", err)
}
if result != true {
t.Error("Expected 'defined' test to return true")
}
})
t.Run("GetConfig and SetConfig", func(t *testing.T) {
env := NewEnvironment()
env.SetConfig("custom_option", "custom_value")
value, ok := env.GetConfig("custom_option")
if !ok || value != "custom_value" {
t.Errorf("Expected 'custom_value', got %v", value)
}
// Non-existent config
nilValue, ok := env.GetConfig("non_existent")
if ok || nilValue != nil {
t.Error("Expected nil and false for non-existent config")
}
})
t.Run("WithStrictUndefined", func(t *testing.T) {
env := NewEnvironment(WithStrictUndefined(true))
if env.undefinedBehavior != runtime.UndefinedStrict {
t.Error("Expected strict undefined behavior")
}
})
t.Run("WithDebugUndefined", func(t *testing.T) {
env := NewEnvironment(WithDebugUndefined(true))
if env.undefinedBehavior != runtime.UndefinedDebug {
t.Error("Expected debug undefined behavior")
}
})
t.Run("WithUndefinedBehavior", func(t *testing.T) {
env := NewEnvironment(WithUndefinedBehavior(runtime.UndefinedStrict))
if env.undefinedBehavior != runtime.UndefinedStrict {
t.Error("Expected strict undefined behavior")
}
})
}
// TestExtensionMethods tests extension-related methods
func TestExtensionMethods(t *testing.T) {
t.Run("GetExtensionRegistry", func(t *testing.T) {
env := NewEnvironment()
registry := env.GetExtensionRegistry()
if registry == nil {
t.Error("Expected non-nil extension registry")
}
})
t.Run("IsCustomTag returns false for unknown tag", func(t *testing.T) {
env := NewEnvironment()
isCustom := env.IsCustomTag("unknowntag")
if isCustom {
t.Error("Expected false for unknown tag")
}
})
t.Run("GetExtensionForTag returns false for unknown tag", func(t *testing.T) {
env := NewEnvironment()
ext, ok := env.GetExtensionForTag("unknowntag")
if ok || ext != nil {
t.Error("Expected nil and false for unknown tag")
}
})
t.Run("GetExtension returns false for unknown extension", func(t *testing.T) {
env := NewEnvironment()
ext, ok := env.GetExtension("unknownext")
if ok || ext != nil {
t.Error("Expected nil and false for unknown extension")
}
})
}
// TestInheritanceCacheMethods tests inheritance cache methods
func TestInheritanceCacheMethods(t *testing.T) {
t.Run("ClearInheritanceCache", func(t *testing.T) {
env := NewEnvironment()
// Should not panic
env.ClearInheritanceCache()
})
t.Run("InvalidateTemplate", func(t *testing.T) {
env := NewEnvironment()
// Should not panic
env.InvalidateTemplate("test.html")
})
t.Run("GetInheritanceCacheStats", func(t *testing.T) {
env := NewEnvironment()
stats := env.GetInheritanceCacheStats()
// Stats is a struct, just verify it doesn't panic
_ = stats
})
t.Run("ConfigureInheritanceCache", func(t *testing.T) {
env := NewEnvironment()
// Should not panic
env.ConfigureInheritanceCache(500, 30, 10)
})
}
// TestConcurrentSafety tests concurrent safety functions
func TestConcurrentSafety(t *testing.T) {
t.Run("NewThreadSafeTemplate", func(t *testing.T) {
env := NewEnvironment()
tmpl, err := env.FromString("Hello {{ name }}!")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
safeTmpl := NewThreadSafeTemplate(tmpl)
if safeTmpl == nil {
t.Fatal("Expected non-nil thread-safe template")
}
})
t.Run("RenderConcurrent", func(t *testing.T) {
env := NewEnvironment()
tmpl, err := env.FromString("Hello {{ name }}!")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
safeTmpl := NewThreadSafeTemplate(tmpl)
ctx := NewContext()
ctx.Set("name", "World")
result, err := safeTmpl.RenderConcurrent(ctx)
if err != nil {
t.Fatalf("RenderConcurrent failed: %v", err)
}
if result != "Hello World!" {
t.Errorf("Expected 'Hello World!', got %q", result)
}
})
}
// TestTemplateAdapters tests template and environment adapters
func TestTemplateAdapters(t *testing.T) {
t.Run("environmentAdapter GetTemplate", func(t *testing.T) {
env := NewEnvironment()
_, err := env.FromString("test content")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
adapter := &environmentAdapter{env: env}
_, err = adapter.GetTemplate("nonexistent")
// Should error for non-existent template
if err == nil {
t.Log("GetTemplate for non-existent template may return error or nil")
}
})
t.Run("environmentAdapter GetLoader", func(t *testing.T) {
env := NewEnvironment()
adapter := &environmentAdapter{env: env}
loader := adapter.GetLoader()
// May or may not be nil depending on configuration
_ = loader
})
t.Run("templateAdapter methods", func(t *testing.T) {
env := NewEnvironment()
tmpl, err := env.FromString("Hello {{ name }}!")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
adapter := &templateAdapter{template: tmpl}
if adapter.Name() != tmpl.Name() {
t.Error("Expected adapter Name() to match template Name()")
}
ast := adapter.AST()
if ast == nil {
t.Error("Expected non-nil AST from adapter")
}
})
}
// TestContextNestedAccess tests nested attribute access
func TestContextNestedAccess(t *testing.T) {
t.Run("Access nested map attributes", func(t *testing.T) {
ctx := NewContext()
ctx.Set("user", map[string]interface{}{
"name": "Alice",
"address": map[string]interface{}{
"city": "New York",
},
})
// This tests the getLocal and getAttribute functions
user, ok := ctx.Get("user")
if !ok {
t.Fatal("Expected to find user")
}
userMap, ok := user.(map[string]interface{})
if !ok {
t.Fatal("Expected user to be a map")
}
if userMap["name"] != "Alice" {
t.Error("Expected user.name to be Alice")
}
})
t.Run("Access struct attributes via reflection", func(t *testing.T) {
type User struct {
Name string
Email string
}
ctx := NewContext()
ctx.Set("user", User{Name: "Bob", Email: "bob@example.com"})
user, ok := ctx.Get("user")
if !ok {
t.Fatal("Expected to find user")
}
// Access should work for struct
val := reflect.ValueOf(user)
if val.Kind() == reflect.Struct {
nameField := val.FieldByName("Name")
if nameField.IsValid() && nameField.String() != "Bob" {
t.Error("Expected Name field to be Bob")
}
}
})
}
// TestEnvironmentMacros tests macro-related environment methods
func TestEnvironmentMacros(t *testing.T) {
t.Run("GetMacro returns false for undefined macro", func(t *testing.T) {
env := NewEnvironment()
macro, ok := env.GetMacro("undefined_macro")
if ok || macro != nil {
t.Error("Expected nil and false for undefined macro")
}
})
t.Run("ListMacros", func(t *testing.T) {
env := NewEnvironment()
macros := env.ListMacros()
// Should return empty list or nil for no macros
_ = macros
})
t.Run("ClearMacros", func(t *testing.T) {
env := NewEnvironment()
// Should not panic
env.ClearMacros()
})
t.Run("CallMacro calls a compiled macro", func(t *testing.T) {
env := NewEnvironment()
if _, err := env.FromString("{% macro m(x) %}[{{ x }}]{% endmacro %}"); err != nil {
t.Fatal(err)
}
got, err := env.CallMacro("m", NewContext(), []interface{}{"a"}, nil)
if err != nil || got != "[a]" {
t.Fatalf("CallMacro = %q, %v; want \"[a]\", nil", got, err)
}
})
}
// TestCapitalizeFirst tests the capitalizeFirst helper
func TestCapitalizeFirst(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello", "Hello"},
{"Hello", "Hello"},
{"HELLO", "HELLO"},
{"", ""},
{"a", "A"},
{"1abc", "1abc"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := capitalizeFirst(tt.input)
if result != tt.expected {
t.Errorf("capitalizeFirst(%q) = %q, expected %q", tt.input, result, tt.expected)
}
})
}
}
// Benchmark tests
func BenchmarkContextOperations(b *testing.B) {
ctx := NewContext()
b.Run("Set", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ctx.Set("key", "value")
}
})
b.Run("Get", func(b *testing.B) {
ctx.Set("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Get("key")
}
})
b.Run("Clone", func(b *testing.B) {
ctx.Set("key1", "value1")
ctx.Set("key2", "value2")
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Clone()
}
})
}