forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_test.go
250 lines (188 loc) · 5.93 KB
/
registry_test.go
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
package template
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
func checkRegistryFuncs(t *testing.T, r *Registry, expectedFuncs ...string) {
if v := len(r.funcs); v != len(expectedFuncs) {
t.Fatalf("Expected total %d funcs, got %d", len(expectedFuncs), v)
}
for _, name := range expectedFuncs {
if _, ok := r.funcs[name]; !ok {
t.Fatalf("Missing %q func", name)
}
}
}
func TestNewRegistry(t *testing.T) {
r := NewRegistry()
if r.cache == nil {
t.Fatalf("Expected cache store to be initialized, got nil")
}
if v := r.cache.Length(); v != 0 {
t.Fatalf("Expected cache store length to be 0, got %d", v)
}
checkRegistryFuncs(t, r, "raw")
}
func TestRegistryAddFuncs(t *testing.T) {
r := NewRegistry()
r.AddFuncs(map[string]any{
"test": func(a string) string { return a + "-TEST" },
})
checkRegistryFuncs(t, r, "raw", "test")
result, err := r.LoadString(`{{.|test}}`).Render("example")
if err != nil {
t.Fatalf("Unexpected Render() error, got %v", err)
}
expected := "example-TEST"
if result != expected {
t.Fatalf("Expected Render() result %q, got %q", expected, result)
}
}
func TestRegistryLoadFiles(t *testing.T) {
r := NewRegistry()
t.Run("invalid or missing files", func(t *testing.T) {
r.LoadFiles("file1.missing", "file2.missing")
key := "file1.missing,file2.missing"
renderer := r.cache.Get(key)
if renderer == nil {
t.Fatal("Expected renderer to be initialized even if invalid, got nil")
}
if renderer.template != nil {
t.Fatalf("Expected renderer template to be nil, got %v", renderer.template)
}
if renderer.parseError == nil {
t.Fatalf("Expected renderer parseError to be set, got nil")
}
})
t.Run("valid files", func(t *testing.T) {
// create test templates
dir, err := os.MkdirTemp(os.TempDir(), "template_test")
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "base.html"), []byte(`Base:{{template "content" .}}`), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "content.html"), []byte(`{{define "content"}}Content:{{.|raw}}{{end}}`), 0644); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
files := []string{filepath.Join(dir, "base.html"), filepath.Join(dir, "content.html")}
r.LoadFiles(files...)
renderer := r.cache.Get(strings.Join(files, ","))
if renderer == nil {
t.Fatal("Expected renderer to be initialized even if invalid, got nil")
}
if renderer.template == nil {
t.Fatal("Expected renderer template to be set, got nil")
}
if renderer.parseError != nil {
t.Fatalf("Expected renderer parseError to be nil, got %v", renderer.parseError)
}
result, err := renderer.Render("<h1>123</h1>")
if err != nil {
t.Fatalf("Unexpected Render() error, got %v", err)
}
expected := "Base:Content:<h1>123</h1>"
if result != expected {
t.Fatalf("Expected Render() result %q, got %q", expected, result)
}
})
}
func TestRegistryLoadString(t *testing.T) {
r := NewRegistry()
t.Run("invalid template string", func(t *testing.T) {
txt := `test {{define "content"}}`
r.LoadString(txt)
renderer := r.cache.Get(txt)
if renderer == nil {
t.Fatal("Expected renderer to be initialized even if invalid, got nil")
}
if renderer.template != nil {
t.Fatalf("Expected renderer template to be nil, got %v", renderer.template)
}
if renderer.parseError == nil {
t.Fatalf("Expected renderer parseError to be set, got nil")
}
})
t.Run("valid template string", func(t *testing.T) {
txt := `test {{.|raw}}`
r.LoadString(txt)
renderer := r.cache.Get(txt)
if renderer == nil {
t.Fatal("Expected renderer to be initialized even if invalid, got nil")
}
if renderer.template == nil {
t.Fatal("Expected renderer template to be set, got nil")
}
if renderer.parseError != nil {
t.Fatalf("Expected renderer parseError to be nil, got %v", renderer.parseError)
}
result, err := renderer.Render("<h1>123</h1>")
if err != nil {
t.Fatalf("Unexpected Render() error, got %v", err)
}
expected := "test <h1>123</h1>"
if result != expected {
t.Fatalf("Expected Render() result %q, got %q", expected, result)
}
})
}
func TestRegistryLoadFS(t *testing.T) {
r := NewRegistry()
t.Run("invalid fs", func(t *testing.T) {
fs := os.DirFS("__missing__")
files := []string{"missing1", "missing2"}
key := fmt.Sprintf("%v%v", fs, files)
r.LoadFS(fs, files...)
renderer := r.cache.Get(key)
if renderer == nil {
t.Fatal("Expected renderer to be initialized even if invalid, got nil")
}
if renderer.template != nil {
t.Fatalf("Expected renderer template to be nil, got %v", renderer.template)
}
if renderer.parseError == nil {
t.Fatalf("Expected renderer parseError to be set, got nil")
}
})
t.Run("valid fs", func(t *testing.T) {
// create test templates
dir, err := os.MkdirTemp(os.TempDir(), "template_test2")
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "base.html"), []byte(`Base:{{template "content" .}}`), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "content.html"), []byte(`{{define "content"}}Content:{{.|raw}}{{end}}`), 0644); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
fs := os.DirFS(dir)
files := []string{"base.html", "content.html"}
key := fmt.Sprintf("%v%v", fs, files)
r.LoadFS(fs, files...)
renderer := r.cache.Get(key)
if renderer == nil {
t.Fatal("Expected renderer to be initialized even if invalid, got nil")
}
if renderer.template == nil {
t.Fatal("Expected renderer template to be set, got nil")
}
if renderer.parseError != nil {
t.Fatalf("Expected renderer parseError to be nil, got %v", renderer.parseError)
}
result, err := renderer.Render("<h1>123</h1>")
if err != nil {
t.Fatalf("Unexpected Render() error, got %v", err)
}
expected := "Base:Content:<h1>123</h1>"
if result != expected {
t.Fatalf("Expected Render() result %q, got %q", expected, result)
}
})
}