-
Notifications
You must be signed in to change notification settings - Fork 139
/
result_test.go
899 lines (823 loc) · 28.8 KB
/
result_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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
package gojsonq
import (
"reflect"
"testing"
"time"
)
func TestNewResult(t *testing.T) {
result := NewResult("gojsonq")
if reflect.ValueOf(result).Type().String() != "*gojsonq.Result" {
t.Error("failed to match gojsonq.Result type")
}
}
func TestNil(t *testing.T) {
result := NewResult(nil)
if result.Nil() == false {
t.Error("failed to check Nil")
}
}
func TestAs(t *testing.T) {
type flt float64
testCases := []struct {
tag string
value interface{}
newExpectedValue func() interface{}
expect func(value, i interface{}) bool
errExpect bool
}{
{
tag: "float64 as int", // golang unmarshal number to float64
value: float64(1),
newExpectedValue: func() interface{} {
var a int
return &a
},
expect: func(value, i interface{}) bool {
val, ok := i.(int)
if !ok {
return false
}
return val == (value.(int))
},
errExpect: false,
},
{
tag: "float64 as uint",
value: float64(1),
newExpectedValue: func() interface{} {
var a uint
return &a
},
expect: func(value, i interface{}) bool {
val, ok := i.(uint)
if !ok {
return false
}
return val == (value.(uint))
},
errExpect: false,
},
{
tag: "float64 assign to non ptr",
value: float64(1),
newExpectedValue: func() interface{} {
var a float64
return a
},
expect: func(value, i interface{}) bool {
val, ok := i.(float64)
if !ok {
return false
}
return val == (value.(float64))
},
errExpect: true,
},
{
tag: "string assign to int",
value: string("*nop"),
newExpectedValue: func() interface{} {
var a float64
return &a
},
expect: func(value, i interface{}) bool {
val, ok := i.(string)
if !ok {
return false
}
return val == (value.(string))
},
errExpect: true,
},
{
tag: "custom type error check",
value: string("*nop"),
newExpectedValue: func() interface{} {
var a flt
return &a
},
expect: func(value, i interface{}) bool {
val, ok := i.(string)
if !ok {
return false
}
return val == (value.(string))
},
errExpect: true,
},
}
for _, tc := range testCases {
expected := tc.newExpectedValue()
err := NewResult(tc.value).As(expected)
if err != nil && !tc.errExpect {
t.Error(tc.tag, err)
}
if tc.expect(tc.value, expected) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.value, reflect.ValueOf(expected).Elem())
}
}
}
func TestBool(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect bool
errExpect bool
}{
{tag: "bool value as expected", value: true, valExpect: true, errExpect: false},
{tag: "invalid bool, error expected", value: 123, valExpect: false, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Bool()
if err != nil && !tc.errExpect {
t.Error("bool:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestTime(t *testing.T) {
layout := "2006-01-02T15:04:05.000Z"
str := "2014-11-12T11:45:26.371Z"
tm, err := time.Parse(layout, str)
if err != nil {
t.Error("failed to parse time:", err)
}
testCases := []struct {
tag string
value interface{}
valExpect time.Time
errExpect bool
}{
{tag: "time value as expected", value: "2014-11-12T11:45:26.371Z", valExpect: tm, errExpect: false},
{tag: "invalid time, error expected", value: "2014-11-12", valExpect: time.Time{}, errExpect: true},
{tag: "invalid time, error expected", value: 12322, valExpect: time.Time{}, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Time(layout)
if err != nil && !tc.errExpect {
t.Error("time:", err)
}
if !reflect.DeepEqual(v, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestDuration(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect time.Duration
errExpect bool
}{
{tag: "duration value as expected", value: "10s", valExpect: time.Duration(10 * time.Second), errExpect: false},
{tag: "duration value as expected", value: "10m", valExpect: time.Duration(10 * time.Minute), errExpect: false},
{tag: "duration value as expected", value: float64(10), valExpect: time.Duration(10 * time.Nanosecond), errExpect: false}, // go decode number to float64
{tag: "invalid duration, error expected", value: "1", valExpect: time.Duration(0), errExpect: true},
{tag: "invalid duration, error expected", value: 1, valExpect: time.Duration(0), errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Duration()
if err != nil && !tc.errExpect {
t.Error("duration:", err)
}
if !reflect.DeepEqual(v, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestString(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect string
errExpect bool
}{
{tag: "string value as expected", value: "hello", valExpect: "hello", errExpect: false},
{tag: "invalid string, error expected", value: 123, valExpect: "", errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).String()
if err != nil && !tc.errExpect {
t.Error("string: ", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestInt(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect int
errExpect bool
}{
{tag: "int value as expected", value: 123.8, valExpect: 123, errExpect: false},
{tag: "int value as expected", value: 12.3, valExpect: 12, errExpect: false},
{tag: "invalid int, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Int()
if err != nil && !tc.errExpect {
t.Error("int:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestInt8(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect int8
errExpect bool
}{
{tag: "int8 value as expected", value: 123.8, valExpect: int8(123), errExpect: false},
{tag: "int8 value as expected", value: 12.3, valExpect: int8(12), errExpect: false},
{tag: "invalid int8, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Int8()
if err != nil && !tc.errExpect {
t.Error("int8:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestInt16(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect int16
errExpect bool
}{
{tag: "int16 value as expected", value: 123.8, valExpect: int16(123), errExpect: false},
{tag: "int16 value as expected", value: 12.3, valExpect: int16(12), errExpect: false},
{tag: "invalid int16, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Int16()
if err != nil && !tc.errExpect {
t.Error("int16:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestInt32(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect int32
errExpect bool
}{
{tag: "int32 value as expected", value: 123.8, valExpect: int32(123), errExpect: false},
{tag: "int32 value as expected", value: 12.3, valExpect: int32(12), errExpect: false},
{tag: "invalid int32, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Int32()
if err != nil && !tc.errExpect {
t.Error("int32:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestInt64(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect int64
errExpect bool
}{
{tag: "int64 value as expected", value: 123.8, valExpect: int64(123), errExpect: false},
{tag: "int64 value as expected", value: 12.3, valExpect: int64(12), errExpect: false},
{tag: "invalid int64, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Int64()
if err != nil && !tc.errExpect {
t.Error("int64:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestUint(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect uint
errExpect bool
}{
{tag: "uint value as expected", value: 123.8, valExpect: uint(123), errExpect: false},
{tag: "uint value as expected", value: 12.3, valExpect: uint(12), errExpect: false},
{tag: "invalid uint, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Uint()
if err != nil && !tc.errExpect {
t.Error("uint:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestUint8(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect uint8
errExpect bool
}{
{tag: "uint8 value as expected", value: 123.8, valExpect: uint8(123), errExpect: false},
{tag: "uint8 value as expected", value: 12.3, valExpect: uint8(12), errExpect: false},
{tag: "invalid uint8, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Uint8()
if err != nil && !tc.errExpect {
t.Error("uint8:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestUint16(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect uint16
errExpect bool
}{
{tag: "uint16 value as expected", value: 123.8, valExpect: uint16(123), errExpect: false},
{tag: "uint16 value as expected", value: 12.3, valExpect: uint16(12), errExpect: false},
{tag: "invalid uint16, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Uint16()
if err != nil && !tc.errExpect {
t.Error("uint16:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestUint32(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect uint32
errExpect bool
}{
{tag: "uint32 value as expected", value: 123.8, valExpect: uint32(123), errExpect: false},
{tag: "uint32 value as expected", value: 12.3, valExpect: uint32(12), errExpect: false},
{tag: "invalid uint32, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Uint32()
if err != nil && !tc.errExpect {
t.Error("uint32:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestUint64(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect uint64
errExpect bool
}{
{tag: "uint64 value as expected", value: 123.8, valExpect: uint64(123), errExpect: false},
{tag: "uint64 value as expected", value: 12.3, valExpect: uint64(12), errExpect: false},
{tag: "invalid uint64, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Uint64()
if err != nil && !tc.errExpect {
t.Error("uint64:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestFloat32(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect float32
errExpect bool
}{
{tag: "float32 value as expected", value: 123.8, valExpect: float32(123.8), errExpect: false},
{tag: "float32 value as expected", value: 12.3, valExpect: float32(12.3), errExpect: false},
{tag: "invalid float32, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Float32()
if err != nil && !tc.errExpect {
t.Error("float32:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestFloat64(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect float64
errExpect bool
}{
{tag: "float64 value as expected", value: 123.8, valExpect: float64(123.8), errExpect: false},
{tag: "float64 value as expected", value: 12.3, valExpect: float64(12.3), errExpect: true},
{tag: "invalid float64, error expected", value: "123", valExpect: 0, errExpect: true},
}
for _, tc := range testCases {
v, err := NewResult(tc.value).Float64()
if err != nil && !tc.errExpect {
t.Error("float64:", err)
}
if v != tc.valExpect && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, v)
}
}
}
func TestBoolSlice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []bool
errExpect bool
}{
{tag: "boolSlice value as expected", value: []interface{}{true, false}, valExpect: []bool{true, false}, errExpect: false},
{tag: "boolSlice value as expected", value: []interface{}{false, true, true}, valExpect: []bool{false, true, true}, errExpect: false},
{tag: "invalid boolSlice, error expected", value: []interface{}{1, 3}, valExpect: []bool{}, errExpect: false},
{tag: "invalid boolSlice, error expected", value: []int{1, 3}, valExpect: []bool{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).BoolSlice()
if err != nil && !tc.errExpect {
t.Error("boolSlice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestTimelice(t *testing.T) {
layout := "2006-01-02T15:04:05.000Z"
t1, err1 := time.Parse(layout, "2014-11-12T11:45:26.371Z")
if err1 != nil {
t.Error("failed to parse time1:", err1)
}
t2, err2 := time.Parse(layout, "2019-11-12T11:45:26.371Z")
if err2 != nil {
t.Error("failed to parse time2:", err2)
}
testCases := []struct {
tag string
value interface{}
timeLayout string
valExpect []time.Time
errExpect bool
}{
{tag: "timeSlice value as expected", value: []interface{}{"2014-11-12T11:45:26.371Z", "2019-11-12T11:45:26.371Z"}, timeLayout: layout, valExpect: []time.Time{t1, t2}, errExpect: false},
{tag: "invalid timeSlice layout, error expected", value: []interface{}{"2014-11-12T11:45:26.371Z", "2019-11-12T11:45:26.371Z"}, timeLayout: "invalid layout", valExpect: []time.Time{}, errExpect: true},
{tag: "invalid timeSlice, error expected", value: []int{1, 3}, timeLayout: layout, valExpect: []time.Time{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).TimeSlice(tc.timeLayout)
if err != nil && !tc.errExpect {
t.Error("timeSlice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestDurationlice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []time.Duration
errExpect bool
}{
{tag: "durationSlice value as expected", value: []interface{}{"1s", "1m"}, valExpect: []time.Duration{1 * time.Second, 1 * time.Minute}, errExpect: false},
{tag: "durationSlice value as expected", value: []interface{}{"1", "2"}, valExpect: []time.Duration{1 * time.Nanosecond, 2 * time.Nanosecond}, errExpect: false},
{tag: "durationSlice value as expected", value: []interface{}{float64(2), float64(3)}, valExpect: []time.Duration{2 * time.Nanosecond, 3 * time.Nanosecond}, errExpect: false},
{tag: "invalid durationSlice, error expected", value: []interface{}{"invalid duration 1", "invalid duration 2"}, valExpect: []time.Duration{}, errExpect: true},
{tag: "invalid durationSlice, error expected", value: []float64{3, 5}, valExpect: []time.Duration{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).DurationSlice()
if err != nil && !tc.errExpect {
t.Error("durationSlice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestStringSlice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []string
errExpect bool
}{
{tag: "stringSlice value as expected", value: []interface{}{"hello", "world"}, valExpect: []string{"hello", "world"}, errExpect: false},
{tag: "stringSlice value as expected", value: []interface{}{"tom", "jerry"}, valExpect: []string{"tom", "jerry"}, errExpect: false},
{tag: "invalid stringSlice, error expected", value: []interface{}{1, 3}, valExpect: []string{}, errExpect: false},
{tag: "invalid stringSlice, error expected", value: []int{1, 3}, valExpect: []string{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).StringSlice()
if err != nil && !tc.errExpect {
t.Error("stringSlice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestIntSlice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []int
errExpect bool
}{
{tag: "intSlice value as expected", value: []interface{}{132.1, 12.99}, valExpect: []int{132, 12}, errExpect: false},
{tag: "intSlice value as expected", value: []interface{}{float64(131), float64(12)}, valExpect: []int{131, 12}, errExpect: false}, // as golang decode number to float64
{tag: "invalid intSlice, error expected", value: []interface{}{1, 3}, valExpect: []int{}, errExpect: false},
{tag: "invalid intSlice, error expected", value: []int{1, 3}, valExpect: []int{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).IntSlice()
if err != nil && !tc.errExpect {
t.Error("intSlice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestInt8Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []int8
errExpect bool
}{
{tag: "int8Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int8{3, 12}, errExpect: false},
{tag: "int8Slice value as expected", value: []interface{}{float64(11), float64(12)}, valExpect: []int8{11, 12}, errExpect: false}, // as golang decode number to float64
{tag: "invalid int8Slice, error expected", value: []interface{}{1, 3}, valExpect: []int8{}, errExpect: false},
{tag: "invalid int8Slice, error expected", value: []int{1, 3}, valExpect: []int8{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Int8Slice()
if err != nil && !tc.errExpect {
t.Error("int8Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestInt16Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []int16
errExpect bool
}{
{tag: "int16Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int16{3, 12}, errExpect: false},
{tag: "int16Slice value as expected", value: []interface{}{float64(11), float64(12)}, valExpect: []int16{11, 12}, errExpect: false}, // as golang decode number to float64
{tag: "invalid int16Slice, error expected", value: []interface{}{1, 3}, valExpect: []int16{}, errExpect: false},
{tag: "invalid int16Slice, error expected", value: []int{1, 3}, valExpect: []int16{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Int16Slice()
if err != nil && !tc.errExpect {
t.Error("int16Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestInt32Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []int32
errExpect bool
}{
{tag: "int32Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int32{3, 12}, errExpect: false},
{tag: "int32Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []int32{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid int32Slice, error expected", value: []interface{}{1, 3}, valExpect: []int32{}, errExpect: false},
{tag: "invalid int32Slice, error expected", value: []int{1, 3}, valExpect: []int32{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Int32Slice()
if err != nil && !tc.errExpect {
t.Error("int32Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestInt64Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []int64
errExpect bool
}{
{tag: "int64Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []int64{3, 12}, errExpect: false},
{tag: "int64Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []int64{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid int64Slice, error expected", value: []interface{}{1, 3}, valExpect: []int64{}, errExpect: false},
{tag: "invalid int64Slice, error expected", value: []int{1, 3}, valExpect: []int64{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Int64Slice()
if err != nil && !tc.errExpect {
t.Error("int64Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestUintSlice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []uint
errExpect bool
}{
{tag: "uintSlice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint{3, 12}, errExpect: false},
{tag: "uintSlice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid uintSlice, error expected", value: []interface{}{1, 3}, valExpect: []uint{}, errExpect: false},
{tag: "invalid uintSlice, error expected", value: []int{1, 3}, valExpect: []uint{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).UintSlice()
if err != nil && !tc.errExpect {
t.Error("uintSlice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestUint8Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []uint8
errExpect bool
}{
{tag: "uint8Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint8{3, 12}, errExpect: false},
{tag: "uint8Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint8{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid uint8Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint8{}, errExpect: false},
{tag: "invalid uint8Slice, error expected", value: []int{1, 3}, valExpect: []uint8{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Uint8Slice()
if err != nil && !tc.errExpect {
t.Error("uint8Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestUint16Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []uint16
errExpect bool
}{
{tag: "uint16Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint16{3, 12}, errExpect: false},
{tag: "uint16Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint16{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid uint16Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint16{}, errExpect: false},
{tag: "invalid uint16Slice, error expected", value: []int{1, 3}, valExpect: []uint16{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Uint16Slice()
if err != nil && !tc.errExpect {
t.Error("uint16Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestUint32Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []uint32
errExpect bool
}{
{tag: "uint32Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint32{3, 12}, errExpect: false},
{tag: "uint32Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint32{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid uint32Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint32{}, errExpect: false},
{tag: "invalid uint32Slice, error expected", value: []int{1, 3}, valExpect: []uint32{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Uint32Slice()
if err != nil && !tc.errExpect {
t.Error("uint32Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestUint64Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []uint64
errExpect bool
}{
{tag: "uint64Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []uint64{3, 12}, errExpect: false},
{tag: "uint64Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []uint64{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid uint64Slice, error expected", value: []interface{}{1, 3}, valExpect: []uint64{}, errExpect: false},
{tag: "invalid uint64Slice, error expected", value: []int{1, 3}, valExpect: []uint64{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Uint64Slice()
if err != nil && !tc.errExpect {
t.Error("uint64Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestFloat32Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []float32
errExpect bool
}{
{tag: "float32Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []float32{3.1, 12.99}, errExpect: false},
{tag: "float32Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []float32{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid float32Slice, error expected", value: []interface{}{1, 3}, valExpect: []float32{}, errExpect: false},
{tag: "invalid float32Slice, error expected", value: []int{1, 3}, valExpect: []float32{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Float32Slice()
if err != nil && !tc.errExpect {
t.Error("float32Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}
func TestFloat64Slice(t *testing.T) {
testCases := []struct {
tag string
value interface{}
valExpect []float64
errExpect bool
}{
{tag: "float64Slice value as expected", value: []interface{}{3.1, 12.99}, valExpect: []float64{3.1, 12.99}, errExpect: false},
{tag: "float64Slice value as expected", value: []interface{}{float64(131), float64(132)}, valExpect: []float64{131, 132}, errExpect: false}, // as golang decode number to float64
{tag: "invalid float64Slice, error expected", value: []interface{}{1, 3}, valExpect: []float64{}, errExpect: false},
{tag: "invalid float64Slice, error expected", value: []int{1, 3}, valExpect: []float64{}, errExpect: true},
}
for _, tc := range testCases {
vv, err := NewResult(tc.value).Float64Slice()
if err != nil && !tc.errExpect {
t.Error("float64Slice:", err)
}
if !reflect.DeepEqual(vv, tc.valExpect) && !tc.errExpect {
t.Errorf("tag: %s\nexpected: %v got %v", tc.tag, tc.valExpect, vv)
}
}
}