@@ -627,3 +627,74 @@ func TestDecoderArrayFunc(t *testing.T) {
627627 var f DecodeArrayFunc
628628 assert .True (t , f .IsNil ())
629629}
630+
631+ type testArrayStrings [3 ]string
632+
633+ func (a * testArrayStrings ) UnmarshalJSONArray (dec * Decoder ) error {
634+ var str string
635+ if err := dec .String (& str ); err != nil {
636+ return err
637+ }
638+ a [dec .Index ()] = str
639+ return nil
640+ }
641+
642+ func TestArrayStrings (t * testing.T ) {
643+ data := []byte (`["a", "b", "c"]` )
644+ arr := testArrayStrings {}
645+ err := Unmarshal (data , & arr )
646+ assert .Nil (t , err , "err must be nil" )
647+ assert .Equal (t , "a" , arr [0 ], "arr[0] must be equal to 'a'" )
648+ assert .Equal (t , "b" , arr [1 ], "arr[1] must be equal to 'b'" )
649+ assert .Equal (t , "c" , arr [2 ], "arr[2] must be equal to 'c'" )
650+ }
651+
652+ type testSliceArraysStrings struct {
653+ arrays []testArrayStrings
654+ t * testing.T
655+ }
656+
657+ func (s * testSliceArraysStrings ) UnmarshalJSONArray (dec * Decoder ) error {
658+ var a testArrayStrings
659+ assert .Equal (s .t , len (s .arrays ), dec .Index (), "decoded array index must be equal to current slice len" )
660+ if err := dec .AddArray (& a ); err != nil {
661+ return err
662+ }
663+ assert .Equal (s .t , len (s .arrays ), dec .Index (), "decoded array index must be equal to current slice len" )
664+ s .arrays = append (s .arrays , a )
665+ return nil
666+ }
667+
668+ func TestIndex (t * testing.T ) {
669+ testCases := []struct {
670+ name string
671+ json string
672+ expectedResult []testArrayStrings
673+ }{
674+ {
675+ name : "basic-test" ,
676+ json : `[["a","b","c"],["1","2","3"],["x","y","z"]]` ,
677+ expectedResult : []testArrayStrings {{"a" , "b" , "c" }, {"1" , "2" , "3" }, {"x" , "y" , "z" }},
678+ },
679+ {
680+ name : "basic-test-null" ,
681+ json : `[["a","b","c"],null,["x","y","z"]]` ,
682+ expectedResult : []testArrayStrings {{"a" , "b" , "c" }, {"" , "" , "" }, {"x" , "y" , "z" }},
683+ },
684+ }
685+
686+ for _ , testCase := range testCases {
687+ t .Run (testCase .name , func (t * testing.T ) {
688+ s := make ([]testArrayStrings , 0 )
689+ dec := BorrowDecoder (strings .NewReader (testCase .json ))
690+ defer dec .Release ()
691+ a := testSliceArraysStrings {arrays : s , t : t }
692+ err := dec .Decode (& a )
693+ assert .Nil (t , err , "err should be nil" )
694+ assert .Zero (t , dec .Index (), "Index() must return zero after decoding" )
695+ for k , v := range testCase .expectedResult {
696+ assert .Equal (t , v , a .arrays [k ], "value at given index should be the same as expected results" )
697+ }
698+ })
699+ }
700+ }
0 commit comments