-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtemplate_html_go_consistency_test.go
More file actions
236 lines (196 loc) · 6.87 KB
/
Copy pathtemplate_html_go_consistency_test.go
File metadata and controls
236 lines (196 loc) · 6.87 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
package xun
import (
"html/template"
"strings"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
)
// TestBlockBehaviorVsGo verifies that xun's {{block}} behavior matches Go's standard
func TestBlockBehaviorVsGo(t *testing.T) {
t.Run("block without definition uses default", func(t *testing.T) {
// Go standard: {{block "opt" .}}default{{end}} renders "default"
fsys := fstest.MapFS{
"layouts/test.html": &fstest.MapFile{
Data: []byte(`{{block "optional" .}}default content{{end}}`),
},
"pages/test.html": &fstest.MapFile{
Data: []byte(`<!--layout:test-->`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
layout := NewHtmlTemplate("layouts/test", "layouts/test.html")
err := layout.Load(fsys, templates, fm)
require.NoError(t, err)
templates["layouts/test"] = layout
page := NewHtmlTemplate("pages/test", "pages/test.html")
err = page.Load(fsys, templates, fm)
require.NoError(t, err)
var buf strings.Builder
err = page.Execute(&buf, nil)
require.NoError(t, err)
require.Equal(t, "default content", buf.String())
})
t.Run("block with define uses custom", func(t *testing.T) {
// Go standard: define overrides block
fsys := fstest.MapFS{
"layouts/test.html": &fstest.MapFile{
Data: []byte(`{{block "optional" .}}default{{end}}`),
},
"pages/test.html": &fstest.MapFile{
Data: []byte(`<!--layout:test-->{{define "optional"}}custom{{end}}`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
layout := NewHtmlTemplate("layouts/test", "layouts/test.html")
err := layout.Load(fsys, templates, fm)
require.NoError(t, err)
templates["layouts/test"] = layout
page := NewHtmlTemplate("pages/test", "pages/test.html")
err = page.Load(fsys, templates, fm)
require.NoError(t, err)
var buf strings.Builder
err = page.Execute(&buf, nil)
require.NoError(t, err)
require.Equal(t, "custom", buf.String())
})
}
// TestTemplateBehaviorVsGo verifies that xun's {{template}} behavior matches Go's standard
func TestTemplateBehaviorVsGo(t *testing.T) {
t.Run("template without definition fails", func(t *testing.T) {
// Go standard: {{template "missing"}} causes runtime error
fsys := fstest.MapFS{
"pages/test.html": &fstest.MapFile{
Data: []byte(`{{template "missing" .}}`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
page := NewHtmlTemplate("pages/test", "pages/test.html")
err := page.Load(fsys, templates, fm)
require.NoError(t, err) // Parse succeeds
var buf strings.Builder
err = page.Execute(&buf, nil)
require.Error(t, err) // Execute fails
require.Contains(t, err.Error(), "missing")
})
t.Run("template with define works", func(t *testing.T) {
// Go standard: {{template}} with {{define}} works
fsys := fstest.MapFS{
"pages/test.html": &fstest.MapFile{
Data: []byte(`{{define "header"}}Header{{end}}{{template "header" .}}`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
page := NewHtmlTemplate("pages/test", "pages/test.html")
err := page.Load(fsys, templates, fm)
require.NoError(t, err)
var buf strings.Builder
err = page.Execute(&buf, nil)
require.NoError(t, err)
require.Equal(t, "Header", buf.String())
})
}
// TestDependencyDetection verifies that Templates() returns all defined templates
func TestDependencyDetection(t *testing.T) {
t.Run("block creates stub in Templates()", func(t *testing.T) {
// Go standard: {{block "name"}} creates "name" in Templates()
fsys := fstest.MapFS{
"layouts/test.html": &fstest.MapFile{
Data: []byte(`{{block "opt" .}}default{{end}}`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
layout := NewHtmlTemplate("layouts/test", "layouts/test.html")
err := layout.Load(fsys, templates, fm)
require.NoError(t, err)
// Check that "opt" is in dependencies (detected via Templates())
require.Contains(t, layout.dependencies, "opt")
})
t.Run("define creates entry in Templates()", func(t *testing.T) {
// Go standard: {{define "name"}} creates "name" in Templates()
fsys := fstest.MapFS{
"pages/test.html": &fstest.MapFile{
Data: []byte(`{{define "header"}}H{{end}}{{define "footer"}}F{{end}}`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
page := NewHtmlTemplate("pages/test", "pages/test.html")
err := page.Load(fsys, templates, fm)
require.NoError(t, err)
// Check that both defines are detected
require.Contains(t, page.dependencies, "header")
require.Contains(t, page.dependencies, "footer")
})
t.Run("template call does not create stub", func(t *testing.T) {
// Go standard: {{template "name"}} does NOT create stub
fsys := fstest.MapFS{
"pages/test.html": &fstest.MapFile{
Data: []byte(`{{template "missing" .}}`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
page := NewHtmlTemplate("pages/test", "pages/test.html")
err := page.Load(fsys, templates, fm)
require.NoError(t, err)
// "missing" should NOT be in dependencies
require.NotContains(t, page.dependencies, "missing")
})
}
// TestBlockAndTemplateConsistency ensures block and template behave differently as expected
func TestBlockAndTemplateConsistency(t *testing.T) {
fsys := fstest.MapFS{
"layouts/test.html": &fstest.MapFile{
Data: []byte(`
{{block "blockName" .}}block default{{end}}
{{template "templateName" .}}
`),
},
"pages/with-both.html": &fstest.MapFile{
Data: []byte(`<!--layout:test-->
{{define "blockName"}}block custom{{end}}
{{define "templateName"}}template custom{{end}}
`),
},
"pages/without-either.html": &fstest.MapFile{
Data: []byte(`<!--layout:test-->`),
},
}
templates := make(map[string]*HtmlTemplate)
fm := template.FuncMap{}
layout := NewHtmlTemplate("layouts/test", "layouts/test.html")
err := layout.Load(fsys, templates, fm)
require.NoError(t, err)
templates["layouts/test"] = layout
t.Run("with both definitions", func(t *testing.T) {
page := NewHtmlTemplate("pages/with-both", "pages/with-both.html")
err := page.Load(fsys, templates, fm)
require.NoError(t, err)
var buf strings.Builder
err = page.Execute(&buf, nil)
require.NoError(t, err)
require.Contains(t, buf.String(), "block custom")
require.Contains(t, buf.String(), "template custom")
})
t.Run("without either definition", func(t *testing.T) {
page := NewHtmlTemplate("pages/without-either", "pages/without-either.html")
err := page.Load(fsys, templates, fm)
require.NoError(t, err)
var buf strings.Builder
err = page.Execute(&buf, nil)
// block uses default, template fails
if err != nil {
// Expected: template call fails because templateName not defined
require.Contains(t, err.Error(), "templateName")
} else {
// If it somehow succeeded, block should show default
require.Contains(t, buf.String(), "block default")
}
})
}