-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonptr_test.go
144 lines (140 loc) · 2.5 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
package jsonptr_test
import (
"encoding/json"
"github.com/0x51-dev/jsonptr"
"testing"
)
func TestJsonPointer_Eval(t *testing.T) {
documentStr := `{
"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
}`
var document map[string]any
if err := json.Unmarshal([]byte(documentStr), &document); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
ptr string
val any
}{
{"/foo", []any{"bar", "baz"}},
{"/foo/0", "bar"},
{"/", 0},
{"/a~1b", 1},
{"/c%d", 2},
{"/e^f", 3},
{"/g|h", 4},
{"/i\\j", 5},
{"/k\"l", 6},
{"/ ", 7},
{"/m~0n", 8},
} {
ptr, err := jsonptr.ParseJsonPointer(test.ptr)
if err != nil {
t.Fatal(err)
}
val, err := ptr.Eval(document)
if err != nil {
t.Fatal(err)
}
cmp(t, test.val, val)
}
}
func TestRelativeJsonPointer_Eval(t *testing.T) {
documentStr := `{
"foo": ["bar", "baz", "biz"],
"highly": {
"nested": {
"objects": true
}
}
}`
var document map[string]any
if err := json.Unmarshal([]byte(documentStr), &document); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
path string
tests []struct {
ptr string
val any
}
}{
{
path: "/foo/1",
tests: []struct {
ptr string
val any
}{
{"0", "baz"},
{"1/0", "bar"},
{"0-1", "bar"},
{"2/highly/nested/objects", true},
{"0#", 1},
{"0+1#", 2},
{"1#", "foo"},
},
},
{
path: "/highly/nested",
tests: []struct {
ptr string
val any
}{
{"0/objects", true},
{"1/nested/objects", true},
{"2/foo/0", "bar"},
{"0#", "nested"},
{"1#", "highly"},
},
},
} {
t.Run(test.path, func(t *testing.T) {
path := test.path
for _, test := range test.tests {
start, err := jsonptr.ParseJsonPointer(path)
if err != nil {
t.Fatal(err)
}
ptr, err := jsonptr.ParseRelativeJsonPointer(test.ptr)
if err != nil {
t.Fatal(err)
}
val, err := ptr.Eval(start, document)
if err != nil {
t.Fatal(err)
}
cmp(t, test.val, val)
}
})
}
}
func cmp(t *testing.T, a, b any) {
switch a := a.(type) {
case []any:
for i, a := range a {
if a != b.([]any)[i] {
t.Fatalf("expected %v, got %v", a, b)
}
}
default:
switch a := a.(type) {
case string:
if b != a {
t.Fatalf("expected %v, got %v", a, b)
}
case int:
if b != float64(a) { // JSON numbers are always float64.
t.Fatalf("expected %v, got %v", a, b)
}
}
}
}