-
Notifications
You must be signed in to change notification settings - Fork 9
/
map_test.go
177 lines (157 loc) · 3.54 KB
/
map_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
169
170
171
172
173
174
175
176
177
package jsonpointer
import (
"io/ioutil"
"reflect"
"testing"
"github.com/dustin/gojson"
)
const objSrc = `{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8,
"g/n/r": "has slash, will travel",
"g": { "n": {"r": "where's tito?"}}
}`
var obj = map[string]interface{}{}
var tests = []struct {
path string
exp interface{}
}{
{"", obj},
{"/foo", []interface{}{"bar", "baz"}},
{"/foo/0", "bar"},
{"/foo/99", nil},
{"/foo/0/3", nil},
{"/", 0.0},
{"/a~1b", 1.0},
{"/c%d", 2.0},
{"/e^f", 3.0},
{"/g|h", 4.0},
{"/i\\j", 5.0},
{"/k\"l", 6.0},
{"/ ", 7.0},
{"/m~0n", 8.0},
{"/g~1n~1r", "has slash, will travel"},
{"/g/n/r", "where's tito?"},
}
func init() {
err := json.Unmarshal([]byte(objSrc), &obj)
if err != nil {
panic(err)
}
}
func TestPaths(t *testing.T) {
for _, test := range tests {
got := Get(obj, test.path)
if !reflect.DeepEqual(got, test.exp) {
t.Errorf("On %v, expected %+v (%T), got %+v (%T)",
test.path, test.exp, test.exp, got, got)
} else {
t.Logf("Success - got %v for %v", got, test.path)
}
}
}
func BenchmarkPaths(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range tests {
Get(obj, test.path)
}
}
}
func BenchmarkParseAndPath(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range tests {
o := map[string]interface{}{}
err := json.Unmarshal([]byte(objSrc), &o)
if err != nil {
b.Fatalf("Error parsing: %v", err)
}
Get(o, test.path)
}
}
}
var bug3Data = []byte(`{"foo" : "bar"}`)
func TestFindSpaceBeforeColon(t *testing.T) {
val, err := Find(bug3Data, "/foo")
if err != nil {
t.Fatalf("Failed to find /foo: %v", err)
}
x, ok := json.UnquoteBytes(val)
if !ok {
t.Fatalf("Failed to unquote json bytes from %q", val)
}
if string(x) != "bar" {
t.Fatalf("Expected %q, got %q", "bar", val)
}
}
func TestListSpaceBeforeColon(t *testing.T) {
ptrs, err := ListPointers(bug3Data)
if err != nil {
t.Fatalf("Error listing pointers: %v", err)
}
if len(ptrs) != 2 || ptrs[0] != "" || ptrs[1] != "/foo" {
t.Fatalf(`Expected ["", "/foo"], got %#v`, ptrs)
}
}
func TestIndexNotFoundSameAsPropertyNotFound(t *testing.T) {
data, err := ioutil.ReadFile("testdata/357.json")
if err != nil {
t.Fatalf("Error beer-sample brewery 357 data: %v", err)
}
expectedResult, expectedError := Find(data, "/doesNotExist")
missingVals := []string{
"/address/0",
"/address/1",
"/address2/1",
"/address2/2",
"/address3/0",
"/address3/1",
}
for _, a := range missingVals {
found, err := Find(data, a)
if !reflect.DeepEqual(err, expectedError) {
t.Errorf("Expected %v at %v, got %v", expectedError, a, err)
}
if !reflect.DeepEqual(expectedResult, found) {
t.Errorf("Expected %v at %v, got %v", expectedResult, a, found)
}
}
}
const bug822src = `{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
"k2": {},
" ": 7,
"m~n": 8,
"g/n/r": "has slash, will travel",
"g": { "n": {"r": "where's tito?"}},
"h": {}
}`
func TestListEmptyObjectPanic822(t *testing.T) {
ptrs, err := ListPointers([]byte(bug822src))
if err != nil {
t.Fatalf("Error parsing: %v", err)
}
t.Logf("Got pointers: %v", ptrs)
}
func TestFindEmptyObjectPanic823(t *testing.T) {
for _, test := range tests {
_, err := Find([]byte(bug822src), test.path)
if err != nil {
t.Errorf("Error looking for %v: %v", test.path, err)
}
}
}