-
Notifications
You must be signed in to change notification settings - Fork 114
/
decode_array_test.go
720 lines (670 loc) · 19.3 KB
/
decode_array_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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
package gojay
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type testSliceInts []int
func (t *testSliceInts) UnmarshalJSONArray(dec *Decoder) error {
i := 0
if err := dec.AddInt(&i); err != nil {
return err
}
*t = append(*t, i)
return nil
}
func TestSliceInts(t *testing.T) {
testCases := []struct {
name string
json string
expectedResult testSliceInts
err bool
errType interface{}
}{
{
name: "basic-test",
json: "[1,2,3,43567788543,45777655,432,0]",
expectedResult: testSliceInts{1, 2, 3, 43567788543, 45777655, 432, 0},
},
{
name: "basic-test",
json: "[1,2,3,43567788543,null,432,0]",
expectedResult: testSliceInts{1, 2, 3, 43567788543, 0, 432, 0},
},
{
name: "empty",
json: "[]",
expectedResult: testSliceInts{},
},
{
name: "invalid-json",
json: "[",
expectedResult: testSliceInts{},
err: true,
},
{
name: "floats",
json: "[1,2,3,43567788543,457.7765,432,0,0.45]",
expectedResult: testSliceInts{1, 2, 3, 43567788543, 457, 432, 0, 0},
},
{
name: "invalid-type",
json: `[1,2,3,43567788543,457.7765,432,0,"test"]`,
expectedResult: testSliceInts{1, 2, 3, 43567788543, 457, 432, 0, 0},
err: true,
errType: InvalidUnmarshalError(""),
},
{
name: "invalid-json",
json: `[1,2,3",43567788543,457.7765,432,0,"test"]`,
expectedResult: testSliceInts{1, 2, 3, 43567788543, 457, 432, 0, 0},
err: true,
errType: InvalidJSONError(""),
},
}
for _, testCase := range testCases {
s := make(testSliceInts, 0)
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
err := dec.Decode(&s)
if testCase.err {
assert.NotNil(t, err, "err should not be nil")
if testCase.errType != nil {
assert.IsType(t, testCase.errType, err, "err should be of the given type")
}
continue
}
for k, v := range testCase.expectedResult {
assert.Equal(t, v, s[k], "value at given index should be the same as expected results")
}
}
}
type testSliceStrings []string
func (t *testSliceStrings) UnmarshalJSONArray(dec *Decoder) error {
str := ""
if err := dec.AddString(&str); err != nil {
return err
}
*t = append(*t, str)
return nil
}
func TestSliceStrings(t *testing.T) {
testCases := []struct {
name string
json string
expectedResult testSliceStrings
err bool
errType interface{}
}{
{
name: "basic-test",
json: `["hello world", "hey" , "foo","bar"]`,
expectedResult: testSliceStrings{"hello world", "hey", "foo", "bar"},
},
{
name: "basic-test",
json: `["hello world", "hey" , "foo","bar \n escape"]`,
expectedResult: testSliceStrings{"hello world", "hey", "foo", "bar \n escape"},
},
{
name: "basic-test",
json: `["hello world", "hey" , null,"bar \n escape"]`,
expectedResult: testSliceStrings{"hello world", "hey", "", "bar \n escape"},
},
{
name: "invalid-type",
json: `["foo",1,2,3,"test"]`,
expectedResult: testSliceStrings{},
err: true,
errType: InvalidUnmarshalError(""),
},
{
name: "invalid-json",
json: `["hello world]`,
expectedResult: testSliceStrings{},
err: true,
errType: InvalidJSONError(""),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
s := make(testSliceStrings, 0)
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
err := dec.Decode(&s)
if testCase.err {
assert.NotNil(t, err, "err should not be nil")
if testCase.errType != nil {
assert.IsType(t, testCase.errType, err, "err should be of the given type")
}
return
}
assert.Nil(t, err, "err should be nil")
for k, v := range testCase.expectedResult {
assert.Equal(t, v, s[k], "value at given index should be the same as expected results")
}
})
}
}
type testSliceBools []bool
func (t *testSliceBools) UnmarshalJSONArray(dec *Decoder) error {
b := false
if err := dec.AddBool(&b); err != nil {
return err
}
*t = append(*t, b)
return nil
}
func TestSliceBools(t *testing.T) {
testCases := []struct {
name string
json string
expectedResult testSliceBools
err bool
errType interface{}
}{
{
name: "basic-test",
json: `[true, false, false, true, true, false]`,
expectedResult: testSliceBools{true, false, false, true, true, false},
},
{
name: "basic-test2",
json: `[true, false, false, true, null,null,true,false]`,
expectedResult: testSliceBools{true, false, false, true, false, false, true, false},
},
{
name: "invalid-type",
json: `["foo",1,2,3,"test"]`,
expectedResult: testSliceBools{},
err: true,
errType: InvalidUnmarshalError(""),
},
{
name: "invalid-json",
json: `["hello world]`,
expectedResult: testSliceBools{},
err: true,
errType: InvalidJSONError(""),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
s := make(testSliceBools, 0)
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
err := dec.Decode(&s)
if testCase.err {
assert.NotNil(t, err, "err should not be nil")
if testCase.errType != nil {
assert.IsType(t, testCase.errType, err, "err should be of the given type")
}
return
}
assert.Nil(t, err, "err should be nil")
for k, v := range testCase.expectedResult {
assert.Equal(t, v, s[k], "value at given index should be the same as expected results")
}
})
}
}
type testSliceSlicesSlices []testSliceInts
func (t *testSliceSlicesSlices) UnmarshalJSONArray(dec *Decoder) error {
sl := make(testSliceInts, 0)
if err := dec.AddArray(&sl); err != nil {
return err
}
*t = append(*t, sl)
return nil
}
func TestSliceSlices(t *testing.T) {
testCases := []struct {
name string
json string
expectedResult testSliceSlicesSlices
err bool
errType interface{}
}{
{
name: "basic-test",
json: `[[1,2],[1,2],[1,2]]`,
expectedResult: testSliceSlicesSlices{testSliceInts{1, 2}, testSliceInts{1, 2}, testSliceInts{1, 2}},
},
{
name: "basic-test",
json: `[[1,2],null,[1,2]]`,
expectedResult: testSliceSlicesSlices{testSliceInts{1, 2}, testSliceInts{}, testSliceInts{1, 2}},
},
{
name: "invalid-type",
json: `["foo",1,2,3,"test"]`,
expectedResult: testSliceSlicesSlices{},
err: true,
errType: InvalidUnmarshalError(""),
},
{
name: "invalid-json",
json: `["hello world]`,
expectedResult: testSliceSlicesSlices{},
err: true,
errType: InvalidJSONError(""),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
s := make(testSliceSlicesSlices, 0)
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
err := dec.Decode(&s)
if testCase.err {
assert.NotNil(t, err, "err should not be nil")
if testCase.errType != nil {
assert.IsType(t, testCase.errType, err, "err should be of the given type")
}
return
}
assert.Nil(t, err, "err should be nil")
for k, v := range testCase.expectedResult {
assert.Equal(t, v, s[k], "value at given index should be the same as expected results")
}
})
}
}
type testSliceObjects []*testObject
func (t *testSliceObjects) UnmarshalJSONArray(dec *Decoder) error {
obj := &testObject{}
*t = append(*t, obj)
return dec.AddObject(obj)
}
func TestSliceObjects(t *testing.T) {
testCases := []struct {
name string
json string
expectedResult testSliceObjects
err bool
errType interface{}
}{
{
name: "basic-test",
json: `[{"testStr":"foo bar","testInt":123},{"testStr":"foo bar","testInt":123}]`,
expectedResult: testSliceObjects{
&testObject{
testStr: "foo bar",
testInt: 123,
},
&testObject{
testStr: "foo bar",
testInt: 123,
},
},
},
{
name: "basic-test",
json: `[{"testStr":"foo bar","testInt":123},null,{"testStr":"foo bar","testInt":123}]`,
expectedResult: testSliceObjects{
&testObject{
testStr: "foo bar",
testInt: 123,
},
&testObject{},
&testObject{
testStr: "foo bar",
testInt: 123,
},
},
},
{
name: "invalid-type",
json: `["foo",1,2,3,"test"]`,
expectedResult: testSliceObjects{},
err: true,
errType: InvalidUnmarshalError(""),
},
{
name: "invalid-json",
json: `["hello world]`,
expectedResult: testSliceObjects{},
err: true,
errType: InvalidJSONError(""),
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
s := make(testSliceObjects, 0)
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
err := dec.Decode(&s)
if testCase.err {
assert.NotNil(t, err, "err should not be nil")
if testCase.errType != nil {
assert.IsType(t, testCase.errType, err, "err should be of the given type")
}
return
}
assert.Nil(t, err, "err should be nil")
for k, v := range testCase.expectedResult {
assert.Equal(t, *v, *s[k], "value at given index should be the same as expected results")
}
})
}
}
type ArrayNull []string
func (a *ArrayNull) UnmarshalJSONArray(dec *Decoder) error {
var str string
if err := dec.String(&str); err != nil {
return err
}
*a = append(*a, str)
return nil
}
type ObjectArrayNull struct {
SubArray *ArrayNull
}
func (o *ObjectArrayNull) UnmarshalJSONObject(dec *Decoder, k string) error {
switch k {
case "subarray":
return dec.ArrayNull(&o.SubArray)
}
return nil
}
func (o *ObjectArrayNull) NKeys() int {
return 1
}
func TestDecodeArrayNullPtr(t *testing.T) {
t.Run("sub obj should not be nil", func(t *testing.T) {
var o = &ObjectArrayNull{}
var err = UnmarshalJSONObject([]byte(`{"subarray": ["test"]}`), o)
assert.Nil(t, err)
assert.NotNil(t, o.SubArray)
assert.Len(t, *o.SubArray, 1)
})
t.Run("sub array should be nil", func(t *testing.T) {
var o = &ObjectArrayNull{}
var err = UnmarshalJSONObject([]byte(`{"subarray": null}`), o)
assert.Nil(t, err)
assert.Nil(t, o.SubArray)
})
t.Run("sub array err, not closing arr", func(t *testing.T) {
var o = &ObjectArrayNull{}
var err = UnmarshalJSONObject([]byte(`{"subarray": [ `), o)
assert.NotNil(t, err)
})
t.Run("sub array err, invalid string", func(t *testing.T) {
var o = &ObjectArrayNull{}
var err = UnmarshalJSONObject([]byte(`{"subarray":[",]}`), o)
assert.NotNil(t, err)
})
t.Run("sub array err, invalid null", func(t *testing.T) {
var o = &ObjectArrayNull{}
var err = UnmarshalJSONObject([]byte(`{"subarray":nll}`), o)
assert.NotNil(t, err)
})
t.Run("sub array err, empty", func(t *testing.T) {
var o = &ObjectArrayNull{}
var err = UnmarshalJSONObject([]byte(`{"subarray":`), o)
assert.NotNil(t, err)
})
}
type testChannelArray chan *TestObj
func (c *testChannelArray) UnmarshalJSONArray(dec *Decoder) error {
obj := &TestObj{}
if err := dec.AddObject(obj); err != nil {
return err
}
*c <- obj
return nil
}
func TestDecoderSliceNull(t *testing.T) {
json := []byte(`null`)
v := &testSliceStrings{}
err := Unmarshal(json, v)
assert.Nil(t, err, "Err must be nil")
assert.Equal(t, len(*v), 0, "v must be of len 0")
}
func TestDecodeSliceInvalidType(t *testing.T) {
result := testSliceObjects{}
err := UnmarshalJSONArray([]byte(`{}`), &result)
assert.NotNil(t, err, "err should not be nil")
assert.IsType(t, InvalidUnmarshalError(""), err, "err should be of type InvalidUnmarshalError")
assert.Equal(t, "Cannot unmarshal JSON to type '*gojay.testSliceObjects'", err.Error(), "err should not be nil")
}
func TestDecoderChannelOfObjectsBasic(t *testing.T) {
json := []byte(`[
{
"test": 245,
"test2": -246,
"test3": "string"
},
{
"test": 247,
"test2": 248,
"test3": "string"
},
{
"test": 777,
"test2": 456,
"test3": "string"
}
]`)
testChan := testChannelArray(make(chan *TestObj, 3))
err := UnmarshalJSONArray(json, &testChan)
assert.Nil(t, err, "Err must be nil")
ct := 0
l := len(testChan)
for range testChan {
ct++
if ct == l {
break
}
}
assert.Equal(t, ct, 3)
}
func TestDecoderSliceInvalidJSON(t *testing.T) {
json := []byte(`hello`)
testArr := testSliceInts{}
err := UnmarshalJSONArray(json, &testArr)
assert.NotNil(t, err, "Err must not be nil as JSON is invalid")
assert.IsType(t, InvalidJSONError(""), err, "err message must be 'Invalid JSON'")
}
func TestDecoderSliceDecoderAPI(t *testing.T) {
json := `["string","string1"]`
testArr := testSliceStrings{}
dec := NewDecoder(strings.NewReader(json))
err := dec.DecodeArray(&testArr)
assert.Nil(t, err, "Err must be nil")
assert.Len(t, testArr, 2, "testArr should be of len 2")
assert.Equal(t, "string", testArr[0], "testArr[0] should be 'string'")
assert.Equal(t, "string1", testArr[1], "testArr[1] should be 'string1'")
}
func TestDecoderSliceDecoderAPIError(t *testing.T) {
testArr := testSliceInts{}
dec := NewDecoder(strings.NewReader(`hello`))
err := dec.DecodeArray(&testArr)
assert.NotNil(t, err, "Err must not be nil as JSON is invalid")
assert.IsType(t, InvalidJSONError(""), err, "err message must be 'Invalid JSON'")
}
func TestUnmarshalJSONArrays(t *testing.T) {
testCases := []struct {
name string
v UnmarshalerJSONArray
d []byte
expectations func(err error, v interface{}, t *testing.T)
}{
{
v: new(testDecodeSlice),
d: []byte(`[{"test":"test"}]`),
name: "test decode slice",
expectations: func(err error, v interface{}, t *testing.T) {
vtPtr := v.(*testDecodeSlice)
vt := *vtPtr
assert.Nil(t, err, "err must be nil")
assert.Len(t, vt, 1, "len of vt must be 1")
assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
},
},
{
v: new(testDecodeSlice),
d: []byte(`[{"test":"test"},{"test":"test2"}]`),
name: "test decode slice",
expectations: func(err error, v interface{}, t *testing.T) {
vtPtr := v.(*testDecodeSlice)
vt := *vtPtr
assert.Nil(t, err, "err must be nil")
assert.Len(t, vt, 2, "len of vt must be 2")
assert.Equal(t, "test", vt[0].test, "vt[0].test must be equal to 'test'")
assert.Equal(t, "test2", vt[1].test, "vt[1].test must be equal to 'test2'")
},
},
{
v: new(testDecodeSlice),
d: []byte(`invalid json`),
name: "test decode object null",
expectations: func(err error, v interface{}, t *testing.T) {
assert.NotNil(t, err, "err must not be nil")
assert.IsType(t, InvalidJSONError(""), err, "err must be of type InvalidJSONError")
},
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(*testing.T) {
err := UnmarshalJSONArray(testCase.d, testCase.v)
testCase.expectations(err, testCase.v, t)
})
}
}
func TestSkipArray(t *testing.T) {
testCases := []struct {
name string
json string
expectations func(*testing.T, int, error)
}{
{
name: "basic",
json: `"testbasic"]`,
expectations: func(t *testing.T, i int, err error) {
assert.Equal(t, len(`"testbasic"]`), i)
assert.Nil(t, err)
},
},
{
name: "complex escape string",
json: `"test \\\\\" escape"]`,
expectations: func(t *testing.T, i int, err error) {
assert.Equal(t, len(`"test \\\\\" escape"]`), i)
assert.Nil(t, err)
},
},
{
name: "complex escape slash",
json: `"test \\\\\\"]`,
expectations: func(t *testing.T, i int, err error) {
assert.Equal(t, len(`"test \\\\\\"]`), i)
assert.Nil(t, err)
},
},
{
json: `"test \n"]`,
expectations: func(t *testing.T, i int, err error) {
assert.Equal(t, len(`"test \n"]`), i)
assert.Nil(t, err)
},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
dec := NewDecoder(strings.NewReader(test.json))
i, err := dec.skipArray()
test.expectations(t, i, err)
})
}
}
func TestDecodeArrayEmpty(t *testing.T) {
v := new(testDecodeSlice)
dec := NewDecoder(strings.NewReader(""))
err := dec.Decode(v)
assert.NotNil(t, err, "err should not be nil")
assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
}
func TestDecodeArraySkipError(t *testing.T) {
v := new(testDecodeSlice)
dec := NewDecoder(strings.NewReader("34fef"))
err := dec.Decode(v)
assert.NotNil(t, err, "err should not be nil")
assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
}
func TestDecodeArrayNullError(t *testing.T) {
v := new(testDecodeSlice)
dec := NewDecoder(strings.NewReader("nall"))
err := dec.Decode(v)
assert.NotNil(t, err, "err should not be nil")
assert.IsType(t, InvalidJSONError(""), err, "err should be of type InvalidJSONError")
}
func TestDecoderArrayFunc(t *testing.T) {
var f DecodeArrayFunc
assert.True(t, f.IsNil())
}
type testArrayStrings [3]string
func (a *testArrayStrings) UnmarshalJSONArray(dec *Decoder) error {
var str string
if err := dec.String(&str); err != nil {
return err
}
a[dec.Index()] = str
return nil
}
func TestArrayStrings(t *testing.T) {
data := []byte(`["a", "b", "c"]`)
arr := testArrayStrings{}
err := Unmarshal(data, &arr)
assert.Nil(t, err, "err must be nil")
assert.Equal(t, "a", arr[0], "arr[0] must be equal to 'a'")
assert.Equal(t, "b", arr[1], "arr[1] must be equal to 'b'")
assert.Equal(t, "c", arr[2], "arr[2] must be equal to 'c'")
}
type testSliceArraysStrings struct {
arrays []testArrayStrings
t *testing.T
}
func (s *testSliceArraysStrings) UnmarshalJSONArray(dec *Decoder) error {
var a testArrayStrings
assert.Equal(s.t, len(s.arrays), dec.Index(), "decoded array index must be equal to current slice len")
if err := dec.AddArray(&a); err != nil {
return err
}
assert.Equal(s.t, len(s.arrays), dec.Index(), "decoded array index must be equal to current slice len")
s.arrays = append(s.arrays, a)
return nil
}
func TestIndex(t *testing.T) {
testCases := []struct {
name string
json string
expectedResult []testArrayStrings
}{
{
name: "basic-test",
json: `[["a","b","c"],["1","2","3"],["x","y","z"]]`,
expectedResult: []testArrayStrings{{"a", "b", "c"}, {"1", "2", "3"}, {"x", "y", "z"}},
},
{
name: "basic-test-null",
json: `[["a","b","c"],null,["x","y","z"]]`,
expectedResult: []testArrayStrings{{"a", "b", "c"}, {"", "", ""}, {"x", "y", "z"}},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
s := make([]testArrayStrings, 0)
dec := BorrowDecoder(strings.NewReader(testCase.json))
defer dec.Release()
a := testSliceArraysStrings{arrays: s, t: t}
err := dec.Decode(&a)
assert.Nil(t, err, "err should be nil")
assert.Zero(t, dec.Index(), "Index() must return zero after decoding")
for k, v := range testCase.expectedResult {
assert.Equal(t, v, a.arrays[k], "value at given index should be the same as expected results")
}
})
}
}