-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
template_test.go
83 lines (79 loc) · 2.1 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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRenderTemplate(t *testing.T) {
cases := []struct {
Case string
Expected string
Template string
ShouldError bool
Context interface{}
}{
{Case: "single property", Expected: "hello world", Template: "{{.Text}} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "invalid property", ShouldError: true, Template: "{{.Durp}} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "invalid template", ShouldError: true, Template: "{{ if .Text }} world", Context: struct{ Text string }{Text: "hello"}},
{Case: "if statement true", Expected: "hello world", Template: "{{ if .Text }}{{.Text}} world{{end}}", Context: struct{ Text string }{Text: "hello"}},
{Case: "if statement false", Expected: "world", Template: "{{ if .Text }}{{.Text}} {{end}}world", Context: struct{ Text string }{Text: ""}},
{
Case: "if statement true with 2 properties",
Expected: "hello world",
Template: "{{.Text}}{{ if .Text2 }} {{.Text2}}{{end}}",
Context: struct {
Text string
Text2 string
}{
Text: "hello",
Text2: "world",
},
},
{
Case: "if statement false with 2 properties",
Expected: "hello",
Template: "{{.Text}}{{ if .Text2 }} {{.Text2}}{{end}}",
Context: struct {
Text string
Text2 string
}{
Text: "hello",
},
},
{
Case: "double property template",
Expected: "hello world",
Template: "{{.Text}} {{.Text2}}",
Context: struct {
Text string
Text2 string
}{
Text: "hello",
Text2: "world",
},
},
{
Case: "sprig - contains",
Expected: "hello world",
Template: "{{ if contains \"hell\" .Text }}{{.Text}} {{end}}{{.Text2}}",
Context: struct {
Text string
Text2 string
}{
Text: "hello",
Text2: "world",
},
},
}
for _, tc := range cases {
template := &textTemplate{
Template: tc.Template,
Context: tc.Context,
}
text, err := template.render()
if tc.ShouldError {
assert.Error(t, err)
continue
}
assert.Equal(t, tc.Expected, text, tc.Case)
}
}