-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer_test.go
More file actions
138 lines (127 loc) · 3.02 KB
/
pointer_test.go
File metadata and controls
138 lines (127 loc) · 3.02 KB
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
package jsonpatch
import (
"encoding/json"
"testing"
)
func mustParseJSON(s string) interface{} {
var v interface{}
if err := json.Unmarshal([]byte(s), &v); err != nil {
panic(err)
}
return v
}
func TestParsePointer(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
wantStr string
}{
{"empty string (root)", "", false, ""},
{"single key", "/foo", false, "/foo"},
{"nested path", "/a/b/c", false, "/a/b/c"},
{"array index", "/foo/0", false, "/foo/0"},
{"tilde escape ~0", "/~0", false, "/~0"},
{"slash escape ~1", "/~1", false, "/~1"},
{"combined escape", "/~01", false, "/~01"},
{"missing leading slash", "foo", true, ""},
{"invalid escape ~2", "/~2", true, ""},
{"dangling tilde at end", "/foo~", true, ""},
{"invalid escape ~a", "/~a", true, ""},
{"tilde at end of multi-token", "/foo/bar~", true, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := ParsePointer(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParsePointer(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
return
}
if !tt.wantErr && p.String() != tt.wantStr {
t.Errorf("ParsePointer(%q).String() = %q, want %q", tt.input, p.String(), tt.wantStr)
}
})
}
}
func TestPointerEscaping(t *testing.T) {
tests := []struct {
name string
token string
want string
}{
{"tilde", "~", "~0"},
{"slash", "/", "~1"},
{"tilde-one literal", "~1", "~01"},
{"no escape needed", "foo", "foo"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := escapePointerToken(tt.token)
if got != tt.want {
t.Errorf("escapePointerToken(%q) = %q, want %q", tt.token, got, tt.want)
}
back := unescapePointerToken(got)
if back != tt.token {
t.Errorf("round-trip failed: %q -> %q -> %q", tt.token, got, back)
}
})
}
}
func TestPointerEvaluate(t *testing.T) {
doc := mustParseJSON(`{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
" ": 7,
"m~n": 8
}`)
tests := []struct {
pointer string
want interface{}
}{
{"/foo", []interface{}{"bar", "baz"}},
{"/foo/0", "bar"},
{"/", float64(0)},
{"/a~1b", float64(1)},
{"/m~0n", float64(8)},
}
for _, tt := range tests {
t.Run(tt.pointer, func(t *testing.T) {
p, err := ParsePointer(tt.pointer)
if err != nil {
t.Fatal(err)
}
got, err := p.Evaluate(doc)
if err != nil {
t.Fatal(err)
}
if !jsonEqual(got, tt.want) {
t.Errorf("Evaluate(%q) = %v, want %v", tt.pointer, got, tt.want)
}
})
}
}
func TestPointerIsPrefixOf(t *testing.T) {
tests := []struct {
a, b string
want bool
}{
{"/a/b", "/a/b/c", true},
{"/a", "/a/b", true},
{"/a/b", "/a/b", false},
{"/a/b/c", "/a/b", false},
{"", "/a", true},
}
for _, tt := range tests {
t.Run(tt.a+"_prefix_of_"+tt.b, func(t *testing.T) {
pa, _ := ParsePointer(tt.a)
pb, _ := ParsePointer(tt.b)
if got := pa.IsPrefixOf(pb); got != tt.want {
t.Errorf("(%q).IsPrefixOf(%q) = %v, want %v", tt.a, tt.b, got, tt.want)
}
})
}
}