@@ -199,55 +199,48 @@ var getTests = []GetTest{
199
199
200
200
// Not found key tests
201
201
GetTest {
202
- desc : "non-existent key 1" ,
203
- json : `{"a":"b"}` ,
204
- path : []string {"c" },
205
- isFound : false ,
206
- isErr : true ,
202
+ desc : "non-existent key 1" ,
203
+ json : `{"a":"b"}` ,
204
+ path : []string {"c" },
205
+ isErr : true ,
207
206
},
208
207
GetTest {
209
- desc : "non-existent key 2" ,
210
- json : `{"a":"b"}` ,
211
- path : []string {"b" },
212
- isFound : false ,
213
- isErr : true ,
208
+ desc : "non-existent key 2" ,
209
+ json : `{"a":"b"}` ,
210
+ path : []string {"b" },
211
+ isErr : true ,
214
212
},
215
213
GetTest {
216
- desc : "non-existent key 3" ,
217
- json : `{"aa":"b"}` ,
218
- path : []string {"a" },
219
- isFound : false ,
220
- isErr : true ,
214
+ desc : "non-existent key 3" ,
215
+ json : `{"aa":"b"}` ,
216
+ path : []string {"a" },
217
+ isErr : true ,
221
218
},
222
219
GetTest {
223
- desc : "apply scope of parent when search for nested key" ,
224
- json : `{"a": { "b": 1}, "c": 2 }` ,
225
- path : []string {"a" , "b" , "c" },
226
- isFound : false ,
227
- isErr : true ,
220
+ desc : "apply scope of parent when search for nested key" ,
221
+ json : `{"a": { "b": 1}, "c": 2 }` ,
222
+ path : []string {"a" , "b" , "c" },
223
+ isErr : true ,
228
224
},
229
225
GetTest {
230
- desc : `apply scope to key level` ,
231
- json : `{"a": { "b": 1}, "c": 2 }` ,
232
- path : []string {"b" },
233
- isFound : false ,
234
- isErr : true ,
226
+ desc : `apply scope to key level` ,
227
+ json : `{"a": { "b": 1}, "c": 2 }` ,
228
+ path : []string {"b" },
229
+ isErr : true ,
235
230
},
236
231
GetTest {
237
- desc : `handle escaped quote in key name in JSON` ,
238
- json : `{"key\"key": 1}` ,
239
- path : []string {"key" },
240
- isFound : false ,
241
- isErr : true ,
232
+ desc : `handle escaped quote in key name in JSON` ,
233
+ json : `{"key\"key": 1}` ,
234
+ path : []string {"key" },
235
+ isErr : true ,
242
236
},
243
237
244
238
// Error/invalid tests
245
239
GetTest {
246
- desc : `handle escaped quote in key name in JSON` ,
247
- json : `{"key\"key": 1}` ,
248
- path : []string {"key" },
249
- isFound : false ,
250
- isErr : true ,
240
+ desc : `handle escaped quote in key name in JSON` ,
241
+ json : `{"key\"key": 1}` ,
242
+ path : []string {"key" },
243
+ isErr : true ,
251
244
},
252
245
GetTest {
253
246
desc : `missing closing brace, but can still find key` ,
@@ -463,8 +456,8 @@ var getArrayTests = []GetTest{
463
456
464
457
// checkFoundAndNoError checks the dataType and error return from Get*() against the test case expectations.
465
458
// Returns true the test should proceed to checking the actual data returned from Get*(), or false if the test is finished.
466
- func checkFoundAndNoError (t * testing.T , testKind string , test GetTest , jtype ValueType , value interface {}, err error ) bool {
467
- isFound := (jtype != NotExist )
459
+ func getTestCheckFoundAndNoError (t * testing.T , testKind string , test GetTest , jtype ValueType , value interface {}, err error ) bool {
460
+ isFound := (jtype != NotExist ) && ( err != KeyPathNotFoundError )
468
461
isErr := (err != nil )
469
462
470
463
if test .isErr != isErr {
@@ -478,10 +471,6 @@ func checkFoundAndNoError(t *testing.T, testKind string, test GetTest, jtype Val
478
471
// Else, if the call didn't match the is-found expectation, fail
479
472
t .Errorf ("%s test '%s' isFound mismatch: expected %t, obtained %t" , testKind , test .desc , test .isFound , isFound )
480
473
return false
481
- } else if ! isFound && err != KeyPathNotFoundError {
482
- // Else, if no value was found and the error is not correct, fail
483
- t .Errorf ("%s test '%s' error mismatch: expected %t, obtained %t" , testKind , test .desc , KeyPathNotFoundError , err )
484
- return false
485
474
} else if ! isFound {
486
475
// Else, if no value was found, don't fail and don't check the value
487
476
return false
@@ -491,7 +480,7 @@ func checkFoundAndNoError(t *testing.T, testKind string, test GetTest, jtype Val
491
480
}
492
481
}
493
482
494
- func runTests (t * testing.T , tests []GetTest , runner func (GetTest ) (interface {}, ValueType , error ), typeChecker func (GetTest , interface {}) (bool , interface {})) {
483
+ func runGetTests (t * testing.T , testKind string , tests []GetTest , runner func (GetTest ) (interface {}, ValueType , error ), resultChecker func (GetTest , interface {}) (bool , interface {})) {
495
484
for _ , test := range tests {
496
485
if activeTest != "" && test .desc != activeTest {
497
486
continue
@@ -501,27 +490,27 @@ func runTests(t *testing.T, tests []GetTest, runner func(GetTest) (interface{},
501
490
502
491
value , dataType , err := runner (test )
503
492
504
- if checkFoundAndNoError (t , "Get()" , test , dataType , value , err ) {
493
+ if getTestCheckFoundAndNoError (t , testKind , test , dataType , value , err ) {
505
494
if test .data == nil {
506
495
t .Errorf ("MALFORMED TEST: %v" , test )
507
496
continue
508
497
}
509
498
510
- if ok , expected := typeChecker (test , value ); ! ok {
499
+ if ok , expected := resultChecker (test , value ); ! ok {
511
500
if expectedBytes , ok := expected .([]byte ); ok {
512
501
expected = string (expectedBytes )
513
502
}
514
503
if valueBytes , ok := value .([]byte ); ok {
515
504
value = string (valueBytes )
516
505
}
517
- t .Errorf ("Test '%s' expected to return value %v, but did returned %v instead" , test .desc , expected , value )
506
+ t .Errorf ("%s test '%s' expected to return value %v, but did returned %v instead" , testKind , test .desc , expected , value )
518
507
}
519
508
}
520
509
}
521
510
}
522
511
523
512
func TestGet (t * testing.T ) {
524
- runTests ( t , getTests ,
513
+ runGetTests ( t , "Get()" , getTests ,
525
514
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
526
515
value , dataType , _ , err = Get ([]byte (test .json ), test .path ... )
527
516
return
@@ -534,7 +523,7 @@ func TestGet(t *testing.T) {
534
523
}
535
524
536
525
func TestGetString (t * testing.T ) {
537
- runTests ( t , getStringTests ,
526
+ runGetTests ( t , "GetString()" , getStringTests ,
538
527
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
539
528
value , err = GetString ([]byte (test .json ), test .path ... )
540
529
return value , String , err
@@ -547,7 +536,7 @@ func TestGetString(t *testing.T) {
547
536
}
548
537
549
538
func TestGetInt (t * testing.T ) {
550
- runTests ( t , getIntTests ,
539
+ runGetTests ( t , "GetInt()" , getIntTests ,
551
540
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
552
541
value , err = GetInt ([]byte (test .json ), test .path ... )
553
542
return value , Number , err
@@ -560,7 +549,7 @@ func TestGetInt(t *testing.T) {
560
549
}
561
550
562
551
func TestGetFloat (t * testing.T ) {
563
- runTests ( t , getFloatTests ,
552
+ runGetTests ( t , "GetFloat()" , getFloatTests ,
564
553
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
565
554
value , err = GetFloat ([]byte (test .json ), test .path ... )
566
555
return value , Number , err
@@ -573,7 +562,7 @@ func TestGetFloat(t *testing.T) {
573
562
}
574
563
575
564
func TestGetBoolean (t * testing.T ) {
576
- runTests ( t , getBoolTests ,
565
+ runGetTests ( t , "GetBoolean()" , getBoolTests ,
577
566
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
578
567
value , err = GetBoolean ([]byte (test .json ), test .path ... )
579
568
return value , Boolean , err
@@ -586,7 +575,7 @@ func TestGetBoolean(t *testing.T) {
586
575
}
587
576
588
577
func TestGetSlice (t * testing.T ) {
589
- runTests ( t , getArrayTests ,
578
+ runGetTests ( t , "Get()-for-arrays" , getArrayTests ,
590
579
func (test GetTest ) (value interface {}, dataType ValueType , err error ) {
591
580
value , dataType , _ , err = Get ([]byte (test .json ), test .path ... )
592
581
return
@@ -627,3 +616,164 @@ func TestArrayEach(t *testing.T) {
627
616
}
628
617
}, "a" , "b" )
629
618
}
619
+
620
+ type ParseTest struct {
621
+ in string
622
+ intype ValueType
623
+ out interface {}
624
+ isErr bool
625
+ }
626
+
627
+ var parseBoolTests = []ParseTest {
628
+ ParseTest {
629
+ in : "true" ,
630
+ intype : Boolean ,
631
+ out : true ,
632
+ },
633
+ ParseTest {
634
+ in : "false" ,
635
+ intype : Boolean ,
636
+ out : false ,
637
+ },
638
+ ParseTest {
639
+ in : "foo" ,
640
+ intype : Boolean ,
641
+ isErr : true ,
642
+ },
643
+ ParseTest {
644
+ in : "trux" ,
645
+ intype : Boolean ,
646
+ isErr : true ,
647
+ },
648
+ ParseTest {
649
+ in : "truex" ,
650
+ intype : Boolean ,
651
+ isErr : true ,
652
+ },
653
+ ParseTest {
654
+ in : "" ,
655
+ intype : Boolean ,
656
+ isErr : true ,
657
+ },
658
+ }
659
+
660
+ var parseFloatTest = []ParseTest {
661
+ ParseTest {
662
+ in : "0" ,
663
+ intype : Number ,
664
+ out : float64 (0 ),
665
+ },
666
+ ParseTest {
667
+ in : "0.0" ,
668
+ intype : Number ,
669
+ out : float64 (0.0 ),
670
+ },
671
+ ParseTest {
672
+ in : "1" ,
673
+ intype : Number ,
674
+ out : float64 (1 ),
675
+ },
676
+ ParseTest {
677
+ in : "1.234" ,
678
+ intype : Number ,
679
+ out : float64 (1.234 ),
680
+ },
681
+ ParseTest {
682
+ in : "1.234e5" ,
683
+ intype : Number ,
684
+ out : float64 (1.234e5 ),
685
+ },
686
+ ParseTest {
687
+ in : "-1.234e5" ,
688
+ intype : Number ,
689
+ out : float64 (- 1.234e5 ),
690
+ },
691
+ ParseTest {
692
+ in : "+1.234e5" , // Note: + sign not allowed under RFC7159, but our parser accepts it since it uses strconv.ParseFloat
693
+ intype : Number ,
694
+ out : float64 (1.234e5 ),
695
+ },
696
+ ParseTest {
697
+ in : "1.2.3" ,
698
+ intype : Number ,
699
+ isErr : true ,
700
+ },
701
+ ParseTest {
702
+ in : "1..1" ,
703
+ intype : Number ,
704
+ isErr : true ,
705
+ },
706
+ ParseTest {
707
+ in : "1a" ,
708
+ intype : Number ,
709
+ isErr : true ,
710
+ },
711
+ ParseTest {
712
+ in : "" ,
713
+ intype : Number ,
714
+ isErr : true ,
715
+ },
716
+ }
717
+
718
+ // parseTestCheckNoError checks the error return from Parse*() against the test case expectations.
719
+ // Returns true the test should proceed to checking the actual data returned from Parse*(), or false if the test is finished.
720
+ func parseTestCheckNoError (t * testing.T , testKind string , test ParseTest , value interface {}, err error ) bool {
721
+ if isErr := (err != nil ); test .isErr != isErr {
722
+ // If the call didn't match the error expectation, fail
723
+ t .Errorf ("%s test '%s' isErr mismatch: expected %t, obtained %t (err %v). Obtained value: %v" , testKind , test .in , test .isErr , isErr , err , value )
724
+ return false
725
+ } else if isErr {
726
+ // Else, if there was an error, don't fail and don't check isFound or the value
727
+ return false
728
+ } else {
729
+ // Else, there was no error and a value was found, so check the value
730
+ return true
731
+ }
732
+ }
733
+
734
+ func runParseTests (t * testing.T , testKind string , tests []ParseTest , runner func (ParseTest ) (interface {}, error ), resultChecker func (ParseTest , interface {}) (bool , interface {})) {
735
+ for _ , test := range tests {
736
+ value , err := runner (test )
737
+
738
+ if parseTestCheckNoError (t , testKind , test , value , err ) {
739
+ if test .out == nil {
740
+ t .Errorf ("MALFORMED TEST: %v" , test )
741
+ continue
742
+ }
743
+
744
+ if ok , expected := resultChecker (test , value ); ! ok {
745
+ if expectedBytes , ok := expected .([]byte ); ok {
746
+ expected = string (expectedBytes )
747
+ }
748
+ if valueBytes , ok := value .([]byte ); ok {
749
+ value = string (valueBytes )
750
+ }
751
+ t .Errorf ("%s test '%s' expected to return value %v, but did returned %v instead" , testKind , test .in , expected , value )
752
+ }
753
+ }
754
+ }
755
+ }
756
+
757
+ func TestParseBoolean (t * testing.T ) {
758
+ runParseTests (t , "ParseBoolean()" , parseBoolTests ,
759
+ func (test ParseTest ) (value interface {}, err error ) {
760
+ return ParseBoolean ([]byte (test .in ))
761
+ },
762
+ func (test ParseTest , obtained interface {}) (bool , interface {}) {
763
+ expected := test .out .(bool )
764
+ return obtained .(bool ) == expected , expected
765
+ },
766
+ )
767
+ }
768
+
769
+ func TestParseFloat (t * testing.T ) {
770
+ runParseTests (t , "ParseFloat()" , parseFloatTest ,
771
+ func (test ParseTest ) (value interface {}, err error ) {
772
+ return ParseFloat ([]byte (test .in ))
773
+ },
774
+ func (test ParseTest , obtained interface {}) (bool , interface {}) {
775
+ expected := test .out .(float64 )
776
+ return obtained .(float64 ) == expected , expected
777
+ },
778
+ )
779
+ }
0 commit comments