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