-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode_test.go
More file actions
44 lines (36 loc) · 1 KB
/
unicode_test.go
File metadata and controls
44 lines (36 loc) · 1 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
package tpl_test
import (
"context"
"testing"
"github.com/KarpelesLab/tpl"
)
func TestUnicodeFilter(t *testing.T) {
tests := []struct {
name string
template string
vars map[string]any
expected string
}{
{"unicode_length", `{{_val|unicode()|length()}}`, map[string]any{"_val": "ABC"}, "3"},
{"unicode_single", `{{_val|unicode()|length()}}`, map[string]any{"_val": "A"}, "1"},
{"unicode_multibyte", `{{_val|unicode()|length()}}`, map[string]any{"_val": "日本"}, "2"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
engine := tpl.New()
engine.Raw.TemplateData["main"] = tt.template
ctx := context.Background()
ctx = tpl.ValuesCtx(ctx, tt.vars)
if err := engine.Compile(ctx); err != nil {
t.Fatalf("Compile failed: %v", err)
}
result, err := engine.ParseAndReturn(ctx, "main")
if err != nil {
t.Fatalf("ParseAndReturn failed: %v", err)
}
if result != tt.expected {
t.Errorf("got %q, want %q", result, tt.expected)
}
})
}
}