forked from kyoto-framework/kyoto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_test.go
157 lines (151 loc) · 3.94 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
package kyoto
import (
"bytes"
"html/template"
"io/ioutil"
"os"
"testing"
)
// TestComposeFuncMap ensures that funcmaps are composed correctly.
func TestComposeFuncMap(t *testing.T) {
// Define test funcmaps
fm1 := template.FuncMap{
"Foo": func() int { return 1 },
}
fm2 := template.FuncMap{
"Bar": func() int { return 2 },
}
// Compose
fm3 := ComposeFuncMap(fm1, fm2)
// Check len
if len(fm3) != 2 {
t.Error("FuncMap is not composed correctly")
}
// Check functions are correct
if fm3["Foo"].(func() int)() != 1 {
t.Error("FuncMap is not composed correctly")
}
if fm3["Bar"].(func() int)() != 2 {
t.Error("FuncMap is not composed correctly")
}
}
// TestTemplateDefault tests Template function with a default TemplateConf.
func TestTemplateDefault(t *testing.T) {
// Define a setup for a test
setup := func() {
// Create a template file for test
ioutil.WriteFile("template_test.html", []byte("Placeholder for tests"), 0644)
}
// Define a cleanup for a test
cleanup := func() {
// Remove template file
os.Remove("template_test.html")
}
// Setup
setup()
defer cleanup()
// Create context
c := &Context{}
// Create template
Template(c, "template_test.html")
// Check template is set
if c.Template == nil {
t.Error("Template is not set")
return
}
// Check template is rendering
buf := &bytes.Buffer{}
if err := c.Template.Execute(buf, nil); err != nil || buf.String() != "Placeholder for tests" {
t.Error("Something wrong with template rendering")
return
}
}
// TestTemplateGlob tests Template function with a TemplateConf.ParseGlob setting.
func TestTemplateGlob(t *testing.T) {
// Config store
glob := ""
// Define a setup for a test
setup := func() {
// Create a template file inside of temp directory for test
os.Mkdir("tmp", 0700)
err := ioutil.WriteFile("tmp/template_test.html", []byte("Placeholder for tests"), 0644)
if err != nil {
panic(err)
}
// Set configuration and save default
glob = TemplateConf.ParseGlob
TemplateConf.ParseGlob = "tmp/*.html"
}
// Define a cleanup for a test
cleanup := func() {
// Cleanup template
os.RemoveAll("tmp")
// Bring back configuration
TemplateConf.ParseGlob = glob
}
// Setup
setup()
defer cleanup()
// Create context
c := &Context{}
// Create template
Template(c, "template_test.html")
// Check template is set
if c.Template == nil {
t.Error("Template is not set")
return
}
// Check template is rendering
buf := &bytes.Buffer{}
if err := c.Template.Execute(buf, nil); err != nil || buf.String() != "Placeholder for tests" {
t.Error("Something wrong with template rendering")
return
}
}
// TestTemplateGlob tests TemplateInline function with a default TemplateConf.
func TestTemplateInline(t *testing.T) {
setup := func() {
// Create a template file for test
ioutil.WriteFile("template_test.html", []byte("Placeholder for tests"), 0644)
}
cleanup := func() {
// Remove template file
os.Remove("template_test.html")
}
// Setup
setup()
defer cleanup()
// Create context
c := Context{}
// Create template
TemplateInline(&c, "<html>This is a test</html>")
// Check template is set
if c.Template == nil {
t.Error("Template is not set")
}
// Check template is rendering
buf := &bytes.Buffer{}
if err := c.Template.Execute(buf, nil); err != nil || buf.String() != "<html>This is a test</html>" {
t.Error("Something wrong with template rendering")
return
}
}
// TestTemplateGlob tests TemplateInline function with a default TemplateConf.
func TestTemplateRaw(t *testing.T) {
// Create context
c := Context{}
// Create template
tmpl, _ := template.New("test").Parse("<html>This is a test</html>")
TemplateRaw(&c, tmpl)
// Check template is set
if c.Template == nil {
t.Error("Template is not set")
return
}
// Check template is rendering
buf := &bytes.Buffer{}
if err := c.Template.Execute(buf, nil); err != nil || buf.String() != "<html>This is a test</html>" {
t.Error("Something wrong with template rendering")
return
}
}