-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdec_skip_cases_test.go
99 lines (95 loc) · 2.32 KB
/
dec_skip_cases_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
package jx
import (
"encoding/json"
"io"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func Test_skip(t *testing.T) {
type testCase struct {
ptr interface{}
inputs []string
}
var testCases []testCase
testCases = append(testCases, testCase{
ptr: (*string)(nil),
inputs: []string{
`""`, // valid
`"hello"`, // valid
`"`, // invalid
`"\"`, // invalid
`"\x00"`, // invalid
"\"\x00\"", // invalid
"\"\t\"", // invalid
`"\t"`, // valid
},
})
testCases = append(testCases, testCase{
ptr: (*[]interface{})(nil),
inputs: []string{
`[]`, // valid
`[1]`, // valid
`[ 1, "hello"]`, // valid
`[abc]`, // invalid
`[`, // invalid
`[[]`, // invalid
},
})
testCases = append(testCases, testCase{
ptr: (*float64)(nil),
inputs: []string{
"+1", // invalid
"-a", // invalid
"-\x00", // invalid, zero byte
"0.1", // valid
"0..1", // invalid, more dot
"1e+1", // valid
"1+1", // invalid
"1E1", // valid, e or E
"1ee1", // invalid
"100a", // invalid
"10.", // invalid
},
})
testCases = append(testCases, testCase{
ptr: (*struct{})(nil),
inputs: []string{
`{}`, // valid
`{"hello":"world"}`, // valid
`{hello:"world"}`, // invalid
`{"hello:"world"}`, // invalid
`{"hello","world"}`, // invalid
`{"hello":{}`, // invalid
`{"hello":{}}`, // valid
`{"hello":{}}}`, // invalid
`{"hello": { "hello": 1}}`, // valid
`{abc}`, // invalid
},
})
for _, testCase := range testCases {
valType := reflect.TypeOf(testCase.ptr).Elem()
for _, input := range testCase.inputs {
t.Run(input, func(t *testing.T) {
should := require.New(t)
ptrVal := reflect.New(valType)
stdErr := json.Unmarshal([]byte(input), ptrVal.Interface())
iter := DecodeStr(input)
if stdErr == nil {
should.NoError(iter.Skip())
should.ErrorIs(iter.Null(), io.ErrUnexpectedEOF)
} else {
should.Error(func() error {
if err := iter.Skip(); err != nil {
return err
}
if err := iter.Skip(); err != io.EOF {
return err
}
return nil
}())
}
})
}
}
}