-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonptr_test.go
168 lines (159 loc) · 3.08 KB
/
jsonptr_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package jsonptr_test
import (
"bytes"
"reflect"
"testing"
"github.com/lestrrat-go/jsonptr"
)
func TestParse(t *testing.T) {
const src = `{"a": 1, "b": 2, "c": {"d": 3, "e": 4}, "f": [5, 6, "g", {"h": 7}, ["i", 8, 9]]}`
c, err := jsonptr.Parse([]byte(src))
if err != nil {
t.Fatalf("failed to parse JSON: %s", err)
}
testcases := []struct {
Path string
Expected interface{}
Source []byte
Error bool
}{
{
Path: "/a",
// because we're doing Unmarshal(&interface{}), we need to use float64 here
Expected: float64(1),
Source: []byte{'1'},
},
{
Path: "/b",
// because we're doing Unmarshal(&interface{}), we need to use float64 here
Expected: float64(2),
Source: []byte{'2'},
},
{
Path: "/c",
Expected: map[string]interface{}{
"d": float64(3),
"e": float64(4),
},
Source: []byte(`{"d": 3, "e": 4}`),
},
{
Path: "/c/d",
Expected: float64(3),
Source: []byte{'3'},
},
{
Path: "/c/e",
Expected: float64(4),
Source: []byte{'4'},
},
{
Path: "/f",
Expected: []interface{}{
float64(5),
float64(6),
"g",
map[string]interface{}{
"h": float64(7),
},
[]interface{}{
"i",
float64(8),
float64(9),
},
},
Source: []byte(`[5, 6, "g", {"h": 7}, ["i", 8, 9]]`),
},
{
Path: "/f/0",
Expected: float64(5),
Source: []byte{'5'},
},
{
Path: "/f/1",
Expected: float64(6),
Source: []byte{'6'},
},
{
Path: "/f/2",
Expected: "g",
Source: []byte{'"', 'g', '"'},
},
{
Path: "/f/3",
Expected: map[string]interface{}{
"h": float64(7),
},
Source: []byte(`{"h": 7}`),
},
{
Path: "/f/3/h",
Expected: float64(7),
Source: []byte{'7'},
},
{
Path: "/f/4",
Expected: []interface{}{
"i",
float64(8),
float64(9),
},
Source: []byte(`["i", 8, 9]`),
},
{
Path: "/f/4/0",
Expected: "i",
Source: []byte{'"', 'i', '"'},
},
{
Path: "/f/4/1",
Expected: float64(8),
Source: []byte{'8'},
},
{
Path: "/f/4/2",
Expected: float64(9),
Source: []byte{'9'},
},
{
Path: "/g",
Error: true,
},
{
Path: "/c/g",
Error: true,
},
{
Path: "/f/5",
Error: true,
},
}
for _, tc := range testcases {
t.Run(tc.Path, func(t *testing.T) {
var v interface{}
if tc.Error {
if _, err := c.Get(tc.Path); err == nil {
t.Errorf("c.Get should fail")
}
if err := c.Unmarshal(tc.Path, &v); err == nil {
t.Errorf("c.Unmarshal should fail")
}
} else {
fragment, err := c.Get(tc.Path)
if err != nil {
t.Fatalf("failed to find %s: %s", tc.Path, err)
}
if !bytes.Equal(fragment, tc.Source) {
t.Fatalf("expected %s to be %s, but got %s", tc.Path, tc.Expected, fragment)
}
if err := c.Unmarshal(tc.Path, &v); err != nil {
t.Fatalf("failed to find %s: %s", tc.Path, err)
}
if !reflect.DeepEqual(v, tc.Expected) {
t.Fatalf("expected %s to be %#v, but got %#v", tc.Path, tc.Expected, v)
}
t.Logf("%s = %#v", tc.Path, v)
}
})
}
}