6
6
package cast
7
7
8
8
import (
9
+ "encoding/json"
9
10
"errors"
10
11
"fmt"
11
12
"html/template"
@@ -18,6 +19,10 @@ import (
18
19
)
19
20
20
21
func TestToUintE (t * testing.T ) {
22
+ var jn , nj , jne json.Number
23
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
24
+ _ = json .Unmarshal ([]byte ("-8" ), & nj )
25
+ _ = json .Unmarshal ([]byte ("8.0" ), & jne )
21
26
tests := []struct {
22
27
input interface {}
23
28
expect uint
@@ -38,6 +43,7 @@ func TestToUintE(t *testing.T) {
38
43
{true , 1 , false },
39
44
{false , 0 , false },
40
45
{"8" , 8 , false },
46
+ {jn , 8 , false },
41
47
{nil , 0 , false },
42
48
// errors
43
49
{int (- 8 ), 0 , true },
@@ -48,6 +54,8 @@ func TestToUintE(t *testing.T) {
48
54
{float32 (- 8.31 ), 0 , true },
49
55
{float64 (- 8.31 ), 0 , true },
50
56
{"-8" , 0 , true },
57
+ {nj , 0 , true },
58
+ {jne , 0 , true },
51
59
{"test" , 0 , true },
52
60
{testing.T {}, 0 , true },
53
61
}
@@ -71,6 +79,10 @@ func TestToUintE(t *testing.T) {
71
79
}
72
80
73
81
func TestToUint64E (t * testing.T ) {
82
+ var jn , nj , jne json.Number
83
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
84
+ _ = json .Unmarshal ([]byte ("-8" ), & nj )
85
+ _ = json .Unmarshal ([]byte ("8.0" ), & jne )
74
86
tests := []struct {
75
87
input interface {}
76
88
expect uint64
@@ -91,6 +103,7 @@ func TestToUint64E(t *testing.T) {
91
103
{true , 1 , false },
92
104
{false , 0 , false },
93
105
{"8" , 8 , false },
106
+ {jn , 8 , false },
94
107
{nil , 0 , false },
95
108
// errors
96
109
{int (- 8 ), 0 , true },
@@ -101,6 +114,8 @@ func TestToUint64E(t *testing.T) {
101
114
{float32 (- 8.31 ), 0 , true },
102
115
{float64 (- 8.31 ), 0 , true },
103
116
{"-8" , 0 , true },
117
+ {nj , 0 , true },
118
+ {jne , 0 , true },
104
119
{"test" , 0 , true },
105
120
{testing.T {}, 0 , true },
106
121
}
@@ -124,6 +139,10 @@ func TestToUint64E(t *testing.T) {
124
139
}
125
140
126
141
func TestToUint32E (t * testing.T ) {
142
+ var jn , nj , jne json.Number
143
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
144
+ _ = json .Unmarshal ([]byte ("-8" ), & nj )
145
+ _ = json .Unmarshal ([]byte ("8.0" ), & jne )
127
146
tests := []struct {
128
147
input interface {}
129
148
expect uint32
@@ -144,6 +163,7 @@ func TestToUint32E(t *testing.T) {
144
163
{true , 1 , false },
145
164
{false , 0 , false },
146
165
{"8" , 8 , false },
166
+ {jn , 8 , false },
147
167
{nil , 0 , false },
148
168
{int (- 8 ), 0 , true },
149
169
{int8 (- 8 ), 0 , true },
@@ -153,7 +173,9 @@ func TestToUint32E(t *testing.T) {
153
173
{float32 (- 8.31 ), 0 , true },
154
174
{float64 (- 8.31 ), 0 , true },
155
175
{"-8" , 0 , true },
176
+ {nj , 0 , true },
156
177
// errors
178
+ {jne , 0 , true },
157
179
{"test" , 0 , true },
158
180
{testing.T {}, 0 , true },
159
181
}
@@ -177,6 +199,10 @@ func TestToUint32E(t *testing.T) {
177
199
}
178
200
179
201
func TestToUint16E (t * testing.T ) {
202
+ var jn , nj , jne json.Number
203
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
204
+ _ = json .Unmarshal ([]byte ("-8" ), & nj )
205
+ _ = json .Unmarshal ([]byte ("8.0" ), & jne )
180
206
tests := []struct {
181
207
input interface {}
182
208
expect uint16
@@ -197,6 +223,7 @@ func TestToUint16E(t *testing.T) {
197
223
{true , 1 , false },
198
224
{false , 0 , false },
199
225
{"8" , 8 , false },
226
+ {jn , 8 , false },
200
227
{nil , 0 , false },
201
228
// errors
202
229
{int (- 8 ), 0 , true },
@@ -207,6 +234,8 @@ func TestToUint16E(t *testing.T) {
207
234
{float32 (- 8.31 ), 0 , true },
208
235
{float64 (- 8.31 ), 0 , true },
209
236
{"-8" , 0 , true },
237
+ {nj , 0 , true },
238
+ {jne , 0 , true },
210
239
{"test" , 0 , true },
211
240
{testing.T {}, 0 , true },
212
241
}
@@ -230,6 +259,10 @@ func TestToUint16E(t *testing.T) {
230
259
}
231
260
232
261
func TestToUint8E (t * testing.T ) {
262
+ var jn , nj , jne json.Number
263
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
264
+ _ = json .Unmarshal ([]byte ("-8" ), & nj )
265
+ _ = json .Unmarshal ([]byte ("8.0" ), & jne )
233
266
tests := []struct {
234
267
input interface {}
235
268
expect uint8
@@ -250,6 +283,7 @@ func TestToUint8E(t *testing.T) {
250
283
{true , 1 , false },
251
284
{false , 0 , false },
252
285
{"8" , 8 , false },
286
+ {jn , 8 , false },
253
287
{nil , 0 , false },
254
288
// errors
255
289
{int (- 8 ), 0 , true },
@@ -260,6 +294,8 @@ func TestToUint8E(t *testing.T) {
260
294
{float32 (- 8.31 ), 0 , true },
261
295
{float64 (- 8.31 ), 0 , true },
262
296
{"-8" , 0 , true },
297
+ {nj , 0 , true },
298
+ {jne , 0 , true },
263
299
{"test" , 0 , true },
264
300
{testing.T {}, 0 , true },
265
301
}
@@ -283,6 +319,9 @@ func TestToUint8E(t *testing.T) {
283
319
}
284
320
285
321
func TestToIntE (t * testing.T ) {
322
+ var jn , nj json.Number
323
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
324
+ _ = json .Unmarshal ([]byte ("8.0" ), & nj )
286
325
tests := []struct {
287
326
input interface {}
288
327
expect int
@@ -303,9 +342,11 @@ func TestToIntE(t *testing.T) {
303
342
{true , 1 , false },
304
343
{false , 0 , false },
305
344
{"8" , 8 , false },
345
+ {jn , 8 , false },
306
346
{nil , 0 , false },
307
347
// errors
308
348
{"test" , 0 , true },
349
+ {nj , 0 , true },
309
350
{testing.T {}, 0 , true },
310
351
}
311
352
@@ -328,6 +369,9 @@ func TestToIntE(t *testing.T) {
328
369
}
329
370
330
371
func TestToInt64E (t * testing.T ) {
372
+ var jn , nj json.Number
373
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
374
+ _ = json .Unmarshal ([]byte (".8" ), & nj )
331
375
tests := []struct {
332
376
input interface {}
333
377
expect int64
@@ -348,9 +392,11 @@ func TestToInt64E(t *testing.T) {
348
392
{true , 1 , false },
349
393
{false , 0 , false },
350
394
{"8" , 8 , false },
395
+ {jn , 8 , false },
351
396
{nil , 0 , false },
352
397
// errors
353
398
{"test" , 0 , true },
399
+ {nj , 0 , true },
354
400
{testing.T {}, 0 , true },
355
401
}
356
402
@@ -373,6 +419,9 @@ func TestToInt64E(t *testing.T) {
373
419
}
374
420
375
421
func TestToInt32E (t * testing.T ) {
422
+ var jn , nj json.Number
423
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
424
+ _ = json .Unmarshal ([]byte ("8.0" ), & nj )
376
425
tests := []struct {
377
426
input interface {}
378
427
expect int32
@@ -393,9 +442,11 @@ func TestToInt32E(t *testing.T) {
393
442
{true , 1 , false },
394
443
{false , 0 , false },
395
444
{"8" , 8 , false },
445
+ {jn , 8 , false },
396
446
{nil , 0 , false },
397
447
// errors
398
448
{"test" , 0 , true },
449
+ {nj , 0 , true },
399
450
{testing.T {}, 0 , true },
400
451
}
401
452
@@ -418,6 +469,9 @@ func TestToInt32E(t *testing.T) {
418
469
}
419
470
420
471
func TestToInt16E (t * testing.T ) {
472
+ var jn , nj json.Number
473
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
474
+ _ = json .Unmarshal ([]byte ("8.0" ), & nj )
421
475
tests := []struct {
422
476
input interface {}
423
477
expect int16
@@ -438,9 +492,11 @@ func TestToInt16E(t *testing.T) {
438
492
{true , 1 , false },
439
493
{false , 0 , false },
440
494
{"8" , 8 , false },
495
+ {jn , 8 , false },
441
496
{nil , 0 , false },
442
497
// errors
443
498
{"test" , 0 , true },
499
+ {nj , 0 , true },
444
500
{testing.T {}, 0 , true },
445
501
}
446
502
@@ -463,6 +519,9 @@ func TestToInt16E(t *testing.T) {
463
519
}
464
520
465
521
func TestToInt8E (t * testing.T ) {
522
+ var jn , nj json.Number
523
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
524
+ _ = json .Unmarshal ([]byte ("8.0" ), & nj )
466
525
tests := []struct {
467
526
input interface {}
468
527
expect int8
@@ -483,9 +542,11 @@ func TestToInt8E(t *testing.T) {
483
542
{true , 1 , false },
484
543
{false , 0 , false },
485
544
{"8" , 8 , false },
545
+ {jn , 8 , false },
486
546
{nil , 0 , false },
487
547
// errors
488
548
{"test" , 0 , true },
549
+ {nj , 0 , true },
489
550
{testing.T {}, 0 , true },
490
551
}
491
552
@@ -508,6 +569,9 @@ func TestToInt8E(t *testing.T) {
508
569
}
509
570
510
571
func TestToFloat64E (t * testing.T ) {
572
+ var jn , nj json.Number
573
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
574
+ _ = json .Unmarshal ([]byte (".8" ), & nj )
511
575
tests := []struct {
512
576
input interface {}
513
577
expect float64
@@ -526,10 +590,12 @@ func TestToFloat64E(t *testing.T) {
526
590
{float32 (8 ), 8 , false },
527
591
{float64 (8.31 ), 8.31 , false },
528
592
{"8" , 8 , false },
593
+ {jn , 8 , false },
529
594
{true , 1 , false },
530
595
{false , 0 , false },
531
596
// errors
532
597
{"test" , 0 , true },
598
+ {nj , 0 , true },
533
599
{testing.T {}, 0 , true },
534
600
}
535
601
@@ -552,6 +618,9 @@ func TestToFloat64E(t *testing.T) {
552
618
}
553
619
554
620
func TestToFloat32E (t * testing.T ) {
621
+ var jn , nj json.Number
622
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
623
+ _ = json .Unmarshal ([]byte (".8" ), & nj )
555
624
tests := []struct {
556
625
input interface {}
557
626
expect float32
@@ -570,10 +639,12 @@ func TestToFloat32E(t *testing.T) {
570
639
{float32 (8.31 ), 8.31 , false },
571
640
{float64 (8.31 ), 8.31 , false },
572
641
{"8" , 8 , false },
642
+ {jn , 8 , false },
573
643
{true , 1 , false },
574
644
{false , 0 , false },
575
645
// errors
576
646
{"test" , 0 , true },
647
+ {nj , 0 , true },
577
648
{testing.T {}, 0 , true },
578
649
}
579
650
@@ -596,6 +667,8 @@ func TestToFloat32E(t *testing.T) {
596
667
}
597
668
598
669
func TestToStringE (t * testing.T ) {
670
+ var jn json.Number
671
+ _ = json .Unmarshal ([]byte ("8" ), & jn )
599
672
type Key struct {
600
673
k string
601
674
}
@@ -618,6 +691,7 @@ func TestToStringE(t *testing.T) {
618
691
{uint64 (8 ), "8" , false },
619
692
{float32 (8.31 ), "8.31" , false },
620
693
{float64 (8.31 ), "8.31" , false },
694
+ {jn , "8" , false },
621
695
{true , "true" , false },
622
696
{false , "false" , false },
623
697
{nil , "" , false },
@@ -1120,12 +1194,17 @@ func TestToDurationSliceE(t *testing.T) {
1120
1194
}
1121
1195
1122
1196
func TestToBoolE (t * testing.T ) {
1197
+ var jf , jt , je json.Number
1198
+ _ = json .Unmarshal ([]byte ("0" ), & jf )
1199
+ _ = json .Unmarshal ([]byte ("1" ), & jt )
1200
+ _ = json .Unmarshal ([]byte ("1.0" ), & je )
1123
1201
tests := []struct {
1124
1202
input interface {}
1125
1203
expect bool
1126
1204
iserr bool
1127
1205
}{
1128
1206
{0 , false , false },
1207
+ {jf , false , false },
1129
1208
{nil , false , false },
1130
1209
{"false" , false , false },
1131
1210
{"FALSE" , false , false },
@@ -1140,11 +1219,13 @@ func TestToBoolE(t *testing.T) {
1140
1219
{"t" , true , false },
1141
1220
{"T" , true , false },
1142
1221
{1 , true , false },
1222
+ {jt , true , false },
1143
1223
{true , true , false },
1144
1224
{- 1 , true , false },
1145
1225
1146
1226
// errors
1147
1227
{"test" , false , true },
1228
+ {je , false , true },
1148
1229
{testing.T {}, false , true },
1149
1230
}
1150
1231
@@ -1197,6 +1278,9 @@ func TestIndirectPointers(t *testing.T) {
1197
1278
}
1198
1279
1199
1280
func TestToTime (t * testing.T ) {
1281
+ var jntime , jnetime json.Number
1282
+ _ = json .Unmarshal ([]byte ("1234567890" ), & jntime )
1283
+ _ = json .Unmarshal ([]byte ("123.4567890" ), & jnetime )
1200
1284
tests := []struct {
1201
1285
input interface {}
1202
1286
expect time.Time
@@ -1235,9 +1319,11 @@ func TestToTime(t *testing.T) {
1235
1319
{uint (1482597504 ), time .Date (2016 , 12 , 24 , 16 , 38 , 24 , 0 , time .UTC ), false },
1236
1320
{uint64 (1234567890 ), time .Date (2009 , 2 , 13 , 23 , 31 , 30 , 0 , time .UTC ), false },
1237
1321
{uint32 (1234567890 ), time .Date (2009 , 2 , 13 , 23 , 31 , 30 , 0 , time .UTC ), false },
1322
+ {jntime , time .Date (2009 , 2 , 13 , 23 , 31 , 30 , 0 , time .UTC ), false },
1238
1323
{time .Date (2009 , 2 , 13 , 23 , 31 , 30 , 0 , time .UTC ), time .Date (2009 , 2 , 13 , 23 , 31 , 30 , 0 , time .UTC ), false },
1239
1324
// errors
1240
1325
{"2006" , time.Time {}, true },
1326
+ {jnetime , time.Time {}, true },
1241
1327
{testing.T {}, time.Time {}, true },
1242
1328
}
1243
1329
@@ -1261,6 +1347,8 @@ func TestToTime(t *testing.T) {
1261
1347
1262
1348
func TestToDurationE (t * testing.T ) {
1263
1349
var td time.Duration = 5
1350
+ var jn json.Number
1351
+ _ = json .Unmarshal ([]byte ("5" ), & jn )
1264
1352
1265
1353
tests := []struct {
1266
1354
input interface {}
@@ -1280,6 +1368,7 @@ func TestToDurationE(t *testing.T) {
1280
1368
{uint8 (5 ), td , false },
1281
1369
{float64 (5 ), td , false },
1282
1370
{float32 (5 ), td , false },
1371
+ {jn , td , false },
1283
1372
{string ("5" ), td , false },
1284
1373
{string ("5ns" ), td , false },
1285
1374
{string ("5us" ), time .Microsecond * td , false },
0 commit comments