forked from evanphx/json-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_test.go
255 lines (226 loc) · 5.01 KB
/
patch_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
package jsonpatch
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
)
func reformatJSON(j string) string {
buf := new(bytes.Buffer)
json.Indent(buf, []byte(j), "", " ")
return buf.String()
}
func compareJSON(a, b string) bool {
// return Equal([]byte(a), []byte(b))
var obj_a, obj_b map[string]interface{}
json.Unmarshal([]byte(a), &obj_a)
json.Unmarshal([]byte(b), &obj_b)
// fmt.Printf("Comparing %#v\nagainst %#v\n", obj_a, obj_b)
return reflect.DeepEqual(obj_a, obj_b)
}
func applyPatch(doc, patch string) (string, error) {
obj, err := DecodePatch([]byte(patch))
if err != nil {
panic(err)
}
out, err := obj.Apply([]byte(doc))
if err != nil {
return "", err
}
return string(out), nil
}
type Case struct {
doc, patch, result string
}
var Cases = []Case{
{
`{ "foo": "bar"}`,
`[
{ "op": "add", "path": "/baz", "value": "qux" }
]`,
`{
"baz": "qux",
"foo": "bar"
}`,
},
{
`{ "foo": [ "bar", "baz" ] }`,
`[
{ "op": "add", "path": "/foo/1", "value": "qux" }
]`,
`{ "foo": [ "bar", "qux", "baz" ] }`,
},
{
`{ "baz": "qux", "foo": "bar" }`,
`[ { "op": "remove", "path": "/baz" } ]`,
`{ "foo": "bar" }`,
},
{
`{ "foo": [ "bar", "qux", "baz" ] }`,
`[ { "op": "remove", "path": "/foo/1" } ]`,
`{ "foo": [ "bar", "baz" ] }`,
},
{
`{ "baz": "qux", "foo": "bar" }`,
`[ { "op": "replace", "path": "/baz", "value": "boo" } ]`,
`{ "baz": "boo", "foo": "bar" }`,
},
{
`{
"foo": {
"bar": "baz",
"waldo": "fred"
},
"qux": {
"corge": "grault"
}
}`,
`[ { "op": "move", "from": "/foo/waldo", "path": "/qux/thud" } ]`,
`{
"foo": {
"bar": "baz"
},
"qux": {
"corge": "grault",
"thud": "fred"
}
}`,
},
{
`{ "foo": [ "all", "grass", "cows", "eat" ] }`,
`[ { "op": "move", "from": "/foo/1", "path": "/foo/3" } ]`,
`{ "foo": [ "all", "cows", "eat", "grass" ] }`,
},
{
`{ "foo": "bar" }`,
`[ { "op": "add", "path": "/child", "value": { "grandchild": { } } } ]`,
`{ "foo": "bar", "child": { "grandchild": { } } }`,
},
{
`{ "foo": ["bar"] }`,
`[ { "op": "add", "path": "/foo/-", "value": ["abc", "def"] } ]`,
`{ "foo": ["bar", ["abc", "def"]] }`,
},
{
`{ "foo": "bar", "qux": { "baz": 1, "bar": null } }`,
`[ { "op": "remove", "path": "/qux/bar" } ]`,
`{ "foo": "bar", "qux": { "baz": 1 } }`,
},
{
`{ "foo": "bar" }`,
`[ { "op": "add", "path": "/baz", "value": null } ]`,
`{ "baz": null, "foo": "bar" }`,
},
}
type BadCase struct {
doc, patch string
}
var MutationTestCases = []BadCase{
{
`{ "foo": "bar", "qux": { "baz": 1, "bar": null } }`,
`[ { "op": "remove", "path": "/qux/bar" } ]`,
},
{
`{ "foo": "bar", "qux": { "baz": 1, "bar": null } }`,
`[ { "op": "replace", "path": "/qux/baz", "value": null } ]`,
},
}
var BadCases = []BadCase{
{
`{ "foo": "bar" }`,
`[ { "op": "add", "path": "/baz/bat", "value": "qux" } ]`,
},
}
func TestAllCases(t *testing.T) {
for _, c := range Cases {
out, err := applyPatch(c.doc, c.patch)
if err != nil {
t.Errorf("Unable to apply patch: %s", err)
}
if !compareJSON(out, c.result) {
t.Errorf("Patch did not apply. Expected:\n%s\n\nActual:\n%s",
reformatJSON(c.result), reformatJSON(out))
}
}
for _, c := range MutationTestCases {
out, err := applyPatch(c.doc, c.patch)
if err != nil {
t.Errorf("Unable to apply patch: %s", err)
}
if compareJSON(out, c.doc) {
t.Errorf("Patch did not apply. Original:\n%s\n\nPatched:\n%s",
reformatJSON(c.doc), reformatJSON(out))
}
}
for _, c := range BadCases {
_, err := applyPatch(c.doc, c.patch)
if err == nil {
t.Errorf("Patch should have failed to apply but it did not")
}
}
}
type TestCase struct {
doc, patch string
result bool
failedPath string
}
var TestCases = []TestCase{
{
`{
"baz": "qux",
"foo": [ "a", 2, "c" ]
}`,
`[
{ "op": "test", "path": "/baz", "value": "qux" },
{ "op": "test", "path": "/foo/1", "value": 2 }
]`,
true,
"",
},
{
`{ "baz": "qux" }`,
`[ { "op": "test", "path": "/baz", "value": "bar" } ]`,
false,
"/baz",
},
{
`{
"baz": "qux",
"foo": ["a", 2, "c"]
}`,
`[
{ "op": "test", "path": "/baz", "value": "qux" },
{ "op": "test", "path": "/foo/1", "value": "c" }
]`,
false,
"/foo/1",
},
{
`{ "baz": "qux" }`,
`[ { "op": "test", "path": "/foo", "value": 42 } ]`,
false,
"/foo",
},
{
`{ "baz": "qux" }`,
`[ { "op": "test", "path": "/foo", "value": null } ]`,
true,
"",
},
}
func TestAllTest(t *testing.T) {
for _, c := range TestCases {
_, err := applyPatch(c.doc, c.patch)
if c.result && err != nil {
t.Errorf("Testing failed when it should have passed: %s", err)
} else if !c.result && err == nil {
t.Errorf("Testing passed when it should have faild: %s", err)
} else if !c.result {
expected := fmt.Sprintf("Testing value %s failed", c.failedPath)
if err.Error() != expected {
t.Errorf("Testing failed as expected but invalid message: expected [%s], got [%s]", expected, err)
}
}
}
}