-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtemplate_test.go
More file actions
322 lines (271 loc) · 8.1 KB
/
template_test.go
File metadata and controls
322 lines (271 loc) · 8.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
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
package xlog
import (
"embed"
"html/template"
"os"
"path/filepath"
"strings"
"testing"
)
//go:embed testdata/custom_templates
var customTemplatesFS embed.FS
func TestRegisterTemplate(t *testing.T) {
// Store original state to restore after test
originalFSs := templatesFSs
defer func() { templatesFSs = originalFSs }()
// Reset templatesFSs
templatesFSs = nil
// Test registering a custom template filesystem
RegisterTemplate(customTemplatesFS, "testdata/custom_templates")
if len(templatesFSs) != 1 {
t.Errorf("Expected 1 template filesystem registered, got %d", len(templatesFSs))
}
// Test registering multiple filesystems
RegisterTemplate(customTemplatesFS, "testdata/custom_templates")
if len(templatesFSs) != 2 {
t.Errorf("Expected 2 template filesystems registered, got %d", len(templatesFSs))
}
}
func TestCompileTemplates(t *testing.T) {
// Store original state
originalTemplates := templates
originalFSs := templatesFSs
defer func() {
templates = originalTemplates
templatesFSs = originalFSs
}()
// Reset state
templatesFSs = nil
templates = nil
// Compile templates
compileTemplates()
if templates == nil {
t.Fatal("Expected templates to be initialized after compileTemplates()")
}
// Check that default templates are loaded
defaultTemplateNames := []string{
"layout",
"page",
"navbar",
"pages",
"pages-grid",
"commands",
"emoji-favicon",
}
for _, name := range defaultTemplateNames {
if templates.Lookup(name) == nil {
t.Errorf("Expected default template '%s' to be compiled", name)
}
}
}
func TestCompileTemplatesWithThemeDirectory(t *testing.T) {
// Create a temporary theme directory
tmpDir := t.TempDir()
themeDir := filepath.Join(tmpDir, "theme")
if err := os.Mkdir(themeDir, 0755); err != nil {
t.Fatalf("Failed to create theme directory: %v", err)
}
// Create a custom template in the theme directory
customTemplate := `<div>Custom Theme Template</div>`
customTemplatePath := filepath.Join(themeDir, "custom.html")
if err := os.WriteFile(customTemplatePath, []byte(customTemplate), 0644); err != nil {
t.Fatalf("Failed to write custom template: %v", err)
}
// Change to the temporary directory
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get working directory: %v", err)
}
defer os.Chdir(originalWd)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to change directory: %v", err)
}
// Store original state
originalTemplates := templates
originalFSs := templatesFSs
defer func() {
templates = originalTemplates
templatesFSs = originalFSs
}()
// Reset state
templatesFSs = nil
templates = nil
// Compile templates (should include theme directory)
compileTemplates()
// Verify custom template was loaded
if templates.Lookup("custom") == nil {
t.Error("Expected custom template from theme directory to be compiled")
}
}
func TestCompileTemplatesOverride(t *testing.T) {
// Store original state
originalTemplates := templates
originalFSs := templatesFSs
defer func() {
templates = originalTemplates
templatesFSs = originalFSs
}()
// Reset state
templatesFSs = nil
templates = nil
// First compilation with default templates
compileTemplates()
// Get the original navbar template
navbarTemplate := templates.Lookup("navbar")
if navbarTemplate == nil {
t.Fatal("navbar template not found in default templates")
}
// Note: In a real override test, we would register a custom filesystem
// with an overriding template, but for this test we're just verifying
// that the latest registered templates take precedence (tested by order)
}
func TestPartial(t *testing.T) {
// Store original state
originalTemplates := templates
originalConfig := Config
defer func() {
templates = originalTemplates
Config = originalConfig
}()
// Compile templates to ensure they're available
compileTemplates()
tests := []struct {
name string
templatePath string
data Locals
shouldContain string
shouldError bool
}{
{
name: "Simple template rendering",
templatePath: "emoji-favicon",
data: Locals{"page": &page{name: "test"}},
shouldContain: "",
shouldError: false,
},
{
name: "Non-existent template",
templatePath: "nonexistent-template",
data: nil,
shouldContain: "template nonexistent-template not found",
shouldError: true,
},
{
name: "Nil data should create empty Locals",
templatePath: "emoji-favicon",
data: nil,
shouldContain: "",
shouldError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Partial(tt.templatePath, tt.data)
resultStr := string(result)
if tt.shouldError {
if !strings.Contains(resultStr, tt.shouldContain) {
t.Errorf("Expected error message to contain '%s', got: %s", tt.shouldContain, resultStr)
}
} else {
if strings.Contains(resultStr, "rendering error") {
t.Errorf("Unexpected rendering error: %s", resultStr)
}
}
})
}
}
func TestPartialWithConfig(t *testing.T) {
// Store original state
originalTemplates := templates
originalConfig := Config
defer func() {
templates = originalTemplates
Config = originalConfig
}()
// Set a test config value
Config = Configuration{
Sitename: "Test Site",
}
// Create a simple test template
templates = template.New("")
testTemplate := `Site: {{.config.Sitename}}`
template.Must(templates.New("test-config").Parse(testTemplate))
// Test that config is passed to template
result := Partial("test-config", Locals{})
resultStr := string(result)
if !strings.Contains(resultStr, "Test Site") {
t.Errorf("Expected template to have access to config, got: %s", resultStr)
}
}
func TestPartialDataMerging(t *testing.T) {
// Store original state
originalTemplates := templates
originalConfig := Config
defer func() {
templates = originalTemplates
Config = originalConfig
}()
// Set a test config
Config = Configuration{
Sitename: "Test",
}
// Create a test template that uses both custom data and config
templates = template.New("")
testTemplate := `Name: {{.name}}, Site: {{.config.Sitename}}`
template.Must(templates.New("test-merge").Parse(testTemplate))
// Test that custom data and config are both available
result := Partial("test-merge", Locals{"name": "TestPage"})
resultStr := string(result)
if !strings.Contains(resultStr, "TestPage") {
t.Errorf("Expected custom data to be available, got: %s", resultStr)
}
if !strings.Contains(resultStr, "Test") {
t.Errorf("Expected config to be available, got: %s", resultStr)
}
}
func TestPartialTemplateError(t *testing.T) {
// Store original state
originalTemplates := templates
originalConfig := Config
defer func() {
templates = originalTemplates
Config = originalConfig
}()
// Create a template that will fail during execution
// We'll use a template that tries to call a method on a nil value
templates = template.New("")
// Create a custom function that will panic
panicFunc := func() string {
panic("intentional test panic")
}
funcMap := template.FuncMap{
"panicFunc": panicFunc,
}
badTemplate := `{{panicFunc}}`
template.Must(templates.New("test-error").Funcs(funcMap).Parse(badTemplate))
// The Partial function catches panics/errors and returns them as strings
result := Partial("test-error", Locals{})
resultStr := string(result)
// The template execution should fail and return an error message
if !strings.Contains(resultStr, "rendering error") {
t.Errorf("Expected rendering error message, got: %s", resultStr)
}
}
func TestTemplateHelpers(t *testing.T) {
// Store original state
originalTemplates := templates
defer func() {
templates = originalTemplates
}()
// Compile templates (which includes helpers)
compileTemplates()
// Create a test template that uses a helper function
testTemplate := `{{base "/path/to/file.md"}}`
template.Must(templates.New("test-helper").Funcs(helpers).Parse(testTemplate))
result := Partial("test-helper", Locals{})
resultStr := string(result)
// The base helper should extract just the filename
if !strings.Contains(resultStr, "file.md") {
t.Errorf("Expected helper function to work, got: %s", resultStr)
}
}