-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpointer_test.go
351 lines (321 loc) · 7.93 KB
/
pointer_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2016-2020 Olivier Mengué. All rights reserved.
// Use of this source code is governed by the Apache 2.0 license that
// can be found in the LICENSE file.
package jsonptr_test
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/dolmen-go/jsonptr"
)
var _ fmt.Stringer = jsonptr.Pointer{}
var parseTests = [...]struct {
in string
out jsonptr.Pointer
err error
}{
{"", nil, nil},
{"a", nil, jsonptr.ErrSyntax},
{"~", nil, jsonptr.ErrSyntax},
{"/", jsonptr.Pointer{""}, nil},
{"////", jsonptr.Pointer{"", "", "", ""}, nil},
{"/a", jsonptr.Pointer{"a"}, nil},
{"/~", nil, &jsonptr.BadPointerError{"/~", jsonptr.ErrSyntax}},
{"/~x", nil, &jsonptr.BadPointerError{"/~x", jsonptr.ErrSyntax}},
{"/a~", nil, &jsonptr.BadPointerError{"/a~", jsonptr.ErrSyntax}},
{"/a~x", nil, &jsonptr.BadPointerError{"/a~x", jsonptr.ErrSyntax}},
{"/abc/~", nil, &jsonptr.BadPointerError{"/abc/~", jsonptr.ErrSyntax}},
{"/abc/~/b", nil, &jsonptr.BadPointerError{"/abc/~", jsonptr.ErrSyntax}},
{"/abc/~x", nil, &jsonptr.BadPointerError{"/abc/~x", jsonptr.ErrSyntax}},
{"/abc/a~", nil, &jsonptr.BadPointerError{"/abc/a~", jsonptr.ErrSyntax}},
{"/~0", jsonptr.Pointer{"~"}, nil},
{"/~1", jsonptr.Pointer{"/"}, nil},
{"/~0~0", jsonptr.Pointer{"~~"}, nil},
{"/~0~1", jsonptr.Pointer{"~/"}, nil},
{"/~1~0", jsonptr.Pointer{"/~"}, nil},
{"/~1~1", jsonptr.Pointer{"//"}, nil},
{"/abc/def~0/ghi", jsonptr.Pointer{"abc", "def~", "ghi"}, nil},
{"/abc/def~0/g~1hi", jsonptr.Pointer{"abc", "def~", "g/hi"}, nil},
// Real test cases
{"/definitions/Location", jsonptr.Pointer{"definitions", "Location"}, nil},
{"/paths/~1home~1dolmen", jsonptr.Pointer{"paths", "/home/dolmen"}, nil},
{"/paths/~0dolmen", jsonptr.Pointer{"paths", "~dolmen"}, nil},
}
func TestPointerParse(t *testing.T) {
targets := [...]struct {
Parse func(string) (jsonptr.Pointer, error)
Stringify func(jsonptr.Pointer) string
}{
{
Parse: jsonptr.Parse,
Stringify: func(p jsonptr.Pointer) string { return p.String() },
},
{
Parse: func(s string) (jsonptr.Pointer, error) {
var p jsonptr.Pointer
err := p.UnmarshalText([]byte(s))
return p, err
},
Stringify: func(p jsonptr.Pointer) string {
s, err := p.MarshalText()
if err != nil {
panic(fmt.Sprintf("unexpected error %q", err))
}
return string(s)
},
},
}
for _, test := range parseTests {
if test.err != nil {
t.Logf("%q => %s", test.in, test.err)
} else {
t.Logf("%q => %#v", test.in, test.out)
}
for _, target := range targets {
out, err := target.Parse(test.in)
if (err == nil) != (test.err == nil) {
t.Errorf("got error: %q", err)
} else if (out == nil) != (test.out == nil) || len(out) != len(test.out) {
t.Errorf("got %#v", out)
} else if err == nil {
for i, part := range out {
if part != test.out[i] {
t.Errorf("got %#v", out)
break
}
}
ptr := target.Stringify(out)
if ptr != test.in {
t.Errorf("roundtrip failure: got %q != %q", ptr, test.in)
}
} else if !reflect.DeepEqual(err, test.err) {
// TODO fix jsonptr.Parse to match UnmarshalText
t.Logf("error mismatch: want %T %q, got %T %q",
test.err, test.err,
err, err,
)
}
}
}
}
// AltParse is another implementation of Parse that aimed
// to be faster, but isn't for the most common case (no escape)
// See BenchmarkParse for comparison.
func AltParse(pointer string) (jsonptr.Pointer, error) {
if pointer == "" {
return nil, nil
}
if pointer[0] != '/' {
return nil, jsonptr.ErrSyntax
}
ptr := strings.Split(pointer[1:], "/")
p := 1
i := 0
for {
q := p
for q < len(pointer) {
if pointer[q] == '~' {
break
}
q++
}
if q == len(pointer) {
break
}
for p+len(ptr[i]) < q {
i++
p += len(ptr) + 1
}
var err error
ptr[i], err = jsonptr.UnescapeString(ptr[i])
if err != nil {
return nil, err
}
i++
if i == len(ptr) {
break
}
}
return ptr, nil
}
func BenchmarkParse(b *testing.B) {
implementations := [...]struct {
name string
parse func(string) (jsonptr.Pointer, error)
}{
{"jsonptr.Parse", jsonptr.Parse},
{"AltParse", AltParse},
}
for _, test := range parseTests {
for _, impl := range implementations {
b.Run(fmt.Sprintf("%q/%s", test.in, impl.name), func(b *testing.B) {
parse := impl.parse
ptr := test.in
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = parse(ptr)
}
})
}
}
}
func TestPointer(t *testing.T) {
var p jsonptr.Pointer
if !p.IsRoot() {
t.Fatal("Empty must give a root pointer")
}
if p.String() != "" {
t.Fatal("A root pointer is \"\"")
}
// Moves operations over p defined as functions
var (
up = p.Up
property = func(name string) func() *jsonptr.Pointer {
return func() *jsonptr.Pointer {
q := p.Property(name)
if q.LeafName() != name {
panic("LeafName() mismatch")
}
return q
}
}
index = func(i int) func() *jsonptr.Pointer {
return func() *jsonptr.Pointer {
q := p.Index(i)
if j, err := q.LeafIndex(); err != nil || j != i {
panic("LeafIndex() mismatch")
}
return q
}
}
chain = func(moves ...func() *jsonptr.Pointer) func() *jsonptr.Pointer {
return func() *jsonptr.Pointer {
q := &p
for _, m := range moves {
q = m()
}
return q
}
}
)
for _, test := range []struct {
move func() *jsonptr.Pointer
expected string
}{
{property("foo"), "/foo"},
{property("bar"), "/foo/bar"},
{index(0), "/foo/bar/0"},
{up, "/foo/bar"},
{up, "/foo"},
{index(4), "/foo/4"},
{up, "/foo"},
{property("a/a"), "/foo/a~1a"},
{chain(up, property("a~a")), "/foo/a~0a"},
{chain(up, property("~~//")), "/foo/~0~0~1~1"},
{chain(up, property("~01")), "/foo/~001"},
{chain(up, up), ""},
} {
t.Logf("Moving to \"%s\"", test.expected)
q := test.move()
if q != &p {
t.Errorf("Chaining failure")
}
got := p.String()
if got != test.expected {
t.Fatalf("got: %s, expected: %s", got, test.expected)
}
if (got == "") != p.IsRoot() {
t.Error("IsRoot failure")
}
if !p.IsRoot() {
got = p.Property(p.Pop()).String()
if got != test.expected {
t.Fatalf("Pop+Property => got: %s, expected: %s", got, test.expected)
}
}
r, err := jsonptr.Parse(test.expected)
if err != nil {
t.Errorf("Can't parse %s", test.expected)
} else {
got = r.String()
if got != test.expected {
t.Errorf("got: %s, expected: %s", got, test.expected)
}
}
r = nil
r = p.Copy()
if r.String() != test.expected {
t.Errorf("Clone error: got %s, expected %s", r.String(), test.expected)
} else {
r.Property("baz")
if r.String() != test.expected+"/baz" {
t.Errorf("Property() failure in clone")
}
// Check that p has not changed
if p.String() != test.expected {
t.Errorf("Cloning failure: p is altered")
}
}
}
if !p.IsRoot() {
t.Fatal("IsRoot failure")
}
}
func ExamplePointer_conversion() {
fmt.Printf("%q\n\n", jsonptr.Pointer{"foo", "bar", "a/b", "x~y"})
for _, ptr := range []jsonptr.Pointer{
nil,
{},
{"a", "b"},
{"a~/b"},
} {
fmt.Printf("%q\n", ptr.String())
}
// Output:
// "/foo/bar/a~1b/x~0y"
//
// ""
// ""
// "/a/b"
// "/a~0~1b"
}
func ExamplePointer_navigation() {
ptr := jsonptr.Pointer{}
fmt.Printf("%q\n", ptr)
ptr.Property("foo").Index(3).Property("a/b")
fmt.Printf("%q\n", ptr.String())
ptr.Up()
fmt.Printf("%q\n", ptr)
ptr.Property("c~d")
fmt.Printf("%q\n", ptr)
// Output:
// ""
// "/foo/3/a~1b"
// "/foo/3"
// "/foo/3/c~0d"
}
func TestPointerClone(t *testing.T) {
orig := jsonptr.Pointer{"foo"}
clone := orig.Copy()
if clone.String() != "/foo" {
t.Errorf("Failure!")
}
orig.Up().Property("bar")
if clone.String() != "/foo" {
t.Errorf("Failure!")
}
}
// TestPointerIn tests Parse() and Pointer.In()
func TestPointerIn(t *testing.T) {
(&getTester{
t: t,
Get: func(doc interface{}, pointer string) (interface{}, error) {
ptr, err := jsonptr.Parse(pointer)
if err != nil {
return nil, err
}
return ptr.In(doc)
},
}).runTest()
}