Add HTMLRender ParseFiles and ParseGlob test

Add LoadHTMLGlobAppendZeroParamsPanics test
This commit is contained in:
Ganlv 2019-04-24 19:46:12 +08:00
parent 4b4054af49
commit e033d889c3
2 changed files with 96 additions and 2 deletions

View File

@ -160,6 +160,20 @@ func TestLoadHTMLGlobAppendReleaseMode(t *testing.T) {
assert.Equal(t, "<h1>Hello world</h1>", string(resp)) assert.Equal(t, "<h1>Hello world</h1>", string(resp))
} }
func TestLoadHTMLGlobAppendZeroParamsPanics(t *testing.T) {
assert.Panics(t, func() {
ts := setupHTMLFiles(
t,
DebugMode,
false,
func(router *Engine) {
router.LoadHTMLGlobAppend()
},
)
defer ts.Close()
})
}
func TestLoadHTMLGlobUsingTLS(t *testing.T) { func TestLoadHTMLGlobUsingTLS(t *testing.T) {
ts := setupHTMLFiles( ts := setupHTMLFiles(
t, t,

View File

@ -422,7 +422,7 @@ func TestRenderHTMLTemplateEmptyName(t *testing.T) {
func TestRenderHTMLDebugFiles(t *testing.T) { func TestRenderHTMLDebugFiles(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
htmlRender := HTMLDebug{Files: []string{"../testdata/template/hello.tmpl"}, htmlRender := HTMLDebug{Files: []string{"../testdata/template/hello.tmpl"},
Globs: []string{}, Globs: nil,
Delims: Delims{Left: "{[{", Right: "}]}"}, Delims: Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil, FuncMap: nil,
} }
@ -457,13 +457,93 @@ func TestRenderHTMLDebugGlob(t *testing.T) {
func TestRenderHTMLDebugPanics(t *testing.T) { func TestRenderHTMLDebugPanics(t *testing.T) {
htmlRender := HTMLDebug{Files: nil, htmlRender := HTMLDebug{Files: nil,
Globs: []string{}, Globs: nil,
Delims: Delims{"{{", "}}"}, Delims: Delims{"{{", "}}"},
FuncMap: nil, FuncMap: nil,
} }
assert.Panics(t, func() { htmlRender.Instance("", nil) }) assert.Panics(t, func() { htmlRender.Instance("", nil) })
} }
func TestRenderHTMLDebugParseFiles(t *testing.T) {
w := httptest.NewRecorder()
htmlRender := HTMLDebug{Files: nil,
Globs: nil,
Delims: Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
htmlRender.ParseFiles("../testdata/template/hello.tmpl")
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
"name": "thinkerou",
})
err := instance.Render(w)
assert.NoError(t, err)
assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
}
func TestRenderHTMLDebugParseGlob(t *testing.T) {
w := httptest.NewRecorder()
htmlRender := HTMLDebug{Files: nil,
Globs: nil,
Delims: Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
htmlRender.ParseGlob("../testdata/template/hello*")
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
"name": "thinkerou",
})
err := instance.Render(w)
assert.NoError(t, err)
assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
}
func TestRenderHTMLProductionParseFiles(t *testing.T) {
w := httptest.NewRecorder()
htmlRender := HTMLProduction{
Template: template.New("").Delims("{[{", "}]}"),
}
htmlRender.ParseFiles("../testdata/template/hello.tmpl")
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
"name": "thinkerou",
})
err := instance.Render(w)
assert.NoError(t, err)
assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
}
func TestRenderHTMLProductionParseGlob(t *testing.T) {
w := httptest.NewRecorder()
htmlRender := HTMLProduction{
Template: template.New("").Delims("{[{", "}]}"),
}
htmlRender.ParseGlob("../testdata/template/hello*")
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
"name": "thinkerou",
})
err := instance.Render(w)
assert.NoError(t, err)
assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
}
func TestRenderReader(t *testing.T) { func TestRenderReader(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()