-
Notifications
You must be signed in to change notification settings - Fork 0
/
execString_test.go
316 lines (300 loc) · 8.63 KB
/
execString_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
313
314
315
316
package treetop
import (
"bytes"
"html/template"
"net/http/httptest"
"strings"
"testing"
)
func TestStringExecutor_constructTemplate(t *testing.T) {
tests := []struct {
name string
view *View
data interface{}
want string
wantErr string
}{
{
name: "basic",
view: NewView("<p>hello {{ . }}!</p>", Noop),
data: "world",
want: "<p>hello world!</p>",
wantErr: "",
},
{
name: "with subviews",
view: func() *View {
b := NewView(`<div> base, content: {{ block "content" . }} default here {{ end }} </div>`, Noop)
b.NewDefaultSubView("content", `<p id="content">hello {{ . }}!</p>`, Noop)
return b
}(),
data: "world",
want: `<div> base, content: <p id="content">hello world!</p> </div>`,
},
{
name: "template parse error",
view: func() *View {
b := NewView(`<div> base, content: {{ b.^^lock "content" . }} default here {{ end }} </div>`, Noop)
b.NewDefaultSubView("content", `<p id="content">hello {{ . }}!</p>`, Noop)
return b
}(),
data: "world",
wantErr: `template: :1: function "b" not defined`,
},
{
name: "with nil subviews",
view: func() *View {
b := NewView(`<div> base, content: {{ block "content" . }} default here {{ end }} </div>`, Noop)
b.NewSubView("content", `<p id="content">hello {{ . }}!</p>`, Noop)
return b
}(),
data: "world",
want: `<div> base, content: default here </div>`,
},
{
name: "error, template missing a declared blockname",
view: func() *View {
b := NewView(`<div> base, content: </div>`, Noop)
b.NewSubView("content", `<p id="content">hello {{ . }}!</p>`, Noop)
return b
}(),
wantErr: `template <div> base, content: </div>: missing template declaration(s) for sub view blocks: "content"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exec := StringExecutor{}
got, ok := exec.NewViewHandler(tt.view).(*TemplateHandler)
if !ok {
t.Fatal("StringExecutor did not return a TemplateHandler")
}
err := exec.FlushErrors()
if err != nil {
if tt.wantErr == "" {
t.Errorf("Unexpected error: %s", err)
} else if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("Expecting error %s to contain: %s", err, tt.wantErr)
}
return
} else if tt.wantErr != "" {
t.Errorf("Expected error %s", tt.wantErr)
return
}
buf := new(bytes.Buffer)
got.PageTemplate.ExecuteTemplate(buf, tt.view.Defines, tt.data)
gotString := buf.String()
if gotString != tt.want {
t.Errorf("StringExecutor.constructTemplate() got %v, want %v", gotString, tt.want)
}
})
}
}
func TestStringExecutor_NewViewHandler(t *testing.T) {
// setup
standardHandler := func(exec ViewExecutor) ViewHandler {
base := NewView(`<html><body>
{{ template "content" .Content }}
{{ block "ps" .PS }}
<p id="ps">Default {{ . }}</p>
{{ end }}
</body></html>`, Constant(struct {
Content interface{}
PS interface{}
}{
Content: struct {
Message string
Sub interface{}
}{
Message: "from base to content",
Sub: "from base via content to sub",
},
PS: "from base to ps",
}))
content := base.NewSubView(
"content",
"<div id=\"content\">\n<p>Given {{ .Message }}</p>\n{{ template \"sub\" .Sub }}\n</div>",
Constant(struct {
Message string
Sub interface{}
}{
Message: "from content to content!",
Sub: "from content to sub",
}),
)
content.NewDefaultSubView("sub", `<p id="sub">Given {{ . }}</p>`, Constant("from sub to sub"))
ps := base.NewSubView("ps", `<div id="ps">Given {{ . }}</div>`, Constant("from ps to ps"))
return exec.NewViewHandler(content, ps)
}
// tests
tests := []struct {
name string
getHandler func(ViewExecutor) ViewHandler
expectPage string
expectTemplate string
expectErrors []string
pageOnly bool
templateOnly bool
}{
{
name: "functional example",
getHandler: standardHandler,
expectPage: stripIndent(`<html><body>
<div id="content">
<p>Given from base to content</p>
<p id="sub">Given from base via content to sub</p>
</div>
<div id="ps">Given from base to ps</div>
</body></html>`),
expectTemplate: stripIndent(`<template>
<div id="content">
<p>Given from content to content!</p>
<p id="sub">Given from content to sub</p>
</div>
<div id="ps">Given from ps to ps</div>
</template>`),
},
{
name: "template parse errors",
getHandler: func(exec ViewExecutor) ViewHandler {
base := NewView(`{{ fail }}`, Constant(struct {
Content interface{}
PS interface{}
}{
Content: struct {
Message string
Sub interface{}
}{
Message: "from base to content",
Sub: "from base via content to sub",
},
PS: "from base to ps",
}))
content := base.NewSubView(
"content",
`{{ failcontent }}`,
Constant(struct {
Message string
Sub interface{}
}{
Message: "from content to content!",
Sub: "from content to sub",
}),
)
content.NewDefaultSubView("sub", `{{ failsub }}`, Constant("from sub to sub"))
ps := base.NewSubView("ps", `{{ failps }}`, Constant("from ps to ps"))
return exec.NewViewHandler(content, ps)
},
expectPage: "Not Acceptable",
expectTemplate: "Not Acceptable",
expectErrors: []string{
`failed to parse template "{{ fail }}": template: :1: function "fail" not defined`,
`failed to parse template "{{ failcontent }}": template: content:1: function "failcontent" not defined`,
`failed to parse template "{{ failps }}": template: ps:1: function "failps" not defined`,
},
},
{
name: "page only",
getHandler: standardHandler,
expectPage: stripIndent(`<html><body>
<div id="content">
<p>Given from base to content</p>
<p id="sub">Given from base via content to sub</p>
</div>
<div id="ps">Given from base to ps</div>
</body></html>`),
expectTemplate: "Not Acceptable",
pageOnly: true,
},
{
name: "template only",
getHandler: standardHandler,
expectPage: "Not Acceptable",
expectTemplate: stripIndent(`<template>
<div id="content">
<p>Given from content to content!</p>
<p id="sub">Given from content to sub</p>
</div>
<div id="ps">Given from ps to ps</div>
</template>`),
templateOnly: true,
},
{
name: "nil view",
getHandler: func(exec ViewExecutor) ViewHandler {
return exec.NewViewHandler(nil)
},
expectPage: "Not Acceptable",
expectTemplate: "Not Acceptable",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exec := &StringExecutor{}
handler := tt.getHandler(exec)
if tt.pageOnly {
handler = handler.PageOnly()
}
if tt.templateOnly {
handler = handler.FragmentOnly()
}
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, mockRequest("/some/path", "*/*"))
gotPage := stripIndent(sDumpBody(rec))
if gotPage != tt.expectPage {
t.Errorf("Expecting page body\n%s\nGot\n%s", tt.expectPage, gotPage)
}
rec = httptest.NewRecorder()
handler.ServeHTTP(rec, mockRequest("/some/path", TemplateContentType))
gotTemplate := stripIndent(sDumpBody(rec))
if gotTemplate != tt.expectTemplate {
t.Errorf("Expecting template body\n%s\nGot\n%s", tt.expectTemplate, gotTemplate)
}
gotErrors := exec.FlushErrors()
for len(gotErrors) < len(tt.expectErrors) {
gotErrors = append(gotErrors, nil)
}
for i, err := range gotErrors {
if err == nil {
t.Errorf("Expecting an error [%d]: %s", i, tt.expectErrors[i])
continue
}
if i >= len(tt.expectErrors) {
t.Errorf("Unexpected error [%d]: %s", i, err.Error())
continue
}
if got := err.Error(); got != tt.expectErrors[i] {
t.Errorf("Expecting error [%d]\n%s\ngot\n%s", i, tt.expectErrors[i], got)
}
}
})
}
}
func TestStringExecutor_FuncMap(t *testing.T) {
exec := StringExecutor{
Funcs: template.FuncMap{
"title": strings.Title,
},
}
v := NewView(`
<div>
<p>Input: {{printf "%q" .}}</p>
<p>Output 0: {{title .}}</p>
<p>Output 1: {{title . | printf "%q"}}</p>
<p>Output 2: {{printf "%q" . | title}}</p>
</div>
`, Constant("the go programming language"))
h := exec.NewViewHandler(v)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, mockRequest("/some/path", "*/*"))
gotPage := stripIndent(sDumpBody(rec))
if gotPage != stripIndent(`
<div>
<p>Input: "the go programming language"</p>
<p>Output 0: The Go Programming Language</p>
<p>Output 1: "The Go Programming Language"</p>
<p>Output 2: "The Go Programming Language"</p>
</div>
`) {
t.Errorf("Expecting title case, got\n%s", gotPage)
}
}