-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtpl_test.go
55 lines (46 loc) · 1.09 KB
/
tpl_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
package main
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestYamlTemplate(t *testing.T) {
type io struct {
Input string
Template string
Output string
}
tests := []io{
io{
Input: "test: value",
Template: "{{.test}}",
Output: "value",
},
io{
Input: "name: Max\nage: 15",
Template: "Hello {{.name}}, of {{.age}} years old",
Output: "Hello Max, of 15 years old",
},
io{
Input: "legumes:\n - potato\n - onion\n - cabbage",
Template: "Legumes:{{ range $index, $el := .legumes}}{{if $index}},{{end}} {{$el}}{{end}}",
Output: "Legumes: potato, onion, cabbage",
},
}
for _, test := range tests {
tpl_file, err := ioutil.TempFile("", "")
assert.Nil(t, err)
defer func() { os.Remove(tpl_file.Name()) }()
_, err = tpl_file.WriteString(test.Template)
assert.Nil(t, err)
tpl_file.Close()
output := bytes.NewBuffer(nil)
err = ExecuteTemplates(strings.NewReader(test.Input), output,
tpl_file.Name())
assert.Nil(t, err)
assert.Equal(t, test.Output, output.String())
}
}