-
Notifications
You must be signed in to change notification settings - Fork 11
/
each_helper_test.go
210 lines (181 loc) · 4.05 KB
/
each_helper_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package velvet_test
import (
"fmt"
"testing"
"github.com/gobuffalo/velvet"
"github.com/stretchr/testify/require"
)
func Test_Each_Helper_NoArgs(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
input := `{{#each }}{{@value}}{{/each}}`
_, err := velvet.Render(input, ctx)
r.Error(err)
}
func Test_Each_Helper(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
ctx.Set("names", []string{"mark", "bates"})
input := `{{#each names }}<p>{{@value}}</p>{{/each}}`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Equal("<p>mark</p><p>bates</p>", s)
}
func Test_Each_Helper_Index(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
ctx.Set("names", []string{"mark", "bates"})
input := `{{#each names }}<p>{{@index}}</p>{{/each}}`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Equal("<p>0</p><p>1</p>", s)
}
func Test_Each_Helper_As(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
ctx.Set("names", []string{"mark", "bates"})
input := `{{#each names as |ind name| }}<p>{{ind}}-{{name}}</p>{{/each}}`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Equal("<p>0-mark</p><p>1-bates</p>", s)
}
func Test_Each_Helper_As_Nested(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
users := []struct {
Name string
Initials []string
}{
{Name: "Mark", Initials: []string{"M", "F", "B"}},
{Name: "Rachel", Initials: []string{"R", "A", "B"}},
}
ctx.Set("users", users)
input := `
{{#each users as |user|}}
<h1>{{user.Name}}</h1>
{{#each user.Initials as |i|}}
{{user.Name}}: {{i}}
{{/each}}
{{/each}}
`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Contains(s, "<h1>Mark</h1>")
r.Contains(s, "Mark: M")
r.Contains(s, "Mark: F")
r.Contains(s, "Mark: B")
r.Contains(s, "<h1>Rachel</h1>")
r.Contains(s, "Rachel: R")
r.Contains(s, "Rachel: A")
r.Contains(s, "Rachel: B")
}
func Test_Each_Helper_SlicePtr(t *testing.T) {
r := require.New(t)
type user struct {
Name string
}
type users []user
us := &users{
{Name: "Mark"},
{Name: "Rachel"},
}
ctx := velvet.NewContext()
ctx.Set("users", us)
input := `
{{#each users as |user|}}
{{user.Name}}
{{/each}}
`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Contains(s, "Mark")
r.Contains(s, "Rachel")
}
func Test_Each_Helper_Map(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
data := map[string]string{
"a": "A",
"b": "B",
}
ctx.Set("letters", data)
input := `
{{#each letters}}
{{@key}}:{{@value}}
{{/each}}
`
s, err := velvet.Render(input, ctx)
r.NoError(err)
for k, v := range data {
r.Contains(s, fmt.Sprintf("%s:%s", k, v))
}
}
func Test_Each_Helper_Map_As(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
data := map[string]string{
"a": "A",
"b": "B",
}
ctx.Set("letters", data)
input := `
{{#each letters as |k v|}}
{{k}}:{{v}}
{{/each}}
`
s, err := velvet.Render(input, ctx)
r.NoError(err)
for k, v := range data {
r.Contains(s, fmt.Sprintf("%s:%s", k, v))
}
}
func Test_Each_Helper_Else(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
data := map[string]string{}
ctx.Set("letters", data)
input := `
{{#each letters as |k v|}}
{{k}}:{{v}}
{{else}}
no letters
{{/each}}
`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Contains(s, "no letters")
}
func Test_Each_Helper_Else_Collection(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
data := map[string][]string{}
ctx.Set("collection", data)
input := `
{{#each collection.emptykey as |k v|}}
{{k}}:{{v}}
{{else}}
no letters
{{/each}}
`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Contains(s, "no letters")
}
func Test_Each_Helper_Else_CollectionMap(t *testing.T) {
r := require.New(t)
ctx := velvet.NewContext()
data := map[string]map[string]string{
"emptykey": map[string]string{},
}
ctx.Set("collection", data)
input := `
{{#each collection.emptykey.something as |k v|}}
{{k}}:{{v}}
{{else}}
no letters
{{/each}}
`
s, err := velvet.Render(input, ctx)
r.NoError(err)
r.Contains(s, "no letters")
}