/
new_test.go
47 lines (43 loc) · 901 Bytes
/
new_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
package rightmpl
import (
"bytes"
"io"
"testing"
)
var testComponents = map[string]func(w io.Writer, values map[string]interface{}){
"sayBoo": func(w io.Writer, values map[string]interface{}) {
w.Write([]byte("boo"))
},
}
func Test_New(t *testing.T) {
tests := []struct {
name string
tmpl string
m map[string]interface{}
expect string
}{{
"hi",
"hello.tmpl",
map[string]interface{}{"val": "world"},
"hello world",
}, {
"addfunc",
"add.tmpl",
nil,
"sub says boo: boo",
}}
ts, err := New("testtmpl/", testComponents)
if err != nil {
t.Fatal("got: ", err.Error(), " want err=nil ")
}
for r := range tests {
w := bytes.NewBuffer(nil)
err = ts.Render(w, r.tmpl, r.m)
if err != nil {
t.Fatal("got: ", err.Error(), " want err=nil ")
}
if w.String() != r.expect {
t.Error("got: '" + w.String() + "', expected '" + r.expect + "'")
}
}
}