-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplates_test.go
289 lines (225 loc) · 6.53 KB
/
templates_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
package got
import (
"bytes"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
// Borrowed from text/template
// https://golang.org/src/text/template/examplefiles_test.go
// templateFile defines the contents of a template to be stored in a file, for testing.
type templateFile struct {
name string
contents string
}
var testingTemplateFiles = []templateFile{
// We have two pages each using a different parent layout
// One of the pages is using a subfolder
{"pages/home/home.html", `{{define "content"}}home {{.Name}}{{end}} {{/* use one */}}`},
{"pages/about.html", `{{define "content"}}about {{.Name}}{{end}}{{/* use two */}}`},
// We have two different layouts (using two different styles)
{"layouts/one.html", `Layout 1: {{.Name}} {{block "content" .}}{{end}} {{block "includes/sidebar" .}}{{end}}`},
{"layouts/two.html", `Layout 2: {{.Name}} {{template "content" .}} {{template "includes/sidebar" .}}`},
// We have two includes shared among the pages
{"includes/header.html", `header`},
{"includes/sidebar.html", `sidebar {{.Name}}`},
}
func createTestDir(files []templateFile) (dir string, err error) {
dir, err = ioutil.TempDir("", "template")
if err != nil {
return
}
for _, file := range files {
// Create sub directory of file (if needed)
fd := filepath.Dir(filepath.Join(dir, file.name))
err = os.MkdirAll(fd, os.ModePerm)
if err != nil {
return
}
var f *os.File
f, err = os.Create(filepath.Join(dir, file.name))
if err != nil {
return
}
defer f.Close()
_, err = io.WriteString(f, file.contents)
if err != nil {
return
}
}
return
}
//
// Tests
//
// Here we demonstrate loading a set of templates from a directory.
func TestTemplates(t *testing.T) {
// Here we create a temporary directory and populate it with our sample
// template definition files; usually the template files would already
// exist in some location known to the program.
dir, err := createTestDir(testingTemplateFiles)
if err != nil {
t.Error(err)
}
// Clean up after the test; another quirk of running as an example.
defer os.RemoveAll(dir)
templates, err := New(dir, ".html", nil)
if err != nil {
t.Error(err)
}
//
// 1: Test page + include + layout
//
data := struct{ Name string }{"John"}
b, err := templates.Compile("home", data)
if err != nil {
log.Fatalf("template execution: %s", err)
}
got := string(b.Bytes())
want := "Layout 1: John home John sidebar John"
if got != want {
t.Errorf("handler returned wrong body:\n\tgot: %q\n\twant: %q", got, want)
}
//
// 2: Test layout isolation
//
data = struct{ Name string }{"Jane"}
b, err = templates.Compile("about", data)
if err != nil {
log.Fatalf("template execution: %s", err)
}
got = string(b.Bytes())
want = "Layout 2: Jane about Jane sidebar Jane"
if got != want {
t.Errorf("handler returned wrong body:\n\tgot: %q\n\twant: %q", got, want)
}
//
// 3: Test HTTP handler
//
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := http.NewServeMux()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data = struct{ Name string }{"Bob"}
err := templates.Render(w, "home", data, http.StatusOK)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
router.ServeHTTP(rr, req)
got = rr.Body.String()
want = "Layout 1: Bob home Bob sidebar Bob"
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
t.Error(rr.Body.String())
}
if got != want {
t.Errorf("handler returned wrong body:\n\tgot: %q\n\twant: %q", got, want)
}
}
func BenchmarkCompile(b *testing.B) {
// Here we create a temporary directory and populate it with our sample
// template definition files; usually the template files would already
// exist in some location known to the program.
dir, err := createTestDir(testingTemplateFiles)
if err != nil {
b.Error(err)
}
// Clean up after the test; another quirk of running as an example.
defer os.RemoveAll(dir)
templates, err := New(dir, ".html", DefaultFunctions)
if err != nil {
b.Error(err)
}
b.ResetTimer()
data := struct{ Name string }{"John"}
for i := 0; i < b.N; i++ {
body, err := templates.Compile("home", data)
if err != nil {
b.Error(err)
}
got := string(body.Bytes())
want := "Layout 1: John home John sidebar John"
if got != want {
b.Errorf("handler returned wrong body:\n\tgot: %q\n\twant: %q", got, want)
}
}
}
/*
func BenchmarkCompileBuffer(b *testing.B) {
// Here we create a temporary directory and populate it with our sample
// template definition files; usually the template files would already
// exist in some location known to the program.
dir, err := createTestDir(testingTemplateFiles)
if err != nil {
b.Error(err)
}
// Clean up after the test; another quirk of running as an example.
defer os.RemoveAll(dir)
templates, err := New(dir, ".html")
if err != nil {
b.Error(err)
}
b.ResetTimer()
data := struct{ Name string }{"John"}
for i := 0; i < b.N; i++ {
buf := bufpool.Get()
err := templates.CompileWithBuffer("home", data, buf)
if err != nil {
b.Error(err)
}
got := string(buf.Bytes())
want := "Layout 1: John home John sidebar John"
bufpool.Put(buf)
if got != want {
b.Errorf("handler returned wrong body:\n\tgot: %q\n\twant: %q", got, want)
}
}
}
*/
func BenchmarkNativeTemplates(b *testing.B) {
// Here we create a temporary directory and populate it with our sample
// template definition files; usually the template files would already
// exist in some location known to the program.
dir, err := createTestDir(testingTemplateFiles)
if err != nil {
b.Error(err)
}
// Clean up after the test; another quirk of running as an example.
defer os.RemoveAll(dir)
t := template.New("")
var by []byte
for _, name := range []string{"pages/home/home", "layouts/one", "includes/sidebar"} {
by, err = ioutil.ReadFile(filepath.Join(dir, name+".html"))
if err != nil {
b.Error(err)
}
_, err = t.New(name).Parse(string(by))
if err != nil {
b.Error(err)
}
}
b.ResetTimer()
data := struct{ Name string }{"John"}
for i := 0; i < b.N; i++ {
by = nil
buf := bytes.NewBuffer(by)
if err := t.ExecuteTemplate(buf, "layouts/one", data); err != nil {
b.Error(err)
}
got := string(buf.Bytes())
want := "Layout 1: John home John sidebar John"
if got != want {
b.Errorf("handler returned wrong body:\n\tgot: %q\n\twant: %q", got, want)
}
}
}