forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_test.go
462 lines (385 loc) · 8.49 KB
/
struct_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package plush
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Render_Struct_Attribute(t *testing.T) {
r := require.New(t)
input := `<%= f.Name %>`
ctx := NewContext()
f := struct {
Name string
}{"Mark"}
ctx.Set("f", f)
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("Mark", s)
}
func Test_Render_UnknownAttribute_on_Callee(t *testing.T) {
r := require.New(t)
ctx := NewContext()
ctx.Set("m", struct{}{})
input := `<%= m.Foo %>`
_, err := Render(input, ctx)
r.Error(err)
r.Contains(err.Error(), "'m' does not have a field or method named 'Foo' (m.Foo)")
}
type Robot struct {
Avatar Avatar
name string
}
type Avatar string
func (a Avatar) URL() string {
return strings.ToUpper(string(a))
}
func (r *Robot) Name() string {
return r.name
}
func Test_Render_Function_on_sub_Struct(t *testing.T) {
r := require.New(t)
ctx := NewContext()
bender := Robot{
Avatar: Avatar("bender.jpg"),
}
ctx.Set("robot", bender)
input := `<%= robot.Avatar.URL() %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("BENDER.JPG", s)
}
func Test_Render_Struct_PointerMethod(t *testing.T) {
r := require.New(t)
ctx := NewContext()
robot := Robot{name: "robot"}
t.Run("ByValue", func(t *testing.T) {
ctx.Set("robot", robot)
input := `<%= robot.Name() %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("robot", s)
})
t.Run("ByPointer", func(t *testing.T) {
ctx.Set("robot", &robot)
input := `<%= robot.Name() %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("robot", s)
})
}
func Test_Render_Struct_PointerMethod_IsNil(t *testing.T) {
r := require.New(t)
type mylist struct {
N int
Next *mylist
}
input := `Current number is <%= p.N %>.<%= if (p.Next) { %> Next up is <%= p.Next.N %>.<% } %>`
first := &mylist{N: 0}
last := first
for i := 0; i < 5; i++ {
last.Next = &mylist{N: i + 1}
last = last.Next
}
resE := []string{
"Current number is 0. Next up is 1.",
"Current number is 1. Next up is 2.",
"Current number is 2. Next up is 3.",
"Current number is 3. Next up is 4.",
"Current number is 4. Next up is 5.",
"Current number is 5.",
}
for p := first; p != nil; p = p.Next {
ctx := NewContextWith(map[string]interface{}{"p": p})
res, err := Render(input, ctx)
r.NoError(err)
r.Equal(resE[p.N], res)
}
}
func Test_Render_Struct_PointerValue_Nil(t *testing.T) {
r := require.New(t)
type user struct {
Name string
Image *string
}
u := user{
Name: "Garn Clapstick",
Image: nil,
}
ctx := NewContextWith(map[string]interface{}{
"user": u,
})
input := `<%= user.Name %>: <%= user.Image %>`
res, err := Render(input, ctx)
r.NoError(err)
r.Equal(`Garn Clapstick: `, res)
}
func Test_Render_Struct_PointerValue_NonNil(t *testing.T) {
r := require.New(t)
type user struct {
Name string
Image *string
}
image := "bicep.png"
u := user{
Name: "Scrinch Archipeligo",
Image: &image,
}
ctx := NewContextWith(map[string]interface{}{
"user": u,
})
input := `<%= user.Name %>: <%= user.Image %>`
res, err := Render(input, ctx)
r.NoError(err)
r.Equal(`Scrinch Archipeligo: bicep.png`, res)
}
func Test_Render_Struct_Multiple_Access(t *testing.T) {
r := require.New(t)
type mylist struct {
Name string
}
input := `<%= myarray[0].Name %> <%= myarray[1].Name %>`
gg := make([]mylist, 3)
gg[0].Name = "John"
gg[1].Name = "Doe"
ctx := NewContext()
ctx.Set("myarray", gg)
res, err := Render(input, ctx)
r.NoError(err)
r.Equal("John Doe", res)
}
func Test_Render_Nested_Structs_Start_With_Slice(t *testing.T) {
r := require.New(t)
type b struct {
Final string
}
type mylist struct {
Name b
}
input := `<%= myarray[0].Name.Final %>`
gg := make([]mylist, 3)
gg[0].Name.Final = "Hello World"
ctx := NewContext()
ctx.Set("myarray", gg)
res, err := Render(input, ctx)
r.NoError(err)
r.Equal("Hello World", res)
}
func Test_Render_Nested_Structs_Start_With_Slice_End_With_Slice(t *testing.T) {
r := require.New(t)
type b struct {
A []string
}
type mylist struct {
Name b
}
input := `<%= myarray[0].Name.A[0] %>`
gg := make([]mylist, 3)
gg[0].Name = b{[]string{"Hello World"}}
ctx := NewContext()
ctx.Set("myarray", gg)
res, err := Render(input, ctx)
r.NoError(err)
r.Equal("Hello World", res)
}
func Test_Render_Nested_Structs_Ends_With_Slice(t *testing.T) {
r := require.New(t)
ctx := NewContext()
type c struct {
Final []string
}
type b struct {
B c
}
type mylist struct {
Name b
}
bender := mylist{
Name: b{
B: c{
Final: []string{"bendser.jpg"},
},
},
}
ctx.Set("robot", bender)
input := `<%= robot.Name.B.Final[0] %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("bendser.jpg", s)
}
func Test_Render_Nested_Structs(t *testing.T) {
r := require.New(t)
ctx := NewContext()
type c struct {
Final string
}
type b struct {
B c
}
type mylist struct {
Name b
}
bender := mylist{
Name: b{
B: c{
Final: "bendser.jpg",
},
},
}
ctx.Set("robot", bender)
input := `<%= robot.Name.B.Final %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("bendser.jpg", s)
}
func Test_Render_Struct_Access_Slice_Field(t *testing.T) {
r := require.New(t)
ctx := NewContext()
type D struct {
Final string
}
type c struct {
Final []D
}
type b struct {
B c
}
type mylist struct {
Name b
}
bender := mylist{
Name: b{
B: c{
Final: []D{
{Final: "String"},
},
},
},
}
ctx.Set("robot", bender)
input := `<%= robot.Name.B.Final[0].Final %>`
s, err := Render(input, ctx)
r.NoError(err)
r.Equal("String", s)
}
func Test_Render_Struct_Nested_Slice_Access(t *testing.T) {
r := require.New(t)
type d struct {
Final string
}
type c struct {
He []d
}
type b struct {
A []c
}
type mylist struct {
Name []b
}
input := `<%= myarray[0].Name[0].A[1].He[2].Final %>`
gg := make([]mylist, 3)
var bc b
var ca c
ca.He = make([]d, 3)
ca.He[2] = d{"Hello World"}
bc.A = make([]c, 3)
bc.A[1] = ca
gg[0].Name = []b{bc}
ctx := NewContext()
ctx.Set("myarray", gg)
res, err := Render(input, ctx)
r.NoError(err)
r.Equal("Hello World", res)
}
func Test_Render_Struct_Nested_Map_Slice_Access(t *testing.T) {
r := require.New(t)
type d struct {
Final string
}
type c struct {
He map[string]d
}
type b struct {
A []c
}
type mylist struct {
Name []b
}
input := `<%= myarray[0].Name[0].A[1].He["test"].Final %>`
gg := make([]mylist, 3)
var bc b
var ca c
ca.He = make(map[string]d)
ca.He["test"] = d{"Hello World"}
bc.A = make([]c, 3)
bc.A[1] = ca
gg[0].Name = []b{bc}
ctx := NewContext()
ctx.Set("myarray", gg)
res, err := Render(input, ctx)
r.NoError(err)
r.Equal("Hello World", res)
}
func Test_Render_Struct_Nested_With_Unexported_Fields(t *testing.T) {
r := require.New(t)
type people struct {
FirstName string
LastName string
}
type employee struct {
employee []people
}
departments := make(map[string]employee)
var joh people
joh.FirstName = "John"
joh.LastName = "Doe"
var jane people
jane.FirstName = "Jane"
jane.LastName = "Dolittle"
var employees employee
employees.employee = []people{joh, jane}
departments["HR"] = employees
input := `<%= departments["HR"].employee[0].FirstName %>`
ctx := NewContext()
ctx.Set("departments", departments)
_, err := Render(input, ctx)
r.Error(err)
//r.Equal("John", res)
}
func Test_Render_Struct_Nested_Map_Access(t *testing.T) {
r := require.New(t)
type people struct {
FirstName string
LastName string
}
type employee struct {
Employee []people
}
departments := make(map[string]employee)
var joh people
joh.FirstName = "John"
joh.LastName = "Doe"
var jane people
jane.FirstName = "Jane"
jane.LastName = "Dolittle"
var employees employee
employees.Employee = []people{joh, jane}
departments["HR"] = employees
input := `<%= departments["HR"].Employee[0].FirstName %> <%= departments["HR"].Employee[0].LastName %>`
ctx := NewContext()
ctx.Set("departments", departments)
res, err := Render(input, ctx)
r.NoError(err)
r.Equal("John Doe", res)
input = `<%= departments["HR"].Employee[1].FirstName %> <%= departments["HR"].Employee[1].LastName %>`
res, err = Render(input, ctx)
r.NoError(err)
r.Equal("Jane Dolittle", res)
input = `<%= departments["HR"].Employee[1].FirstName %> <%= departments["HR"].Employee[0].LastName %>`
res, err = Render(input, ctx)
r.NoError(err)
r.Equal("Jane Doe", res)
input = `<%= departments["HR"].Employee[0].FirstName %> <%= departments["HR"].Employee[1].LastName %>`
res, err = Render(input, ctx)
r.NoError(err)
r.Equal("John Dolittle", res)
}