This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_test.go
179 lines (156 loc) · 4.66 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
package plush
import (
"errors"
"fmt"
"html/template"
"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)
}
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 := Render(input, NewContextWith(map[string]interface{}{
"raw": func(arg string) template.HTML {
return template.HTML(arg)
},
}))
r.NoError(err)
r.Equal(output, s)
}