forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_test.go
129 lines (108 loc) · 2.79 KB
/
functions_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package plush
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Render_Function_Call(t *testing.T) {
r := require.New(t)
input := `<p><%= f() %></p>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"f": func() string {
return "hi!"
},
}))
r.NoError(err)
r.Equal("<p>hi!</p>", s)
}
func Test_Render_Unknown_Function_Call(t *testing.T) {
r := require.New(t)
input := `<p><%= f() %></p>`
_, err := Render(input, NewContext())
r.Error(err)
}
func Test_Render_Function_Call_With_Arg(t *testing.T) {
r := require.New(t)
input := `<p><%= f("mark") %></p>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"f": func(s string) string {
return fmt.Sprintf("hi %s!", s)
},
}))
r.NoError(err)
r.Equal("<p>hi mark!</p>", s)
}
func Test_Render_Function_Call_With_Variable_Arg(t *testing.T) {
r := require.New(t)
input := `<p><%= f(name) %></p>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"f": func(s string) string {
return fmt.Sprintf("hi %s!", s)
},
"name": "mark",
}))
r.NoError(err)
r.Equal("<p>hi mark!</p>", s)
}
func Test_Render_Function_Call_With_Hash(t *testing.T) {
r := require.New(t)
input := `<p><%= f({name: name}) %></p>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"f": func(m map[string]interface{}) string {
return fmt.Sprintf("hi %s!", m["name"])
},
"name": "mark",
}))
r.NoError(err)
r.Equal("<p>hi mark!</p>", s)
}
func Test_Render_Function_Call_With_Error(t *testing.T) {
r := require.New(t)
input := `<p><%= f() %></p>`
_, err := Render(input, NewContextWith(map[string]interface{}{
"f": func() (string, error) {
return "hi!", errors.New("oops")
},
}))
r.Error(err)
}
func Test_Render_Function_Call_With_Block(t *testing.T) {
r := require.New(t)
input := `<p><%= f() { %>hello<% } %></p>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"f": func(h HelperContext) string {
s, _ := h.Block()
return s
},
}))
r.NoError(err)
r.Equal("<p>hello</p>", s)
}
type greeter struct{}
func (g greeter) Greet(s string) string {
return fmt.Sprintf("hi %s!", s)
}
func Test_Render_Function_Call_On_Callee(t *testing.T) {
r := require.New(t)
input := `<p><%= g.Greet("mark") %></p>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"g": greeter{},
}))
r.NoError(err)
r.Equal(`<p>hi mark!</p>`, s)
}
func Test_Render_Function_Optional_Map(t *testing.T) {
r := require.New(t)
input := `<%= foo() %>|<%= bar({a: "A"}) %>`
s, err := Render(input, NewContextWith(map[string]interface{}{
"foo": func(opts map[string]interface{}, help HelperContext) string {
return "foo"
},
"bar": func(opts map[string]interface{}) string {
return opts["a"].(string)
},
}))
r.NoError(err)
r.Equal("foo|A", s)
}