-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtemplate_test.go
416 lines (395 loc) · 12 KB
/
template_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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package kocha_test
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/naoina/kocha"
)
func TestTemplate_FuncMap_in_withInvalidType(t *testing.T) {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{in 1 1}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, nil); err == nil {
t.Errorf("Expect errors, but no errors")
}
}
func TestTemplate_FuncMap_in(t *testing.T) {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
var buf bytes.Buffer
for _, v := range []struct {
Arr interface{}
Sep interface{}
expect string
err error
}{
{[]string{"b", "a", "c"}, "a", "true", nil},
{[]string{"ab", "b", "c"}, "a", "false", nil},
{nil, "a", "", fmt.Errorf("valid types are slice, array and string, got `invalid'")},
} {
buf.Reset()
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{in .Arr .Sep}}`))
err := tmpl.Execute(&buf, v)
if !strings.HasSuffix(fmt.Sprint(err), fmt.Sprint(v.err)) {
t.Errorf(`{{in %#v %#v}}; error has "%v"; want "%v"`, v.Arr, v.Sep, err, v.err)
}
actual := buf.String()
expect := v.expect
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`{{in %#v %#v}} => %#v; want %#v`, v.Arr, v.Sep, actual, expect)
}
}
}
func TestTemplate_FuncMap_url(t *testing.T) {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
func() {
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{url "root"}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, nil); err != nil {
panic(err)
}
actual := buf.String()
expected := "/"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}()
func() {
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{url "user" 713}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, nil); err != nil {
panic(err)
}
actual := buf.String()
expected := "/user/713"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %v, but %v", expected, actual)
}
}()
}
func TestTemplate_FuncMap_nl2br(t *testing.T) {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{nl2br "a\nb\nc\n"}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, nil); err != nil {
panic(err)
}
actual := buf.String()
expected := "a<br>b<br>c<br>"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}
func TestTemplate_FuncMap_raw(t *testing.T) {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{raw "\n<br>"}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, nil); err != nil {
panic(err)
}
actual := buf.String()
expected := "\n<br>"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}
func TestTemplate_FuncMap_invokeTemplate(t *testing.T) {
// test that if ActiveIf returns true.
func() {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
c := &kocha.Context{
Data: map[interface{}]interface{}{
"unit": &testUnit{"test1", true, 0},
"ctx": "testctx1",
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); err != nil {
t.Error(err)
}
actual := buf.String()
expected := "test_tmpl1: testctx1\n"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}()
// test that if ActiveIf returns false.
func() {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
c := &kocha.Context{
Data: map[interface{}]interface{}{
"unit": &testUnit{"test2", false, 0},
"ctx": "testctx2",
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); err != nil {
t.Error(err)
}
actual := buf.String()
expected := "def_tmpl: testctx2\n"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}()
// test that unknown template.
func() {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
c := &kocha.Context{
Data: map[interface{}]interface{}{
"unit": &testUnit{"test3", true, 0},
"ctx": "testctx3",
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "unknown_tmpl" "def_tmpl" $}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); err != nil {
t.Error(err)
}
actual := buf.String()
expected := "def_tmpl: testctx3\n"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}()
// test that unknown templates.
func() {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
c := &kocha.Context{
Data: map[interface{}]interface{}{
"unit": &testUnit{"test4", true, 0},
"ctx": "testctx4",
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "unknown_tmpl" "unknown_def_tmpl" $}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); !strings.HasSuffix(err.Error(), "template not found: appname:unknown_def_tmpl.html") {
t.Error(err)
}
}()
// test that unknown default template.
func() {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
c := &kocha.Context{
Data: map[interface{}]interface{}{
"unit": &testUnit{"test5", true, 0},
"ctx": "testctx5",
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "unknown" $}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); err != nil {
t.Error(err)
}
}()
// test that single context.
func() {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
c := &kocha.Context{
Data: map[interface{}]interface{}{
"unit": &testUnit{"test6", true, 0},
"ctx": "testctx6",
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); err != nil {
t.Error(err)
}
actual := buf.String()
expected := "test_tmpl1: testctx6\n"
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Expect %q, but %q", expected, actual)
}
}()
// test that too many contexts.
func() {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
c := &kocha.Context{
Data: map[interface{}]interface{}{
"unit": &testUnit{"test7", true, 0},
"ctx": "testctx7",
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{invoke_template .Data.unit "test_tmpl1" "def_tmpl" $ $}}`))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); !strings.HasSuffix(err.Error(), "number of context must be 0 or 1") {
t.Error(err)
}
}()
}
func TestTemplateFuncMap_flash(t *testing.T) {
c := newTestContext("testctrlr", "")
funcMap := template.FuncMap(c.App.Template.FuncMap)
for _, v := range []struct {
key string
expect string
}{
{"", ""},
{"success", "test succeeded"},
{"success", "test successful"},
{"error", "test failed"},
{"error", "test failure"},
} {
c.Flash = kocha.Flash{}
c.Flash.Set(v.key, v.expect)
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(fmt.Sprintf(`{{flash . "unknown"}}{{flash . "%s"}}`, v.key)))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, c); err != nil {
t.Error(err)
continue
}
actual := buf.String()
expect := v.expect
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`{{flash . %#v}} => %#v; want %#v`, v.key, actual, expect)
}
}
}
func TestTemplateFuncMap_join(t *testing.T) {
app := kocha.NewTestApp()
funcMap := template.FuncMap(app.Template.FuncMap)
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(`{{join .Arr .Sep}}`))
var buf bytes.Buffer
for _, v := range []struct {
Arr interface{}
Sep string
expect string
}{
{[]int{1, 2, 3}, "&", "1&2&3"},
{[2]uint{12, 34}, " and ", "12 and 34"},
{[]string{"alice", "bob", "carol"}, ", ", "alice, bob, carol"},
{[]string(nil), "|", ""},
{[]bool{}, " or ", ""},
{[]interface{}{"1", 2, "three", uint32(4)}, "-", "1-2-three-4"},
{[]string{"あ", "い", "う", "え", "お"}, "_", "あ_い_う_え_お"},
{[]string{"a", "b", "c"}, "∧", "a∧b∧c"},
} {
buf.Reset()
if err := tmpl.Execute(&buf, v); err != nil {
t.Error(err)
continue
}
actual := buf.String()
expect := v.expect
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`{{join %#v %#v}} => %#v; want %#v`, v.Arr, v.Sep, actual, expect)
}
}
}
func TestTemplate_Get(t *testing.T) {
app := kocha.NewTestApp()
func() {
for _, v := range []struct {
appName string
layout string
ctrlrName string
format string
}{
{"appname", "application", "testctrlr", "html"},
{"appname", "", "testctrlr", "js"},
{"appname", "another_layout", "testctrlr", "html"},
} {
tmpl, err := app.Template.Get(v.appName, v.layout, v.ctrlrName, v.format)
var actual interface{} = err
var expect interface{} = nil
if !reflect.DeepEqual(actual, expect) {
t.Fatalf(`Template.Get(%#v, %#v, %#v, %#v) => %T, %#v, want *template.Template, %#v`, v.appName, v.layout, v.ctrlrName, v.format, tmpl, actual, expect)
}
}
}()
func() {
for _, v := range []struct {
appName string
layout string
ctrlrName string
format string
expectErr error
}{
{"unknownAppName", "app", "test_tmpl1", "html", fmt.Errorf("kocha: template not found: unknownAppName:%s", filepath.Join("layout", "app.html"))},
{"testAppName", "app", "unknown_tmpl1", "html", fmt.Errorf("kocha: template not found: testAppName:%s", filepath.Join("layout", "app.html"))},
{"testAppName", "app", "test_tmpl1", "xml", fmt.Errorf("kocha: template not found: testAppName:%s", filepath.Join("layout", "app.xml"))},
{"testAppName", "", "test_tmpl1", "xml", fmt.Errorf("kocha: template not found: testAppName:test_tmpl1.xml")},
} {
tmpl, err := app.Template.Get(v.appName, v.layout, v.ctrlrName, v.format)
actual := tmpl
expect := (*template.Template)(nil)
actualErr := err
expectErr := v.expectErr
if !reflect.DeepEqual(actual, expect) || !reflect.DeepEqual(actualErr, expectErr) {
t.Errorf(`Template.Get(%#v, %#v, %#v, %#v) => %#v, %#v, ; want %#v, %#v`, v.appName, v.layout, v.ctrlrName, v.format, actual, actualErr, expect, expectErr)
}
}
}()
}
func TestTemplateDelims(t *testing.T) {
app, err := kocha.New(&kocha.Config{
AppPath: "testdata",
AppName: "appname",
DefaultLayout: "",
Template: &kocha.Template{
PathInfo: kocha.TemplatePathInfo{
Name: "appname",
Paths: []string{
filepath.Join("testdata", "app", "view"),
},
},
LeftDelim: "{%",
RightDelim: "%}",
},
RouteTable: []*kocha.Route{
{
Name: "another_delims",
Path: "/",
Controller: &kocha.FixtureAnotherDelimsTestCtrl{
Ctx: "test_other_delims_ctx",
},
},
},
Middlewares: []kocha.Middleware{
&kocha.DispatchMiddleware{},
},
Logger: &kocha.LoggerConfig{
Writer: ioutil.Discard,
},
})
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
var actual interface{} = w.Code
var expect interface{} = 200
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`GET / status => %#v; want %#v`, actual, expect)
}
actual = w.Body.String()
expect = "This is other delims: test_other_delims_ctx\n"
if !reflect.DeepEqual(actual, expect) {
t.Errorf(`GET / => %#v; want %#v`, actual, expect)
}
}