-
Notifications
You must be signed in to change notification settings - Fork 11
/
template_test.go
56 lines (42 loc) · 973 Bytes
/
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
package velvet_test
import (
"fmt"
"testing"
"github.com/gobuffalo/velvet"
"github.com/stretchr/testify/require"
)
func Test_Template_Helpers(t *testing.T) {
r := require.New(t)
input := `{{say "mark"}}`
tpl, err := velvet.Parse(input)
r.NoError(err)
tpl.Helpers.Add("say", func(name string) string {
return fmt.Sprintf("say: %s", name)
})
ctx := velvet.NewContext()
s, err := tpl.Exec(ctx)
r.NoError(err)
r.Equal("say: mark", s)
input = `{{say "jane"}}`
tpl, err = velvet.Parse(input)
r.NoError(err)
_, err = tpl.Exec(ctx)
r.Error(err)
}
func Test_Template_Clone(t *testing.T) {
r := require.New(t)
say := func(name string) string {
return fmt.Sprintf("speak: %s", name)
}
input := `{{speak "mark"}}`
t1, err := velvet.Parse(input)
r.NoError(err)
t2 := t1.Clone()
t2.Helpers.Add("speak", say)
ctx := velvet.NewContext()
_, err = t1.Exec(ctx)
r.Error(err)
s, err := t2.Exec(ctx)
r.NoError(err)
r.Equal("speak: mark", s)
}