-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnode_test.go
564 lines (505 loc) · 11.7 KB
/
node_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
package conf
import (
"fmt"
"reflect"
"strings"
"testing"
"time"
"github.com/segmentio/objconv/json"
)
func TestEqualNode(t *testing.T) {
now := time.Now()
tests := []struct {
name string
node1 Node
node2 Node
equal bool
}{
{
name: "nil nodes",
node1: nil,
node2: nil,
equal: true,
},
{
name: "scalar and nil",
node1: Scalar{},
node2: nil,
equal: false,
},
{
name: "two empty scalars",
node1: Scalar{},
node2: Scalar{},
equal: true,
},
{
name: "42 and empty scalar",
node1: Scalar{reflect.ValueOf(42)},
node2: Scalar{},
equal: false,
},
{
name: "empty scalar and 42",
node1: Scalar{},
node2: Scalar{reflect.ValueOf(42)},
equal: false,
},
{
name: "42 and 42",
node1: Scalar{reflect.ValueOf(42)},
node2: Scalar{reflect.ValueOf(42)},
equal: true,
},
{
name: "42 and empty array",
node1: Scalar{reflect.ValueOf(42)},
node2: Array{},
equal: false,
},
{
name: "non-equal scalars (type mismatch)",
node1: Scalar{reflect.ValueOf(42)},
node2: Scalar{reflect.ValueOf("Hello World!")},
equal: false,
},
{
name: "equal scalars (time values)",
node1: Scalar{reflect.ValueOf(now)},
node2: Scalar{reflect.ValueOf(now.In(time.UTC))},
equal: true,
},
{
name: "two empty arrays",
node1: Array{},
node2: Array{},
equal: true,
},
{
name: "equal non-empty arrays",
node1: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(2)},
Scalar{reflect.ValueOf(3)},
)},
node2: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(2)},
Scalar{reflect.ValueOf(3)},
)},
equal: true,
},
{
name: "non-equal arrays (value mismatch)",
node1: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(2)},
Scalar{reflect.ValueOf(3)},
)},
node2: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(1)},
)},
equal: false,
},
{
name: "non-equal arrays (length mismatch)",
node1: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(2)},
Scalar{reflect.ValueOf(3)},
)},
node2: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(2)},
)},
equal: false,
},
{
name: "two empty maps",
node1: Map{},
node2: Map{},
equal: true,
},
{
name: "equal non-empty maps",
node1: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(3)}},
)},
node2: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(3)}},
)},
equal: true,
},
{
name: "non-equal maps (value mismatch)",
node1: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(3)}},
)},
node2: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(1)}},
)},
equal: false,
},
{
name: "non-equal maps (value not found)",
node1: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(3)}},
)},
node2: Map{items: newMapItems(
MapItem{Name: "D", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "E", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "F", Value: Scalar{reflect.ValueOf(3)}},
)},
equal: false,
},
{
name: "non-equal maps (length mismatch)",
node1: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(3)}},
)},
node2: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
)},
equal: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if equal := EqualNode(test.node1, test.node2); equal != test.equal {
t.Errorf("EqualNode: expected %t but found %t", test.equal, equal)
}
})
}
}
func TestMakeNode(t *testing.T) {
now := time.Now()
tests := []struct {
name string
value interface{}
node Node
}{
{
name: "nil",
value: nil,
node: Scalar{reflect.ValueOf(nil)},
},
{
name: "scalar (integer)",
value: 42,
node: Scalar{reflect.ValueOf(42)},
},
{
name: "scalar (time)",
value: now,
node: Scalar{reflect.ValueOf(now)},
},
{
name: "slice",
value: []int{1, 2, 3},
node: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(2)},
Scalar{reflect.ValueOf(3)},
)},
},
{
name: "map",
value: map[string]int{"A": 1, "B": 2, "C": 3},
node: Map{items: newMapItems(
MapItem{Name: "A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(3)}},
)},
},
{
name: "struct",
value: struct {
A int `conf:"a" help:"value of A"` // override name
B int `conf:"-" help:"value of B"` // skip
C int // default name
D *int // allocate the pointer
e int // unexported
}{1, 2, 3, nil, 42},
node: Map{items: newMapItems(
MapItem{Name: "a", Help: "value of A", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "C", Value: Scalar{reflect.ValueOf(3)}},
MapItem{Name: "D", Value: Scalar{reflect.ValueOf(0)}},
)},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if node := MakeNode(test.value); !EqualNode(node, test.node) {
t.Errorf("\n<<< %s\n>>> %s", test.node, node)
}
})
}
}
func TestNodeValue(t *testing.T) {
tests := []struct {
node Node
value interface{}
}{
{
node: Scalar{},
value: nil,
},
{
node: Scalar{reflect.ValueOf(42)},
value: 42,
},
{
node: Array{},
value: nil,
},
{
node: Array{value: reflect.ValueOf([]int{1, 2, 3})},
value: []int{1, 2, 3},
},
{
node: Map{},
value: nil,
},
{
node: Map{value: reflect.ValueOf(map[string]int{"A": 1, "B": 2, "C": 3})},
value: map[string]int{"A": 1, "B": 2, "C": 3},
},
}
for _, test := range tests {
t.Run(fmt.Sprint(test.value), func(t *testing.T) {
if value := test.node.Value(); !reflect.DeepEqual(value, test.value) {
t.Error(value)
}
})
}
}
func TestNodeString(t *testing.T) {
date := time.Date(2016, 12, 31, 23, 42, 59, 0, time.UTC)
tests := []struct {
repr string
node Node
}{
{
repr: `null`,
node: Scalar{},
},
{
repr: `42`,
node: Scalar{reflect.ValueOf(42)},
},
{
repr: `Hello World!`,
node: Scalar{reflect.ValueOf("Hello World!")},
},
{
repr: `"2016-12-31T23:42:59Z"`,
node: Scalar{reflect.ValueOf(date)},
},
{
repr: `[ ]`,
node: Array{},
},
{
repr: `[1, 2, 3]`,
node: Array{items: newArrayItems(
Scalar{reflect.ValueOf(1)},
Scalar{reflect.ValueOf(2)},
Scalar{reflect.ValueOf(3)},
)},
},
{
repr: `{ }`,
node: Map{},
},
{
repr: `{ A: 1 (first), B: 2, C: 3 (last) }`,
node: Map{items: newMapItems(
MapItem{Name: "A", Help: "first", Value: Scalar{reflect.ValueOf(1)}},
MapItem{Name: "B", Value: Scalar{reflect.ValueOf(2)}},
MapItem{Name: "C", Help: "last", Value: Scalar{reflect.ValueOf(3)}},
)},
},
}
for _, test := range tests {
t.Run(test.repr, func(t *testing.T) {
if repr := test.node.String(); repr != test.repr {
t.Error("representation mismatch")
t.Logf("want: %v, %#v", reflect.TypeOf(test.repr), test.repr)
t.Logf("got: %v, %#v", reflect.TypeOf(repr), repr)
}
})
}
}
func TestNodeJSON(t *testing.T) {
tests := []struct {
node Node
json string
}{
{
node: MakeNode(nil),
json: `null`,
},
{
node: MakeNode(42),
json: `42`,
},
{
node: MakeNode("Hello World!"),
json: `"Hello World!"`,
},
{
node: MakeNode([]int{}),
json: `[]`,
},
{
node: MakeNode([]int{1, 2, 3}),
json: `[1,2,3]`,
},
{
node: MakeNode(map[string]int{}),
json: `{}`,
},
{
node: MakeNode(map[string]int{"A": 1, "B": 2, "C": 3}),
json: `{"A":1,"B":2,"C":3}`,
},
{
node: MakeNode(struct{ A, B, C int }{1, 2, 3}),
json: `{"A":1,"B":2,"C":3}`,
},
{
node: MakeNode(struct{ A []int }{[]int{1, 2, 3}}),
json: `{"A":[1,2,3]}`,
},
}
t.Run("Encode", func(t *testing.T) {
for _, test := range tests {
t.Run(test.json, func(t *testing.T) {
b, err := json.Marshal(test.node)
if err != nil {
t.Error(err)
}
if s := string(b); s != test.json {
t.Error(s)
}
})
}
})
t.Run("Decode", func(t *testing.T) {
for _, test := range tests {
t.Run(test.json, func(t *testing.T) {
if test.node.Value() == nil {
return // skip
}
value := reflect.New(reflect.TypeOf(test.node.Value()))
node := MakeNode(value.Interface())
if err := json.Unmarshal([]byte(test.json), &node); err != nil {
t.Error(err)
}
if !EqualNode(node, test.node) {
t.Errorf("%+v", node)
}
})
}
})
}
func Test_FlattenedEmbeddedStructs(t *testing.T) {
type Smallest struct {
SmallestOne string
}
type Small struct {
Smallest `conf:"_"`
SmallOne string
}
type Medium struct {
Small `conf:"_"`
MediumOne string
}
type Matroska struct {
Medium `conf:"_"`
LargeOne string
}
m := Matroska{}
node := makeNodeStruct(reflect.ValueOf(m), reflect.TypeOf(m))
if len(node.Items()) != 4 {
t.Errorf("expected to find four flattened fields...got %d", len(node.Items()))
}
for _, name := range []string{"SmallestOne", "SmallOne", "MediumOne", "LargeOne"} {
f := node.Item(name)
if f == nil {
t.Errorf("flattened field %s is missing", name)
}
if f.Kind() != ScalarNode {
t.Errorf("flattened field %s should have been scalar but was %d", name, f.Kind())
}
}
}
func Test_InvalidFlattenedEmbeddedStructs(t *testing.T) {
type Thing1 struct {
Stuff string
}
type Thing2 struct {
Stuff string
}
type ConflictingName struct {
Thing1 `conf:"_"`
Thing2 `conf:"_"`
}
type EmbedPrimitive struct {
Str string `conf:"_"`
}
type EmbedNamedStruct struct {
Thing Thing1 `conf:"_"`
}
tests := []struct {
val interface{}
errFragments []string
}{
{
val: ConflictingName{},
errFragments: []string{"'Stuff'", "duplicate"},
},
{
val: EmbedPrimitive{},
errFragments: []string{"\"_\"", "at path EmbedPrimitive.Str"},
},
{
val: EmbedNamedStruct{},
errFragments: []string{"\"_\"", "at path EmbedNamedStruct.Thing"},
},
}
for _, tt := range tests {
t.Run(reflect.TypeOf(tt.val).Name(), func(t *testing.T) {
defer func() {
recovered := recover()
msg, ok := recovered.(string)
if !ok {
t.Errorf("expected a string to be recovered...got %v", recovered)
}
// NOTE : ensure that the type name is included in the message!
for _, frag := range append(tt.errFragments, reflect.TypeOf(tt.val).Name()) {
if !strings.Contains(msg, frag) {
t.Errorf("message should have contained fragment \"%s\": %s", frag, msg)
}
}
}()
makeNodeStruct(reflect.ValueOf(tt.val), reflect.TypeOf(tt.val))
t.Error("test should have panicked")
})
}
}