forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
variadic_test.go
72 lines (60 loc) · 1.41 KB
/
variadic_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
package plush
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_VariadicHelper(t *testing.T) {
r := require.New(t)
input := `<%= foo(1, 2, 3) %>`
ctx := NewContext()
ctx.Set("foo", func(args ...int) int {
return len(args)
})
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("3", s)
}
func Test_VariadicHelper_SecondArg(t *testing.T) {
r := require.New(t)
input := `<%= foo("hello") %>`
ctx := NewContext()
ctx.Set("foo", func(s string, args ...interface{}) string {
return s
})
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("hello", s)
}
func Test_VariadicHelperNoParam(t *testing.T) {
r := require.New(t)
input := `<%= foo() %>`
ctx := NewContext()
ctx.Set("foo", func(args ...int) int {
return len(args)
})
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("0", s)
}
func Test_VariadicHelperNoVariadicParam(t *testing.T) {
r := require.New(t)
input := `<%= foo(1) %>`
ctx := NewContext()
ctx.Set("foo", func(a int, args ...int) int {
return a + len(args)
})
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("1", s)
}
func Test_VariadicHelperWithWrongParam(t *testing.T) {
r := require.New(t)
input := `<%= foo(1, 2, "test") %>`
ctx := NewContext()
ctx.Set("foo", func(args ...int) int {
return len(args)
})
_, err := Render(input, ctx)
r.Error(err)
r.Contains(err.Error(), "test (string) is an invalid argument for foo at pos 2: expected (int)")
}