Commit 729e4f02 authored by Matt Holt's avatar Matt Holt Committed by GitHub

Merge pull request #1594 from tw4452852/templateFuncsTest

template: add test for custom function
parents f28a159b 790c842f
...@@ -264,8 +264,7 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin ...@@ -264,8 +264,7 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin
return "", err return "", err
} }
tpl := template.New(filename).Funcs(TemplateFuncs) tpl, err := template.New(filename).Funcs(TemplateFuncs).Parse(string(body))
tpl, err = tpl.Parse(string(body))
if err != nil { if err != nil {
return "", err return "", err
} }
......
...@@ -72,8 +72,16 @@ func TestInclude(t *testing.T) { ...@@ -72,8 +72,16 @@ func TestInclude(t *testing.T) {
shouldErr: true, shouldErr: true,
expectedErrorContent: `type httpserver.Context`, expectedErrorContent: `type httpserver.Context`,
}, },
// Test 4 - all good, with custom function
{
fileContent: `hello {{ caddy }}`,
expectedContent: "hello caddy",
shouldErr: false,
expectedErrorContent: "",
},
} }
TemplateFuncs["caddy"] = func() string { return "caddy" }
for i, test := range tests { for i, test := range tests {
testPrefix := getTestPrefix(i) testPrefix := getTestPrefix(i)
......
...@@ -121,6 +121,8 @@ func TestTemplates(t *testing.T) { ...@@ -121,6 +121,8 @@ func TestTemplates(t *testing.T) {
rec = httptest.NewRecorder() rec = httptest.NewRecorder()
// register custom function which is used in template
httpserver.TemplateFuncs["root"] = func() string { return "root" }
tmplroot.ServeHTTP(rec, req) tmplroot.ServeHTTP(rec, req)
if rec.Code != http.StatusOK { if rec.Code != http.StatusOK {
......
<!DOCTYPE html><html><head><title>root</title></head><body>{{.Include "header.html"}}</body></html> <!DOCTYPE html><html><head><title>{{ root }}</title></head><body>{{.Include "header.html"}}</body></html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment