-
Notifications
You must be signed in to change notification settings - Fork 306
/
cast_test.go
1229 lines (1038 loc) · 36.4 KB
/
cast_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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2014 Steve Francia <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package cast
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"path"
"reflect"
"testing"
"time"
qt "github.com/frankban/quicktest"
)
type testStep struct {
input interface{}
expect interface{}
iserr bool
}
func createNumberTestSteps(zero, one, eight, eightnegative, eightpoint31, eightpoint31negative interface{}) []testStep {
var jeight, jminuseight, jfloateight json.Number
_ = json.Unmarshal([]byte("8"), &jeight)
_ = json.Unmarshal([]byte("-8"), &jminuseight)
_ = json.Unmarshal([]byte("8.0"), &jfloateight)
kind := reflect.TypeOf(zero).Kind()
isUint := kind == reflect.Uint || kind == reflect.Uint8 || kind == reflect.Uint16 || kind == reflect.Uint32 || kind == reflect.Uint64
// Some precision is lost when converting from float64 to float32.
eightpoint31_32 := eightpoint31
eightpoint31negative_32 := eightpoint31negative
if kind == reflect.Float64 {
eightpoint31_32 = float64(float32(eightpoint31.(float64)))
eightpoint31negative_32 = float64(float32(eightpoint31negative.(float64)))
}
return []testStep{
{int(8), eight, false},
{int8(8), eight, false},
{int16(8), eight, false},
{int32(8), eight, false},
{int64(8), eight, false},
{time.Weekday(8), eight, false},
{time.Month(8), eight, false},
{uint(8), eight, false},
{uint8(8), eight, false},
{uint16(8), eight, false},
{uint32(8), eight, false},
{uint64(8), eight, false},
{float32(8.31), eightpoint31_32, false},
{float64(8.31), eightpoint31, false},
{true, one, false},
{false, zero, false},
{"8", eight, false},
{nil, zero, false},
{int(-8), eightnegative, isUint},
{int8(-8), eightnegative, isUint},
{int16(-8), eightnegative, isUint},
{int32(-8), eightnegative, isUint},
{int64(-8), eightnegative, isUint},
{float32(-8.31), eightpoint31negative_32, isUint},
{float64(-8.31), eightpoint31negative, isUint},
{"-8", eightnegative, isUint},
{jeight, eight, false},
{jminuseight, eightnegative, isUint},
{jfloateight, eight, false},
{"test", zero, true},
{testing.T{}, zero, true},
}
}
// Maybe Go 1.18 generics will make this less ugly?
func runNumberTest(c *qt.C, tests []testStep, tove func(interface{}) (interface{}, error), tov func(interface{}) interface{}) {
c.Helper()
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i)
v, err := tove(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil, errmsg)
continue
}
c.Assert(err, qt.IsNil, errmsg)
c.Assert(v, qt.Equals, test.expect, errmsg)
// Non-E test:
v = tov(test.input)
c.Assert(v, qt.Equals, test.expect, errmsg)
}
}
func TestToUintE(t *testing.T) {
tests := createNumberTestSteps(uint(0), uint(1), uint(8), uint(0), uint(8), uint(8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToUintE(v) },
func(v interface{}) interface{} { return ToUint(v) },
)
}
func TestToUint64E(t *testing.T) {
tests := createNumberTestSteps(uint64(0), uint64(1), uint64(8), uint64(0), uint64(8), uint64(8))
// Maximum value of uint64
tests = append(tests,
testStep{"18446744073709551615", uint64(18446744073709551615), false},
testStep{"18446744073709551616", uint64(0), true},
)
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToUint64E(v) },
func(v interface{}) interface{} { return ToUint64(v) },
)
}
func TestToUint32E(t *testing.T) {
tests := createNumberTestSteps(uint32(0), uint32(1), uint32(8), uint32(0), uint32(8), uint32(8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToUint32E(v) },
func(v interface{}) interface{} { return ToUint32(v) },
)
}
func TestToUint16E(t *testing.T) {
tests := createNumberTestSteps(uint16(0), uint16(1), uint16(8), uint16(0), uint16(8), uint16(8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToUint16E(v) },
func(v interface{}) interface{} { return ToUint16(v) },
)
}
func TestToUint8E(t *testing.T) {
tests := createNumberTestSteps(uint8(0), uint8(1), uint8(8), uint8(0), uint8(8), uint8(8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToUint8E(v) },
func(v interface{}) interface{} { return ToUint8(v) },
)
}
func TestToIntE(t *testing.T) {
tests := createNumberTestSteps(int(0), int(1), int(8), int(-8), int(8), int(-8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToIntE(v) },
func(v interface{}) interface{} { return ToInt(v) },
)
}
func TestToInt64E(t *testing.T) {
tests := createNumberTestSteps(int64(0), int64(1), int64(8), int64(-8), int64(8), int64(-8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToInt64E(v) },
func(v interface{}) interface{} { return ToInt64(v) },
)
}
func TestToInt32E(t *testing.T) {
tests := createNumberTestSteps(int32(0), int32(1), int32(8), int32(-8), int32(8), int32(-8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToInt32E(v) },
func(v interface{}) interface{} { return ToInt32(v) },
)
}
func TestToInt16E(t *testing.T) {
tests := createNumberTestSteps(int16(0), int16(1), int16(8), int16(-8), int16(8), int16(-8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToInt16E(v) },
func(v interface{}) interface{} { return ToInt16(v) },
)
}
func TestToInt8E(t *testing.T) {
tests := createNumberTestSteps(int8(0), int8(1), int8(8), int8(-8), int8(8), int8(-8))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToInt8E(v) },
func(v interface{}) interface{} { return ToInt8(v) },
)
}
func TestToFloat64E(t *testing.T) {
tests := createNumberTestSteps(float64(0), float64(1), float64(8), float64(-8), float64(8.31), float64(-8.31))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToFloat64E(v) },
func(v interface{}) interface{} { return ToFloat64(v) },
)
}
func TestToFloat32E(t *testing.T) {
tests := createNumberTestSteps(float32(0), float32(1), float32(8), float32(-8), float32(8.31), float32(-8.31))
runNumberTest(
qt.New(t),
tests,
func(v interface{}) (interface{}, error) { return ToFloat32E(v) },
func(v interface{}) interface{} { return ToFloat32(v) },
)
}
func TestToStringE(t *testing.T) {
c := qt.New(t)
var jn json.Number
_ = json.Unmarshal([]byte("8"), &jn)
type Key struct {
k string
}
key := &Key{"foo"}
tests := []struct {
input interface{}
expect string
iserr bool
}{
{int(8), "8", false},
{int8(8), "8", false},
{int16(8), "8", false},
{int32(8), "8", false},
{int64(8), "8", false},
{uint(8), "8", false},
{uint8(8), "8", false},
{uint16(8), "8", false},
{uint32(8), "8", false},
{uint64(8), "8", false},
{float32(8.31), "8.31", false},
{float64(8.31), "8.31", false},
{jn, "8", false},
{true, "true", false},
{false, "false", false},
{nil, "", false},
{[]byte("one time"), "one time", false},
{"one more time", "one more time", false},
{template.HTML("one time"), "one time", false},
{template.URL("http://somehost.foo"), "http://somehost.foo", false},
{template.JS("(1+2)"), "(1+2)", false},
{template.CSS("a"), "a", false},
{template.HTMLAttr("a"), "a", false},
// errors
{testing.T{}, "", true},
{key, "", true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil, errmsg)
continue
}
c.Assert(err, qt.IsNil, errmsg)
c.Assert(v, qt.Equals, test.expect, errmsg)
// Non-E test
v = ToString(test.input)
c.Assert(v, qt.Equals, test.expect, errmsg)
}
}
type foo struct {
val string
}
func (x foo) String() string {
return x.val
}
func TestStringerToString(t *testing.T) {
c := qt.New(t)
var x foo
x.val = "bar"
c.Assert(ToString(x), qt.Equals, "bar")
}
type fu struct {
val string
}
func (x fu) Error() string {
return x.val
}
func TestErrorToString(t *testing.T) {
c := qt.New(t)
var x fu
x.val = "bar"
c.Assert(ToString(x), qt.Equals, "bar")
}
func TestStringMapStringSliceE(t *testing.T) {
c := qt.New(t)
// ToStringMapString inputs/outputs
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var interfaceMapString = map[interface{}]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
// ToStringMapStringSlice inputs/outputs
var stringMapStringSlice = map[string][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
var stringMapInterfaceSlice = map[string][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
var stringMapInterfaceInterfaceSlice = map[string]interface{}{"key 1": []interface{}{"value 1", "value 2", "value 3"}, "key 2": []interface{}{"value 1", "value 2", "value 3"}, "key 3": []interface{}{"value 1", "value 2", "value 3"}}
var stringMapStringSingleSliceFieldsResult = map[string][]string{"key 1": {"value", "1"}, "key 2": {"value", "2"}, "key 3": {"value", "3"}}
var interfaceMapStringSlice = map[interface{}][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
var interfaceMapInterfaceSlice = map[interface{}][]interface{}{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
var stringMapStringSliceMultiple = map[string][]string{"key 1": {"value 1", "value 2", "value 3"}, "key 2": {"value 1", "value 2", "value 3"}, "key 3": {"value 1", "value 2", "value 3"}}
var stringMapStringSliceSingle = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}, "key 3": {"value 3"}}
var stringMapInterface1 = map[string]interface{}{"key 1": []string{"value 1"}, "key 2": []string{"value 2"}}
var stringMapInterfaceResult1 = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2"}}
var jsonStringMapString = `{"key 1": "value 1", "key 2": "value 2"}`
var jsonStringMapStringArray = `{"key 1": ["value 1"], "key 2": ["value 2", "value 3"]}`
var jsonStringMapStringArrayResult = map[string][]string{"key 1": {"value 1"}, "key 2": {"value 2", "value 3"}}
type Key struct {
k string
}
tests := []struct {
input interface{}
expect map[string][]string
iserr bool
}{
{stringMapStringSlice, stringMapStringSlice, false},
{stringMapInterfaceSlice, stringMapStringSlice, false},
{stringMapInterfaceInterfaceSlice, stringMapStringSlice, false},
{stringMapStringSliceMultiple, stringMapStringSlice, false},
{stringMapStringSliceMultiple, stringMapStringSlice, false},
{stringMapString, stringMapStringSliceSingle, false},
{stringMapInterface, stringMapStringSliceSingle, false},
{stringMapInterface1, stringMapInterfaceResult1, false},
{interfaceMapStringSlice, stringMapStringSlice, false},
{interfaceMapInterfaceSlice, stringMapStringSlice, false},
{interfaceMapString, stringMapStringSingleSliceFieldsResult, false},
{interfaceMapInterface, stringMapStringSingleSliceFieldsResult, false},
{jsonStringMapStringArray, jsonStringMapStringArrayResult, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{map[interface{}]interface{}{"foo": testing.T{}}, nil, true},
{map[interface{}]interface{}{Key{"foo"}: "bar"}, nil, true}, // ToStringE(Key{"foo"}) should fail
{jsonStringMapString, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringMapStringSliceE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil, errmsg)
continue
}
c.Assert(err, qt.IsNil, errmsg)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToStringMapStringSlice(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToStringMapE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect map[string]interface{}
iserr bool
}{
{map[interface{}]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
{map[string]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
{`{"tag": "tags", "group": "groups"}`, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
{`{"tag": "tags", "group": true}`, map[string]interface{}{"tag": "tags", "group": true}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringMapE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil, errmsg)
continue
}
c.Assert(err, qt.IsNil, errmsg)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToStringMap(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToStringMapBoolE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect map[string]bool
iserr bool
}{
{map[interface{}]interface{}{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
{map[string]interface{}{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
{map[string]bool{"v1": true, "v2": false}, map[string]bool{"v1": true, "v2": false}, false},
{`{"v1": true, "v2": false}`, map[string]bool{"v1": true, "v2": false}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringMapBoolE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil, errmsg)
continue
}
c.Assert(err, qt.IsNil, errmsg)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToStringMapBool(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToStringMapIntE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect map[string]int
iserr bool
}{
{map[interface{}]interface{}{"v1": 1, "v2": 222}, map[string]int{"v1": 1, "v2": 222}, false},
{map[string]interface{}{"v1": 342, "v2": 5141}, map[string]int{"v1": 342, "v2": 5141}, false},
{map[string]int{"v1": 33, "v2": 88}, map[string]int{"v1": 33, "v2": 88}, false},
{map[string]int32{"v1": int32(33), "v2": int32(88)}, map[string]int{"v1": 33, "v2": 88}, false},
{map[string]uint16{"v1": uint16(33), "v2": uint16(88)}, map[string]int{"v1": 33, "v2": 88}, false},
{map[string]float64{"v1": float64(8.22), "v2": float64(43.32)}, map[string]int{"v1": 8, "v2": 43}, false},
{`{"v1": 67, "v2": 56}`, map[string]int{"v1": 67, "v2": 56}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringMapIntE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil, errmsg)
continue
}
c.Assert(err, qt.IsNil, errmsg)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToStringMapInt(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToStringMapInt64E(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect map[string]int64
iserr bool
}{
{map[interface{}]interface{}{"v1": int32(8), "v2": int32(888)}, map[string]int64{"v1": int64(8), "v2": int64(888)}, false},
{map[string]interface{}{"v1": int64(45), "v2": int64(67)}, map[string]int64{"v1": 45, "v2": 67}, false},
{map[string]int64{"v1": 33, "v2": 88}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]int{"v1": 33, "v2": 88}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]int32{"v1": int32(33), "v2": int32(88)}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]uint16{"v1": uint16(33), "v2": uint16(88)}, map[string]int64{"v1": 33, "v2": 88}, false},
{map[string]float64{"v1": float64(8.22), "v2": float64(43.32)}, map[string]int64{"v1": 8, "v2": 43}, false},
{`{"v1": 67, "v2": 56}`, map[string]int64{"v1": 67, "v2": 56}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringMapInt64E(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToStringMapInt64(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToStringMapStringE(t *testing.T) {
c := qt.New(t)
var stringMapString = map[string]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var stringMapInterface = map[string]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var interfaceMapString = map[interface{}]string{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var interfaceMapInterface = map[interface{}]interface{}{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}
var jsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"}`
var invalidJsonString = `{"key 1": "value 1", "key 2": "value 2", "key 3": "value 3"`
var emptyString = ""
tests := []struct {
input interface{}
expect map[string]string
iserr bool
}{
{stringMapString, stringMapString, false},
{stringMapInterface, stringMapString, false},
{interfaceMapString, stringMapString, false},
{interfaceMapInterface, stringMapString, false},
{jsonString, stringMapString, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{invalidJsonString, nil, true},
{emptyString, nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringMapStringE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToStringMapString(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToBoolSliceE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect []bool
iserr bool
}{
{[]bool{true, false, true}, []bool{true, false, true}, false},
{[]interface{}{true, false, true}, []bool{true, false, true}, false},
{[]int{1, 0, 1}, []bool{true, false, true}, false},
{[]string{"true", "false", "true"}, []bool{true, false, true}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{[]string{"foo", "bar"}, nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToBoolSliceE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToBoolSlice(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToIntSliceE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect []int
iserr bool
}{
{[]int{1, 3}, []int{1, 3}, false},
{[]interface{}{1.2, 3.2}, []int{1, 3}, false},
{[]string{"2", "3"}, []int{2, 3}, false},
{[2]string{"2", "3"}, []int{2, 3}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{[]string{"foo", "bar"}, nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToIntSliceE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToIntSlice(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToSliceE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect []interface{}
iserr bool
}{
{[]interface{}{1, 3}, []interface{}{1, 3}, false},
{[]map[string]interface{}{{"k1": 1}, {"k2": 2}}, []interface{}{map[string]interface{}{"k1": 1}, map[string]interface{}{"k2": 2}}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToSliceE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToSlice(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToStringSliceE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect []string
iserr bool
}{
{[]int{1, 2}, []string{"1", "2"}, false},
{[]int8{int8(1), int8(2)}, []string{"1", "2"}, false},
{[]int32{int32(1), int32(2)}, []string{"1", "2"}, false},
{[]int64{int64(1), int64(2)}, []string{"1", "2"}, false},
{[]float32{float32(1.01), float32(2.01)}, []string{"1.01", "2.01"}, false},
{[]float64{float64(1.01), float64(2.01)}, []string{"1.01", "2.01"}, false},
{[]string{"a", "b"}, []string{"a", "b"}, false},
{[]interface{}{1, 3}, []string{"1", "3"}, false},
{interface{}(1), []string{"1"}, false},
{[]error{errors.New("a"), errors.New("b")}, []string{"a", "b"}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToStringSliceE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToStringSlice(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToDurationSliceE(t *testing.T) {
c := qt.New(t)
tests := []struct {
input interface{}
expect []time.Duration
iserr bool
}{
{[]string{"1s", "1m"}, []time.Duration{time.Second, time.Minute}, false},
{[]int{1, 2}, []time.Duration{1, 2}, false},
{[]interface{}{1, 3}, []time.Duration{1, 3}, false},
{[]time.Duration{1, 3}, []time.Duration{1, 3}, false},
// errors
{nil, nil, true},
{testing.T{}, nil, true},
{[]string{"invalid"}, nil, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToDurationSliceE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
// Non-E test
v = ToDurationSlice(test.input)
c.Assert(v, qt.DeepEquals, test.expect, errmsg)
}
}
func TestToBoolE(t *testing.T) {
c := qt.New(t)
var jf, jt, je json.Number
_ = json.Unmarshal([]byte("0"), &jf)
_ = json.Unmarshal([]byte("1"), &jt)
_ = json.Unmarshal([]byte("1.0"), &je)
tests := []struct {
input interface{}
expect bool
iserr bool
}{
{0, false, false},
{int64(0), false, false},
{int32(0), false, false},
{int16(0), false, false},
{int8(0), false, false},
{uint(0), false, false},
{uint64(0), false, false},
{uint32(0), false, false},
{uint16(0), false, false},
{uint8(0), false, false},
{float64(0), false, false},
{float32(0), false, false},
{time.Duration(0), false, false},
{jf, false, false},
{nil, false, false},
{"false", false, false},
{"FALSE", false, false},
{"False", false, false},
{"f", false, false},
{"F", false, false},
{false, false, false},
{"true", true, false},
{"TRUE", true, false},
{"True", true, false},
{"t", true, false},
{"T", true, false},
{1, true, false},
{int64(1), true, false},
{int32(1), true, false},
{int16(1), true, false},
{int8(1), true, false},
{uint(1), true, false},
{uint64(1), true, false},
{uint32(1), true, false},
{uint16(1), true, false},
{uint8(1), true, false},
{float64(1), true, false},
{float32(1), true, false},
{time.Duration(1), true, false},
{jt, true, false},
{je, true, false},
{true, true, false},
{-1, true, false},
{int64(-1), true, false},
{int32(-1), true, false},
{int16(-1), true, false},
{int8(-1), true, false},
// errors
{"test", false, true},
{testing.T{}, false, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToBoolE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v, qt.Equals, test.expect, errmsg)
// Non-E test
v = ToBool(test.input)
c.Assert(v, qt.Equals, test.expect, errmsg)
}
}
func BenchmarkTooBool(b *testing.B) {
for i := 0; i < b.N; i++ {
if !ToBool(true) {
b.Fatal("ToBool returned false")
}
}
}
func BenchmarkTooInt(b *testing.B) {
convert := func(num52 interface{}) {
if v := ToInt(num52); v != 52 {
b.Fatalf("ToInt returned wrong value, got %d, want %d", v, 32)
}
}
for i := 0; i < b.N; i++ {
convert("52")
convert(52.0)
convert(uint64(52))
}
}
func BenchmarkTrimZeroDecimal(b *testing.B) {
for i := 0; i < b.N; i++ {
trimZeroDecimal("")
trimZeroDecimal("123")
trimZeroDecimal("120")
trimZeroDecimal("120.00")
}
}
func BenchmarkCommonTimeLayouts(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, commonLayout := range []string{"2019-04-29", "2017-05-30T00:00:00Z"} {
_, err := StringToDateInDefaultLocation(commonLayout, time.UTC)
if err != nil {
b.Fatal(err)
}
}
}
}
func TestIndirectPointers(t *testing.T) {
c := qt.New(t)
x := 13
y := &x
z := &y
c.Assert(ToInt(y), qt.Equals, 13)
c.Assert(ToInt(z), qt.Equals, 13)
}
func TestToTime(t *testing.T) {
c := qt.New(t)
var jntime, jnetime json.Number
_ = json.Unmarshal([]byte("1234567890"), &jntime)
_ = json.Unmarshal([]byte("123.4567890"), &jnetime)
tests := []struct {
input interface{}
expect time.Time
iserr bool
}{
{"2009-11-10 23:00:00 +0000 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // Time.String()
{"Tue Nov 10 23:00:00 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // ANSIC
{"Tue Nov 10 23:00:00 UTC 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // UnixDate
{"Tue Nov 10 23:00:00 +0000 2009", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RubyDate
{"10 Nov 09 23:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC822
{"10 Nov 09 23:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC822Z
{"Tuesday, 10-Nov-09 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC850
{"Tue, 10 Nov 2009 23:00:00 UTC", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC1123
{"Tue, 10 Nov 2009 23:00:00 +0000", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC1123Z
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC3339
{"2018-10-21T23:21:29+0200", time.Date(2018, 10, 21, 21, 21, 29, 0, time.UTC), false}, // RFC3339 without timezone hh:mm colon
{"2009-11-10T23:00:00Z", time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), false}, // RFC3339Nano
{"11:00PM", time.Date(0, 1, 1, 23, 0, 0, 0, time.UTC), false}, // Kitchen
{"Nov 10 23:00:00", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // Stamp
{"Nov 10 23:00:00.000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampMilli
{"Nov 10 23:00:00.000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampMicro
{"Nov 10 23:00:00.000000000", time.Date(0, 11, 10, 23, 0, 0, 0, time.UTC), false}, // StampNano
{"2016-03-06 15:28:01-00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false}, // RFC3339 without T
{"2016-03-06 15:28:01-0000", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false}, // RFC3339 without T or timezone hh:mm colon
{"2016-03-06 15:28:01", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
{"2016-03-06 15:28:01 -0000", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
{"2016-03-06 15:28:01 -00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, time.UTC), false},
{"2016-03-06 15:28:01 +0900", time.Date(2016, 3, 6, 6, 28, 1, 0, time.UTC), false},
{"2016-03-06 15:28:01 +09:00", time.Date(2016, 3, 6, 6, 28, 1, 0, time.UTC), false},
{"2006-01-02", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
{"02 Jan 2006", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
{1472574600, time.Date(2016, 8, 30, 16, 30, 0, 0, time.UTC), false},
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
{int64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{int32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{uint(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
{uint64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{uint32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{jntime, time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
{time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
// errors
{"2006", time.Time{}, true},
{jnetime, time.Time{}, true},
{testing.T{}, time.Time{}, true},
}
for i, test := range tests {
errmsg := qt.Commentf("i = %d", i) // assert helper message
v, err := ToTimeE(test.input)
if test.iserr {
c.Assert(err, qt.IsNotNil)
continue
}
c.Assert(err, qt.IsNil)
c.Assert(v.UTC(), qt.Equals, test.expect, errmsg)
// Non-E test
v = ToTime(test.input)
c.Assert(v.UTC(), qt.Equals, test.expect, errmsg)
}
}
func TestToDurationE(t *testing.T) {
c := qt.New(t)