-
Notifications
You must be signed in to change notification settings - Fork 42
/
dynamic_test.go
198 lines (164 loc) · 4.52 KB
/
dynamic_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
package multitemplate
import (
"html/template"
"os"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func createFromFileDynamic() Renderer {
r := NewRenderer()
r.AddFromFiles("index", "tests/base.html", "tests/article.html")
return r
}
func createFromGlobDynamic() Renderer {
r := NewRenderer()
r.AddFromGlob("index", "tests/global/*")
return r
}
func createFromFSDynamic() Render {
r := New()
r.AddFromFS("index", os.DirFS("."), "tests/base.html", "tests/article.html")
return r
}
func createFromStringDynamic() Renderer {
r := NewRenderer()
r.AddFromString("index", "Welcome to {{ .name }} template")
return r
}
func createFromStringsWithFuncsDynamic() Renderer {
r := NewRenderer()
r.AddFromStringsFuncs(
"index",
template.FuncMap{},
`Welcome to {{ .name }} {{template "content"}}`, `{{define "content"}}template{{end}}`,
)
return r
}
func createFromFilesWithFuncsDynamic() Renderer {
r := NewRenderer()
r.AddFromFilesFuncs("index", template.FuncMap{}, "tests/welcome.html", "tests/content.html")
return r
}
func createFromTemplatesDynamic() Renderer {
tmpl := template.Must(template.New("test").Parse("Welcome to {{ .name }} template"))
r := NewRenderer()
r.Add("test", tmpl)
return r
}
func TestMissingTemplateOrNameDynamic(t *testing.T) {
r := NewRenderer()
tmpl := template.Must(template.New("test").Parse("Welcome to {{ .name }} template"))
assert.Panics(t, func() {
r.Add("", tmpl)
}, "template name cannot be empty")
assert.Panics(t, func() {
r.Add("test", nil)
}, "template can not be nil")
}
func TestAddFromFilesDynamic(t *testing.T) {
router := gin.New()
router.HTMLRender = createFromFileDynamic()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"title": "Test Multiple Template",
})
})
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
func TestAddFromGlobDynamic(t *testing.T) {
router := gin.New()
router.HTMLRender = createFromGlobDynamic()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"title": "Test Multiple Template",
})
})
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is login template\n", w.Body.String())
}
func TestAddFromFSDynamic(t *testing.T) {
router := gin.New()
router.HTMLRender = createFromFSDynamic()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"title": "Test Multiple Template",
})
})
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "<p>Test Multiple Template</p>\nHi, this is article template\n", w.Body.String())
}
func TestAddFromStringDynamic(t *testing.T) {
router := gin.New()
router.HTMLRender = createFromStringDynamic()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"name": "index",
})
})
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
func TestAddFromStringsFruncsDynamic(t *testing.T) {
router := gin.New()
router.HTMLRender = createFromStringsWithFuncsDynamic()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"name": "index",
})
})
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template", w.Body.String())
}
func TestAddFromFilesFruncsDynamic(t *testing.T) {
router := gin.New()
router.HTMLRender = createFromFilesWithFuncsDynamic()
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index", gin.H{
"name": "index",
})
})
w := performRequest(router)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "Welcome to index template\n", w.Body.String())
}
func TestPanicInvalidTypeBuilder(t *testing.T) {
assert.Panics(t, func() {
b := templateBuilder{}
b.buildType = 10
b.buildTemplate()
})
}
func TestTemplateNotFound(t *testing.T) {
r := make(DynamicRender)
r.AddFromString("index", "This is a test template")
assert.Panics(t, func() {
r.Instance("NotFoundTemplate", nil)
})
}
func TestNotDynamicMode(t *testing.T) {
gin.SetMode("test")
TestAddFromFilesDynamic(t)
gin.SetMode("debug")
}
func TestAddTemplate(t *testing.T) {
tmpl := template.Must(template.ParseFiles("tests/base.html", "tests/article.html"))
b := templateBuilder{}
b.buildType = templateType
b.tmpl = tmpl
b.buildTemplate()
assert.NotPanics(t, func() {
b.buildTemplate()
})
}
func TestAddingTemplate(t *testing.T) {
assert.NotPanics(t, func() {
createFromTemplatesDynamic()
})
}