-
Notifications
You must be signed in to change notification settings - Fork 56
/
variables_test.go
188 lines (163 loc) · 4.56 KB
/
variables_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
package plush_test
import (
"html/template"
"strings"
"testing"
"github.com/gobuffalo/plush/v5"
"github.com/gobuffalo/tags/v3"
"github.com/stretchr/testify/require"
)
func Test_Let_Reassignment(t *testing.T) {
r := require.New(t)
input := `<% let foo = "bar" %>
<%= for (a) in myArray { %>
<%= foo %>
<% if (foo != "baz") { %>
<% foo = "baz" %>
<% } %>
<% } %>
<% } %>`
ctx := plush.NewContext()
ctx.Set("myArray", []string{"a", "b"})
s, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("bar\n \n \nbaz", strings.TrimSpace(s))
}
func Test_Let_SyntaxError_NoEqualSign(t *testing.T) {
r := require.New(t)
input := `<% let foo %>`
ctx := plush.NewContext()
_, err := plush.Render(input, ctx)
r.ErrorContains(err, "expected next token to be =")
}
func Test_Let_SyntaxError_NoIdentifier(t *testing.T) {
r := require.New(t)
input := `<% let = %>`
ctx := plush.NewContext()
_, err := plush.Render(input, ctx)
r.ErrorContains(err, "expected next token to be IDENT")
}
func Test_Let_Reassignment_UnknownIdent(t *testing.T) {
r := require.New(t)
input := `<% foo = "baz" %>`
ctx := plush.NewContext()
ctx.Set("myArray", []string{"a", "b"})
_, err := plush.Render(input, ctx)
r.ErrorContains(err, "\"foo\": unknown identifier")
}
func Test_Let_Inside_Helper(t *testing.T) {
r := require.New(t)
ctx := plush.NewContextWith(map[string]interface{}{
"divwrapper": func(opts map[string]interface{}, helper plush.HelperContext) (template.HTML, error) {
body, err := helper.Block()
if err != nil {
return template.HTML(""), err
}
t := tags.New("div", opts)
t.Append(body)
return t.HTML(), nil
},
})
input := `<%= divwrapper({"class": "myclass"}) { %>
<ul>
<% let a = [1, 2, "three", "four"] %>
<%= for (index, name) in a { %>
<li><%=index%> - <%=name%></li>
<% } %>
</ul>
<% } %>`
s, err := plush.Render(input, ctx)
r.NoError(err)
r.Contains(s, "<li>0 - 1</li>")
r.Contains(s, "<li>1 - 2</li>")
r.Contains(s, "<li>2 - three</li>")
r.Contains(s, "<li>3 - four</li>")
}
func Test_Render_Let_Hash(t *testing.T) {
tests := []struct {
name string
success bool
input string
expected string
}{
{"success", true, `<p><% let h = {"a": "A"} %><%= h["a"] %></p>`, "<p>A</p>"},
{"assign", true, `<p><% let h = {"a": "A"} %><% h["a"] = "C" %><%= h["a"] %></p>`, "<p>C</p>"},
{"assign", true, `<p><% let h = {"a": "A"} %><% h["b"] = "D" %><%= h["b"] %></p>`, "<p>D</p>"},
{"intvar", true, `<p><% let h = {"a": "A"} %><% h["b"] = 3 %><%= h["b"] %></p>`, "<p>3</p>"},
{"invalid", true, `<p><% let h = {"a": "A"} %><% h["b"] = 3 %><%= h["c"] %></p>`, "<p></p>"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
s, err := plush.Render(tc.input, plush.NewContext())
if tc.success {
r.NoError(err)
} else {
r.Error(err)
}
r.Equal(tc.expected, s)
})
}
}
func Test_Render_Let_Array(t *testing.T) {
tests := []struct {
name string
success bool
input string
expected string
}{
{"success", true, `<p><% let a = [1, 2, "three", "four", 3.75] %><% a[0] = 3 %><%= a[0] %></p>`, "<p>3</p>"},
{"addition", true, `<p><% let a = [1, 2, "three", "four", 3.75] %><% a[4] = 3 %><%= a[4] + 2 %></p>`, "<p>5</p>"},
{"invalid_key", false, `<p><% let a = [1, 2, "three", "four", 3.75] %><% a["b"] = 3 %><%= a["c"] %></p>`, ""},
{"outofbounds_assign", false, `<p><% let a = [1, 2, "three", "four", 3.75] %><% a[5] = 3 %><%= a[4] + 2 %></p>`, ""},
{"outofbounds_access", false, `<p><% let a = [1, 2, "three", "four", 3.75] %><%= a[5] %></p>`, ""},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
s, err := plush.Render(tc.input, plush.NewContext())
if tc.success {
r.NoError(err)
} else {
r.Error(err)
}
r.Equal(tc.expected, s)
})
}
}
type Category1 struct {
Products []Product1
}
type Product1 struct {
Name []string
}
func Test_Render_Access_CalleeArray(t *testing.T) {
tests := []struct {
name string
success bool
expected string
data Category1
}{
{"success", true, "Buffalo", Category1{
[]Product1{
{Name: []string{"Buffalo"}},
},
}},
{"outofbounds", false, "", Category1{}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
input := `<% let a = product_listing.Products[0].Name[0] %><%= a %>`
ctx := plush.NewContext()
ctx.Set("product_listing", tc.data)
s, err := plush.Render(input, ctx)
if tc.success {
r.NoError(err)
} else {
r.Error(err)
}
r.Equal(tc.expected, s)
})
}
}