-
Notifications
You must be signed in to change notification settings - Fork 56
/
struct_test.go
589 lines (490 loc) · 11.7 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package plush_test
import (
"strings"
"testing"
"time"
"github.com/gobuffalo/plush/v5"
"github.com/stretchr/testify/require"
)
func Test_Render_Struct_Attribute(t *testing.T) {
r := require.New(t)
input := `<%= f.Name %>`
ctx := plush.NewContext()
f := struct {
Name string
}{"Mark"}
ctx.Set("f", f)
s, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("Mark", s)
}
func Test_Render_UnknownAttribute_on_Callee(t *testing.T) {
r := require.New(t)
ctx := plush.NewContext()
ctx.Set("m", struct{}{})
input := `<%= m.Foo %>`
_, err := plush.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 := plush.NewContext()
bender := Robot{
Avatar: Avatar("bender.jpg"),
}
ctx.Set("robot", bender)
input := `<%= robot.Avatar.URL() %>`
s, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("BENDER.JPG", s)
}
func Test_Render_Struct_PointerMethod(t *testing.T) {
r := require.New(t)
ctx := plush.NewContext()
robot := Robot{name: "robot"}
t.Run("ByValue", func(t *testing.T) {
ctx.Set("robot", robot)
input := `<%= robot.Name() %>`
s, err := plush.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 := plush.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 := plush.NewContextWith(map[string]interface{}{"p": p})
res, err := plush.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 := plush.NewContextWith(map[string]interface{}{
"user": u,
})
input := `<%= user.Name %>: <%= user.Image %>`
res, err := plush.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 := plush.NewContextWith(map[string]interface{}{
"user": u,
})
input := `<%= user.Name %>: <%= user.Image %>`
res, err := plush.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 := plush.NewContext()
ctx.Set("myarray", gg)
res, err := plush.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 := plush.NewContext()
ctx.Set("myarray", gg)
res, err := plush.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 := plush.NewContext()
ctx.Set("myarray", gg)
res, err := plush.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 := plush.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 := plush.Render(input, ctx)
r.NoError(err)
r.Equal("bendser.jpg", s)
}
func Test_Render_Nested_Structs(t *testing.T) {
r := require.New(t)
ctx := plush.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 := plush.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 := plush.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 := plush.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 := plush.NewContext()
ctx.Set("myarray", gg)
res, err := plush.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 := plush.NewContext()
ctx.Set("myarray", gg)
res, err := plush.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 := plush.NewContext()
ctx.Set("departments", departments)
_, err := plush.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 := plush.NewContext()
ctx.Set("departments", departments)
res, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("John Doe", res)
input = `<%= departments["HR"].Employee[1].FirstName %> <%= departments["HR"].Employee[1].LastName %>`
res, err = plush.Render(input, ctx)
r.NoError(err)
r.Equal("Jane Dolittle", res)
input = `<%= departments["HR"].Employee[1].FirstName %> <%= departments["HR"].Employee[0].LastName %>`
res, err = plush.Render(input, ctx)
r.NoError(err)
r.Equal("Jane Doe", res)
input = `<%= departments["HR"].Employee[0].FirstName %> <%= departments["HR"].Employee[1].LastName %>`
res, err = plush.Render(input, ctx)
r.NoError(err)
r.Equal("John Dolittle", res)
}
type person struct {
likes []string
hates []string
born time.Time
}
func (a person) GetAge() time.Duration {
return time.Since(a.born)
}
func (a person) GetBorn() time.Time {
return a.born
}
func (a person) Hates() []string {
return a.hates
}
func (a person) Likes() []string {
return a.likes
}
func Test_Render_Struct_With_ChainingFunction_ArrayAccess(t *testing.T) {
r := require.New(t)
tt := person{likes: []string{"pringles", "galaxy", "carrot cake", "world pendant", "gold braclet"},
hates: []string{"boiled eggs", "coconut"}}
input := `<%= nour.Likes()[0] %>`
ctx := plush.NewContext()
ctx.Set("nour", tt)
res, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("pringles", res)
}
func Test_Render_Struct_With_ChainingFunction_ArrayAccess_Outofbound(t *testing.T) {
r := require.New(t)
tt := person{likes: []string{"pringles", "galaxy", "carrot cake", "world pendant", "gold bracelet"},
hates: []string{"boiled eggs", "coconut"}}
input := `<%= nour.Hates()[30] %>`
ctx := plush.NewContext()
ctx.Set("nour", tt)
_, err := plush.Render(input, ctx)
r.Error(err)
}
func Test_Render_Struct_With_ChainingFunction_FunctionCall(t *testing.T) {
r := require.New(t)
tt := person{born: time.Date(2024, time.January, 11, 0, 0, 0, 0, time.UTC).AddDate(-31, 0, 0)}
input := `<%= nour.GetBorn().Format("Jan 2, 2006") %>`
ctx := plush.NewContext()
ctx.Set("nour", tt)
res, err := plush.Render(input, ctx)
r.NoError(err)
r.Equal("Jan 11, 1993", res)
}
func Test_Render_Struct_With_ChainingFunction_UndefinedStructProperty(t *testing.T) {
r := require.New(t)
tt := person{born: time.Now()}
input := `<%= nour.GetBorn().TEST %>`
ctx := plush.NewContext()
ctx.Set("nour", tt)
_, err := plush.Render(input, ctx)
r.Error(err)
}
func Test_Render_Struct_With_ChainingFunction_InvalidFunctionCall(t *testing.T) {
r := require.New(t)
tt := person{born: time.Now()}
input := `<%= nour.GetBorn().TEST("Jan 2, 2006") %>`
ctx := plush.NewContext()
ctx.Set("nour", tt)
_, err := plush.Render(input, ctx)
r.Error(err)
r.Contains(err.Error(), "'nour.GetBorn' does not have a method named 'TEST' (nour.GetBorn.TEST)")
}
func Test_Render_Function_on_Invalid_Function_Struct(t *testing.T) {
r := require.New(t)
ctx := plush.NewContext()
bender := Robot{
Avatar: Avatar("bender.jpg"),
}
ctx.Set("robot", bender)
input := `<%= robot.Avatar.URL2() %>`
_, err := plush.Render(input, ctx)
r.Error(err)
}
func Test_Render_Struct_Nested_Slice_Access_Out_Of_Range(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
gg[0].Name = []b{bc}
ctx := plush.NewContext()
ctx.Set("myarray", gg)
res, err := plush.Render(input, ctx)
r.Error(err)
r.Empty(res)
r.Error(err, "line 1: array index out of bounds, got index 1, while array size is 0")
}