-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencode_test.go
217 lines (207 loc) · 4.89 KB
/
encode_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
package jsonpatch
import (
"reflect"
"testing"
)
type testUser struct {
Name string
Age int
Email string
Child *testUser
Phones []string
M map[string]string
}
func TestBestMatch(t *testing.T) {
type Test struct {
A string `json:"AwesomeName"`
}
ty := reflect.TypeOf(Test{})
if name := bestMatch("AwesomeName", ty); name != "A" {
t.Fatal("best match did not work", name)
}
}
func TestAdd(t *testing.T) {
u := testUser{
Phones: []string{"37489239"},
}
p := []byte(`[
{"op": "add", "path": "/name", "value": "Calvin"},
{"op": "add", "path": "/age", "value": 6},
{"op": "add", "path": "/email", "value": "[email protected]"},
{"op": "add", "path": "/child/name", "value": "mr. bunny"},
{"op": "add", "path": "/phones/-", "value": "8390240670"},
{"op": "add", "path": "/phones/1", "value": "9096040676"},
{"op": "add", "path": "/m/a", "value": "hello world"}
]`)
err := Apply(p, &u)
if err != nil {
t.Fatal(err)
}
if u.Name != "Calvin" {
t.Fatal("name not set")
}
if u.Age != 6 {
t.Fatal("age not set")
}
if u.Email != "[email protected]" {
t.Fatal("email not set")
}
if u.Child.Name != "mr. bunny" {
t.Fatal("pointer value not set")
}
if len(u.Phones) != 3 || u.Phones[2] != "8390240670" {
t.Fatal("slice not set")
}
if u.Phones[1] != "9096040676" {
t.Fatal("slice not set")
}
if val, ok := u.M["a"]; !ok || val != "hello world" {
t.Fatal("map value not set")
}
}
func TestAdd2(t *testing.T) {
u := testUser{}
p := []byte(`[{"op": "add", "path": "/child", "value": {"name": "hobbes"}}]`)
err := Apply(p, &u)
if err != nil {
t.Fatal(err)
}
if u.Child == nil {
t.Fatal("child not initialized")
}
if u.Child.Name != "hobbes" {
t.Fatal("setting child failed", u.Child)
}
}
func TestAddSlice(t *testing.T) {
a := []*testUser{
&testUser{
Name: "hobbes",
Age: 100,
},
}
p := []byte(`[
{"op": "replace", "path": "/0/name", "value": "Calvin"},
{"op": "replace", "path": "/0/age", "value": 6}
]`)
err := Apply(p, &a)
if err != nil {
t.Fatal(err)
}
if a[0].Name != "Calvin" || a[0].Age != 6 {
t.Fatal("patch not set", *a[0])
}
}
func TestReplace(t *testing.T) {
u := testUser{
Name: "hobbes",
Age: 100,
Email: "[email protected]",
Child: &testUser{
Name: "Susie",
},
Phones: []string{"12830921"},
M: map[string]string{"a": "hello"},
}
p := []byte(`[
{"op": "replace", "path": "/name", "value": "Calvin"},
{"op": "replace", "path": "/age", "value": 6},
{"op": "replace", "path": "/email", "value": "[email protected]"},
{"op": "replace", "path": "/child/name", "value": "mr. bunny"},
{"op": "replace", "path": "/phones/0", "value": "8390240670"},
{"op": "replace", "path": "/m/a", "value": "hello world"}
]`)
err := Apply(p, &u)
if err != nil {
t.Fatal(err)
}
if u.Name != "Calvin" {
t.Fatal("name not set")
}
if u.Age != 6 {
t.Fatal("age not set")
}
if u.Email != "[email protected]" {
t.Fatal("email not set")
}
if u.Child.Name != "mr. bunny" {
t.Fatal("pointer value not set")
}
if len(u.Phones) != 1 || u.Phones[0] != "8390240670" {
t.Fatal("slice not set")
}
if val, ok := u.M["a"]; !ok || val != "hello world" {
t.Fatal("map value not set")
}
p = []byte(`{"op": "replace", "path": "/m/a", "value": "hello world"}`)
err = Apply(p, &u)
if err == nil {
t.Fatal("was supposed to fail", u.M)
}
}
func TestRemove(t *testing.T) {
u := testUser{
Name: "hobbes",
Age: 100,
Email: "[email protected]",
Child: &testUser{
Name: "Susie",
},
Phones: []string{"12830921"},
M: map[string]string{"a": "hello"},
}
p := []byte(`[
{"op": "remove", "path": "/name"},
{"op": "remove", "path": "/age"},
{"op": "remove", "path": "/email"},
{"op": "remove", "path": "/child/name"},
{"op": "remove", "path": "/phones/0"},
{"op": "remove", "path": "/m/a"}
]`)
err := Apply(p, &u)
if err != nil {
t.Fatal(err)
}
if u.Name == "hobbes" {
t.Fatal("name not removed")
}
if u.Age == 100 {
t.Fatal("age not removed")
}
if u.Email == "[email protected]" {
t.Fatal("email not removed")
}
if u.Child.Name == "Susie" {
t.Fatal("pointer value not removed")
}
if len(u.Phones) == 1 {
t.Fatal("slice element not removed")
}
if val, ok := u.M["a"]; ok && val != "" {
t.Fatal("map value not removed", val)
}
}
func TestTest(t *testing.T) {
u := testUser{
Name: "hobbes",
Age: 100,
Email: "[email protected]",
Child: &testUser{
Name: "Susie",
},
Phones: []string{"8390240670"},
M: map[string]string{"a": "hello"},
}
p := []byte(`[
{"op": "test", "path": "/name", "value": "hobbes"},
{"op": "test", "path": "/age", "value": 100},
{"op": "test", "path": "/email", "value": "[email protected]"},
{"op": "test", "path": "/child/name", "value": "Susie"},
{"op": "test", "path": "/phones/0", "value": "8390240670"},
{"op": "test", "path": "/m/a", "value": "hello"}
]`)
err := Apply(p, &u)
if err != nil {
t.Fatal(err)
}
}