-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseek_test.go
48 lines (37 loc) · 1.21 KB
/
seek_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
package json
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecoderSeek(tb *testing.T) {
var d Decoder
for _, tc := range []struct {
In string
Out string
Err error
Path []interface{}
}{
{In: `1`, Out: `1`, Path: []interface{}{}},
{In: `{"a":1}`, Out: `1`, Path: []interface{}{"a"}},
{In: `{"a":{"b":"c"}}`, Out: `"c"`, Path: []interface{}{"a", "b"}},
{In: `{"a":{"b":"c", "d": "e", "f": [0, 1]}}`, Out: `[0, 1]`, Path: []interface{}{"a", "f"}},
{In: `[0,1,2]`, Out: `0`, Path: []interface{}{0}},
{In: `[0,1,2]`, Out: `1`, Path: []interface{}{1}},
{In: `[0,1,2, 3, 4, 5]`, Out: `5`, Path: []interface{}{5}},
{In: `[0,1,2]`, Out: `2`, Path: []interface{}{-1}},
{In: `["a", "b", "c"]`, Out: `"a"`, Path: []interface{}{-3}},
{In: `{"a":{"b":[{"c": "d"}, {"c": [6]}]}}`, Out: `[6]`, Path: []interface{}{"a", "b", 1, "c"}},
{In: `{"a":{"b":"c"}}`, Err: ErrNoSuchKey, Path: []interface{}{"a", "c"}},
} {
st, err := d.Seek([]byte(tc.In), 0, tc.Path...)
if tc.Err == nil {
assert.NoError(tb, err)
} else {
assert.ErrorIs(tb, err, tc.Err)
continue
}
end, err := d.Skip([]byte(tc.In), st)
assert.NoError(tb, err)
assert.Equal(tb, tc.Out, tc.In[st:end])
}
}