-
Notifications
You must be signed in to change notification settings - Fork 56
/
functions_test.go
194 lines (168 loc) · 5.16 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package plush_test
import (
"errors"
"fmt"
"html/template"
"testing"
"github.com/gobuffalo/plush/v5"
"github.com/stretchr/testify/require"
)
func Test_Render_Function_Call(t *testing.T) {
r := require.New(t)
input := `<p><%= f() %></p>`
s, err := plush.Render(input, plush.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 := plush.Render(input, plush.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 := plush.Render(input, plush.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 := plush.Render(input, plush.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 := plush.Render(input, plush.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_Syntax_Error_Hash(t *testing.T) {
r := require.New(t)
input := `<p><%= f({name: name) %></p>`
_, err := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"f": func(m map[string]interface{}) string {
return fmt.Sprintf("hi %s!", m["name"])
},
"name": "mark",
}))
r.Error(err)
}
func Test_Render_Function_Call_With_Error(t *testing.T) {
r := require.New(t)
input := `<p><%= f() %></p>`
_, err := plush.Render(input, plush.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 := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"f": func(h plush.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 := plush.Render(input, plush.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 := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"foo": func(opts map[string]interface{}, help plush.HelperContext) string {
return "foo"
},
"bar": func(opts map[string]interface{}) string {
return opts["a"].(string)
},
}))
r.NoError(err)
r.Equal("foo|A", s)
}
func Test_Render_Function_With_Backticks_And_Quotes(t *testing.T) {
// From https://github.com/gobuffalo/pop/issues/168
r := require.New(t)
input := "<%= raw(`" + `CREATE MATERIALIZED VIEW view_papers AS
SELECT papers.created_at,
papers.updated_at,
papers.id,
papers.name,
( setweight(to_tsvector(papers.name::text), 'A'::"char") ||
setweight(to_tsvector(papers.author_name), 'B'::"char")
) || setweight(to_tsvector(papers.description), 'C'::"char")
AS paper_vector
FROM
( SELECT papers.id, string_agg(categories.code, ',') as categories
FROM papers
LEFT JOIN paper_categories ON paper_categories.paper_id=papers.id LEFT JOIN (select * from categories order by weight asc) categories ON categories.id=paper_categories.category_id
GROUP BY papers.id
) a
LEFT JOIN papers on a.id=papers.id
WHERE (papers.doc_status = ANY (ARRAY[1, 3])) AND papers.status = 1
WITH DATA` + "`) %>"
output := `CREATE MATERIALIZED VIEW view_papers AS
SELECT papers.created_at,
papers.updated_at,
papers.id,
papers.name,
( setweight(to_tsvector(papers.name::text), 'A'::"char") ||
setweight(to_tsvector(papers.author_name), 'B'::"char")
) || setweight(to_tsvector(papers.description), 'C'::"char")
AS paper_vector
FROM
( SELECT papers.id, string_agg(categories.code, ',') as categories
FROM papers
LEFT JOIN paper_categories ON paper_categories.paper_id=papers.id LEFT JOIN (select * from categories order by weight asc) categories ON categories.id=paper_categories.category_id
GROUP BY papers.id
) a
LEFT JOIN papers on a.id=papers.id
WHERE (papers.doc_status = ANY (ARRAY[1, 3])) AND papers.status = 1
WITH DATA`
s, err := plush.Render(input, plush.NewContextWith(map[string]interface{}{
"raw": func(arg string) template.HTML {
return template.HTML(arg)
},
}))
r.NoError(err)
r.Equal(output, s)
}