-
Notifications
You must be signed in to change notification settings - Fork 3
/
templates_test.go
119 lines (109 loc) · 2.87 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
package mars
import (
"bytes"
"html/template"
"net/http"
"net/http/httptest"
"path/filepath"
"runtime"
"strings"
"testing"
)
func setupTemplateTestingApp() {
_, filename, _, _ := runtime.Caller(0)
BasePath = filepath.Join(filepath.Dir(filename), "testdata")
SetupViews()
}
func TestContextAwareRenderFuncs(t *testing.T) {
setupTemplateTestingApp()
loadMessages(testDataPath)
for expected, input := range map[string]interface{}{
"<h1>Hey, there <b>Rob</b>!</h1>": "Rob",
"<h1>Hey, there <b><3</b>!</h1>": Blarp("<3"),
} {
result := runRequest("en", "i18n_ctx.html", Args{"input": input})
if result != expected {
t.Errorf("Expected '%s', got '%s' for input '%s'", expected, result, input)
}
}
}
func simulateRequest(format, view string) string {
w := httptest.NewRecorder()
httpRequest, _ := http.NewRequest("GET", "/", nil)
req := NewRequest(httpRequest)
req.Format = format
c := NewController(req, &Response{Out: w})
c.RenderTemplate(view).Apply(c.Request, c.Response)
buf := &bytes.Buffer{}
buf.ReadFrom(w.Body)
return buf.String()
}
func TestTemplateNotAvailable(t *testing.T) {
setupTemplateTestingApp()
expectedString := "Template non_existant.html not found."
if resp := simulateRequest("html", "non_existant.html"); !strings.Contains(resp, expectedString) {
t.Error("Error rendering template error message for plaintext requests. Got:", resp)
}
if resp := simulateRequest("txt", "non_existant.html"); !strings.Contains(resp, expectedString) {
t.Error("Error rendering template error message for plaintext requests. Got:", resp)
}
}
func TestTemplateFuncs(t *testing.T) {
type Scenario struct {
T string
D Args
R string
E string
}
for _, scenario := range []Scenario{
{
`<a href="/{{slug .title}}">{{.title}}</a>`,
Args{"title": "This is a Blog Post!"},
`<a href="/this-is-a-blog-post">This is a Blog Post!</a>`,
``,
},
{
`{{raw .title}}`,
Args{"title": "<b>bla</b>"},
`<b>bla</b>`,
``,
},
{
`{{if even .no}}yes{{else}}no{{end}}`,
Args{"no": 0},
`yes`,
``,
},
{
`{{if even .no}}yes{{else}}no{{end}}`,
Args{"no": 1},
`no`,
``,
},
} {
tmpl, err := template.New("foo").Funcs(TemplateFuncs).Parse(scenario.T)
if err != nil {
t.Error(err)
}
buf := &strings.Builder{}
err = goTemplateWrapper{loader: nil, funcMap: nil, Template: tmpl}.Template.Execute(buf, scenario.D)
if err != nil {
t.Error(err)
}
if res := buf.String(); res != scenario.R {
t.Errorf("Expected '%s', got '%s' for input '%s'", scenario.R, res, scenario.T)
}
}
}
func TestTemplateParsingErrors(t *testing.T) {
for _, scenario := range []string{
`{{.uhoh}`,
`{{if .condition}}look{{else}}there's no end here`,
`{{undefined_function .parameter}}`,
} {
_, err := template.New("foo").Funcs(TemplateFuncs).Parse(scenario)
if err == nil {
t.Errorf("No error when parsing: %s", scenario)
}
}
}