-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate_test.go
186 lines (172 loc) · 4.9 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
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
package main
import (
"bytes"
"testing"
textTemplate "text/template"
)
func TestParseSimple(t *testing.T) {
tpl, errs := parse("hello world", textTemplate.New("base"))
if len(errs) != 0 {
t.Fatalf("errs found: %v", errs)
}
defined := tpl.DefinedTemplates()
if defined != `; defined templates are: "base"` {
t.Errorf("unexpected templates defined: %s", defined)
}
}
func assertError(t *testing.T, expected templateError, actual templateError) {
if expected.Char != actual.Char {
t.Errorf("error Char doesn't match: expected `%d`, actual `%d`", expected.Char, actual.Char)
}
if expected.Line != actual.Line {
t.Errorf("error Line doesn't match: expected `%d`, actual `%d`", expected.Line, actual.Line)
}
if expected.Level != actual.Level {
t.Errorf("error Level doesn't match: expected `%s`, actual `%s`", expected.Level, actual.Level)
}
if expected.Description != actual.Description {
t.Errorf("error Description doesn't match: expected `%s`, actual `%s`", expected.Description, actual.Description)
}
}
func TestParseUnexpectedEOF(t *testing.T) {
_, errs := parse("{{if .Value}}", textTemplate.New("base"))
if len(errs) != 1 {
t.Errorf("unexpected errors found: %v", errs)
}
assertError(t, templateError{
Char: -1,
Line: 0,
Level: parseErrorLevel,
Description: "unexpected EOF",
}, errs[0])
}
func TestParseUnknownFunctions(t *testing.T) {
_, errs := parse("{{foo}}{{bar}}", textTemplate.New("base"))
if len(errs) != 2 {
t.Errorf("unexpected errors found: %v", errs)
}
assertError(t, templateError{
Char: 2,
Line: 0,
Level: parseErrorLevel,
Description: `function "foo" not defined`,
}, errs[0])
assertError(t, templateError{
Char: 9,
Line: 0,
Level: parseErrorLevel,
Description: `function "bar" not defined`,
}, errs[1])
}
func TestParseNoname(t *testing.T) {
_, errs := parse("{{foo}}", textTemplate.New(""))
if len(errs) != 1 {
t.Errorf("unexpected errors found: %v", errs)
}
assertError(t, templateError{
Char: 2,
Line: 0,
Level: parseErrorLevel,
Description: `function "foo" not defined`,
}, errs[0])
}
func TestParseInvalidIf(t *testing.T) {
_, errs := parse("{{if}}{{end}}", textTemplate.New("base"))
if len(errs) != 1 {
t.Errorf("unexpected errors found: %v", errs)
}
assertError(t, templateError{
Char: -1,
Line: 0,
Level: parseErrorLevel,
Description: `missing value for if`,
}, errs[0])
}
func TestParseIndexSyntax(t *testing.T) {
_, errs := parse("<{{.Foo[2]}}>", textTemplate.New("base"))
if len(errs) != 1 {
t.Errorf("unexpected errors found: %v", errs)
}
assertError(t, templateError{
Char: 7,
Line: 0,
Level: parseErrorLevel,
Description: `bad character U+005B '['`,
}, errs[0])
}
func TestParseEmptyCommand(t *testing.T) {
for _, testCase := range []string{"{{}}", "{{- }}", "{{ -}}"} {
_, errs := parse(testCase, textTemplate.New("base"))
if len(errs) != 1 {
t.Errorf("unexpected errors found: %v", errs)
}
assertError(t, templateError{
Char: 0,
Line: 0,
Level: parseErrorLevel,
Description: `missing value for command`,
}, errs[0])
}
}
func TestParseEmptyCommands(t *testing.T) {
_, errs := parse("\n\n{{ }} hello world {{ }}", textTemplate.New("base"))
if len(errs) != 2 {
t.Errorf("unexpected errors found: %v", errs)
}
assertError(t, templateError{
Char: 0,
Line: 2,
Level: parseErrorLevel,
Description: `missing value for command`,
}, errs[0])
assertError(t, templateError{
Char: 18,
Line: 2,
Level: parseErrorLevel,
Description: `missing value for command`,
}, errs[1])
}
func TestExecWorks(t *testing.T) {
tpl, _ := textTemplate.New("base").Parse("<{{.Value}}>")
var buf bytes.Buffer
errs := exec(tpl, struct{ Value string }{Value: "foo"}, &buf)
if len(errs) != 0 {
t.Errorf("errs found: %v", errs)
}
if buf.String() != "<foo>" {
t.Errorf("output doesn't match: `%s`", buf.String())
}
}
func TestExecGenericStruct(t *testing.T) {
tpl, _ := textTemplate.New("base").Parse("<{{.Foo.Bar}}>")
var buf bytes.Buffer
errs := exec(tpl, map[string]interface{}{}, &buf)
if len(errs) != 0 {
t.Errorf("errs found: %v", errs)
}
}
func TestExecMissing(t *testing.T) {
tpl, _ := textTemplate.New("base").Parse("<{{.Value}}>")
var buf bytes.Buffer
errs := exec(tpl, struct{}{}, &buf)
if len(errs) != 1 {
t.Errorf("unexpected errs: %v", errs)
}
assertError(t, templateError{
Line: 0,
Char: 3,
Level: execErrorLevel,
Description: `executing "base" at <.Value>: can't evaluate field Value in type struct {}`,
}, errs[0])
if buf.String() != "<" {
t.Errorf("output doesn't match: `%s`", buf.String())
}
}
func TestExecIgnoresIncomplete(t *testing.T) {
tpl := textTemplate.New("base")
var buf bytes.Buffer
errs := exec(tpl, nil, &buf)
if len(errs) != 0 {
t.Errorf("errs found: %v", errs)
}
}