-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_test.go
312 lines (297 loc) · 8.93 KB
/
compile_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
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
package tmpl
import (
"bytes"
_ "embed"
"strings"
"testing"
. "github.com/tylermmorton/tmpl/testdata"
)
// TODO: replace tests with table driven tests
// @deprecated
type TestTemplate struct {
// Name tests fields who do not implement TemplateProvider
Name string
// Content tests unnamed TemplateProviders
Content *TextComponent
// Title tests struct values
Title string
// Components tests slices of nested templates
Scripts []ScriptComponent `tmpl:"script"`
}
//go:embed testdata/compiler_test.tmpl.html
var testTemplateText string
func (*TestTemplate) TemplateText() string {
return testTemplateText
}
func (*TestTemplate) TemplateFuncMap() FuncMap {
return FuncMap{
"testFuncMap": func() string { return "testFunc result" },
}
}
// Test_Compile tests the compiler's ability to compile and render templates.
// It's like a package level integration test at this point
func Test_Compile(t *testing.T) {
testCases := map[string]struct {
templateProvider TemplateProvider
renderOptions []RenderOption
expectRenderOutput []string
expectRenderErrMsg string
expectCompileErrMsg string
}{
// tmpl should support all html/template syntax. these test cases are
// to ensure the compiler is not breaking any of the syntax. for sanity
"Supports usage of {{ . }} pipeline statements": {
templateProvider: &TextComponent{Text: "Hello World"},
expectRenderOutput: []string{"Hello World"},
},
"Supports usage of {{ .Field }} pipeline statements": {
templateProvider: &DefinedField{DefField: "Hello World"},
expectRenderOutput: []string{"Hello World"},
},
"Supports usage of {{ .Nested.Field }} pipeline statements": {
templateProvider: &DefinedNestedField{Nested: DefinedField{DefField: "Hello World"}},
expectRenderOutput: []string{"Hello World"},
},
"Supports usage of FuncMapProvider to provide static template functions": {
templateProvider: &TestTemplate{
Title: "FuncMapProvider",
Scripts: []ScriptComponent{},
Content: &TextComponent{Text: "Hello World"},
},
expectRenderOutput: []string{
"FuncMapProvider",
"<footer>",
"<div>testFunc result</div>",
"</footer>",
},
},
"Supports usage of {{ define }} and {{ template }} statements": {
templateProvider: &TestTemplate{
Title: "Test",
Scripts: []ScriptComponent{},
Content: &TextComponent{Text: "Hello World"},
},
expectRenderOutput: []string{
"Test",
"Hello World",
"<form id=\"defineForm\">",
},
},
"Supports usage of {{ if }} statements with bare fields": {
templateProvider: &DefinedIf{DefIf: true, Message: "Hello World"},
expectRenderOutput: []string{
"Hello World",
},
},
"Supports usage of builtin equality operations in {{ if eq .Field 1 }} pipelines": {
templateProvider: &PipelineIf{
DefInt: 1,
Message: "Hello World",
},
expectRenderOutput: []string{
"Hello World",
},
},
"Supports usage of {{ range }} statements over string types": {
templateProvider: &DefinedRange{DefList: []string{"Hello", "World"}},
expectRenderOutput: []string{
"Hello",
"World",
},
},
"Supports usage of {{ range }} statements over anonymous struct types": {
templateProvider: &StructRange{DefList: []struct {
DefField string
}{
{DefField: "Hello"},
{DefField: "World"},
}},
expectRenderOutput: []string{
"Hello",
"World",
},
},
"Supports usage of {{ range }} statements over named struct types": {
templateProvider: &NamedStructRange{NamedStructs: []NamedStruct{
{DefField: "Hello"},
{DefField: "World"},
}},
expectRenderOutput: []string{
"Hello",
"World",
},
},
"Supports usage of {{ if }} statements within {{ range }} bodies": {
templateProvider: &IfWithinRange{
DefList: []DefinedIf{
{DefIf: true, Message: "Hello"},
},
},
expectRenderOutput: []string{
"Hello",
},
},
"Supports usage of {{ range }} statements within {{ range }} bodies": {
templateProvider: &StructRangeWithinRange{
ListOne: []StructOne{
{
ListTwo: []StructTwo{
{DefField: "Hello"},
},
},
{
ListTwo: []StructTwo{
{DefField: "World"},
},
},
},
},
expectRenderOutput: []string{
"Hello",
"World",
},
},
// template nesting tests
"Supports embedded struct fields": {
templateProvider: &EmbeddedField{
EmbeddedStruct: EmbeddedStruct{DefField: "Hello World"},
},
expectRenderOutput: []string{"Hello World"},
},
"Supports multiple levels of embedded TemplateProviders": {
templateProvider: &MultiLevelEmbeds{
LevelOneEmbed: LevelOneEmbed{
LevelTwoEmbed: LevelTwoEmbed{
DefField: "Hello World",
},
},
},
expectRenderOutput: []string{"Hello World"},
},
"Supports nested TemplateProviders that are not embedded": {
templateProvider: &Parent{
N: Child{
DefField: "Hello World",
},
},
expectRenderOutput: []string{"Hello World"},
},
// layout & outlet tests (RenderOption tests)
"Supports usage of WithTarget and WithName when rendering templates": {
templateProvider: &Outlet{
Layout: Layout{},
Content: "Hello World",
},
renderOptions: []RenderOption{
WithName("outlet"),
WithTarget("layout"),
},
expectRenderOutput: []string{"<span>Hello World</span>"},
},
"Supports usage of WithTarget and WithName when rendering templates with nested outlets": {
templateProvider: &OutletWithNested{
Layout: Layout{},
LevelOneEmbed: LevelOneEmbed{
LevelTwoEmbed: LevelTwoEmbed{
DefField: "Hello World",
},
},
},
renderOptions: []RenderOption{
WithName("outlet"),
WithTarget("layout"),
},
expectRenderOutput: []string{"<span>Hello World</span>"},
},
"Supports usage of WithTarget and WithName when rendering layouts with nested templates": {
templateProvider: &OutletWithNestedLayout{
LayoutWithNested: LayoutWithNested{
DefinedField: DefinedField{
DefField: "Hi",
},
},
Content: "Hello World",
},
renderOptions: []RenderOption{
WithName("outlet"),
WithTarget("layout"),
},
expectRenderOutput: []string{"<title>Hi</title>\\n<span>Hello World</span>"},
},
"Supports usage of $ dot reference within range scopes": {
templateProvider: &DollarSignWithinRange{
DefList: []string{"1", "2"},
DefStr: "Hello",
},
expectRenderOutput: []string{"HelloHello"},
},
"Supports usage of $ dot reference within an if within range scopes": {
templateProvider: &DollarSignWithinIfWithinRange{
DefList: []string{"Hello", "World"},
DefStr: "Hello",
},
expectRenderOutput: []string{"PASS", "FAIL"},
},
// these are test cases for the compiler's built-in analyzers
"Catches usage of {{ template }} statements containing undefined template names": {
templateProvider: &UndefinedTemplate{},
expectCompileErrMsg: "template \"undefined\" is not provided",
},
"Catches usage of {{ template }} statements without a pipeline": {
templateProvider: &NoPipeline{
LevelOneEmbed: LevelOneEmbed{},
},
expectCompileErrMsg: "template \"one\" is not invoked with a pipeline",
},
"Catches usage of {{ if }} statements containing non-bool types": {
templateProvider: &AnyTypeIf{DefIf: 0},
expectCompileErrMsg: "field \".DefIf\" is not type bool: got int",
},
"Catches usage of {{ if }} statements containing undefined fields": {
templateProvider: &UndefinedIf{},
expectCompileErrMsg: "field \".UndIf\" not defined",
},
"Catches usage of {{ range }} statements containing undefined fields": {
templateProvider: &UndefinedRange{},
expectCompileErrMsg: "field \".UndList\" not defined",
},
"Catches usage of undefined fields": {
templateProvider: &UndefinedField{},
expectCompileErrMsg: "field \".UndField\" not defined",
},
"Catches usage of undefined nested fields": {
templateProvider: &UndefinedNestedField{Nested: UndefinedField{}},
expectCompileErrMsg: "field \".Nested.UndField\" not defined",
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
tmpl, err := Compile(tc.templateProvider)
if err != nil {
if len(tc.expectCompileErrMsg) == 0 {
t.Fatal(err)
} else if !strings.Contains(err.Error(), tc.expectCompileErrMsg) {
t.Fatalf("expected compile error message to contain %q, got %q", tc.expectCompileErrMsg, err.Error())
} else {
return
}
}
buf := bytes.Buffer{}
err = tmpl.Render(&buf, tc.templateProvider, tc.renderOptions...)
if err != nil {
if len(tc.expectRenderErrMsg) == 0 {
t.Fatal(err)
} else if !strings.Contains(err.Error(), tc.expectRenderErrMsg) {
t.Fatalf("expected render error message to contain %q, got %q", tc.expectRenderErrMsg, err.Error())
} else {
return
}
}
for _, expect := range tc.expectRenderOutput {
if !strings.Contains(buf.String(), expect) {
t.Fatalf("expected render output to contain %q, got %q", expect, buf.String())
}
}
})
}
}