-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_test.go
More file actions
418 lines (368 loc) · 12.8 KB
/
Copy pathpatch_test.go
File metadata and controls
418 lines (368 loc) · 12.8 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package jsonc
import (
"errors"
"strings"
"testing"
)
// ---------------------------------------------------------------------------
// ParsePatch
// ---------------------------------------------------------------------------
func TestParsePatchSimple(t *testing.T) {
src := `[
{"op": "add", "path": "/a", "value": 1},
{"op": "remove", "path": "/b"}
]`
p, err := ParsePatch([]byte(src))
if err != nil {
t.Fatal(err)
}
if len(p) != 2 {
t.Fatalf("got %d ops", len(p))
}
if p[0].Op != "add" || p[0].Path != "/a" || p[0].Value == nil {
t.Errorf("op[0]: %+v", p[0])
}
if p[1].Op != "remove" || p[1].Path != "/b" {
t.Errorf("op[1]: %+v", p[1])
}
}
func TestParsePatchInvalid(t *testing.T) {
_, err := ParsePatch([]byte(`{not array`))
if !errors.Is(err, ErrPatchSyntax) {
t.Errorf("expected ErrPatchSyntax, got %v", err)
}
}
// ---------------------------------------------------------------------------
// add / remove / replace / move / copy / test
// ---------------------------------------------------------------------------
func applyPatchString(t *testing.T, doc, patch string) string {
t.Helper()
root := mustParseRoot(t, doc)
p, err := ParsePatch([]byte(patch))
if err != nil {
t.Fatal(err)
}
out, err := p.Apply(root)
if err != nil {
t.Fatal(err)
}
bytes, err := NodeToBytes(out)
if err != nil {
t.Fatal(err)
}
return string(bytes)
}
func TestPatchAddObjectMember(t *testing.T) {
got := applyPatchString(t,
`{"a": 1}`,
`[{"op": "add", "path": "/b", "value": 2}]`)
if !strings.Contains(got, `"b"`) || !strings.Contains(got, "2") {
t.Errorf("got %s", got)
}
}
func TestPatchAddArrayElement(t *testing.T) {
got := applyPatchString(t,
`{"items": [1, 3]}`,
`[{"op": "add", "path": "/items/1", "value": 2}]`)
if !strings.Contains(got, "1, 2, 3") {
t.Errorf("got %s", got)
}
}
func TestPatchAddArrayAppend(t *testing.T) {
got := applyPatchString(t,
`{"items": [1, 2]}`,
`[{"op": "add", "path": "/items/-", "value": 3}]`)
if !strings.Contains(got, "1, 2, 3") {
t.Errorf("got %s", got)
}
}
func TestPatchAddRoot(t *testing.T) {
got := applyPatchString(t,
`{"a": 1}`,
`[{"op": "add", "path": "", "value": {"b": 2}}]`)
if !strings.Contains(got, `"b"`) || strings.Contains(got, `"a"`) {
t.Errorf("got %s", got)
}
}
func TestPatchRemoveObjectMember(t *testing.T) {
got := applyPatchString(t,
`{"a": 1, "b": 2}`,
`[{"op": "remove", "path": "/a"}]`)
if strings.Contains(got, `"a"`) {
t.Errorf("got %s", got)
}
}
func TestPatchRemoveArrayElement(t *testing.T) {
got := applyPatchString(t,
`{"items": [1, 2, 3]}`,
`[{"op": "remove", "path": "/items/1"}]`)
if !strings.Contains(got, "1, 3") {
t.Errorf("got %s", got)
}
}
func TestPatchReplace(t *testing.T) {
got := applyPatchString(t,
`{"a": 1}`,
`[{"op": "replace", "path": "/a", "value": 99}]`)
if !strings.Contains(got, "99") {
t.Errorf("got %s", got)
}
}
func TestPatchReplaceMissing(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "replace", "path": "/missing", "value": 99}]`))
if _, err := p.Apply(root); err == nil {
t.Fatal("expected error replacing missing key")
}
}
func TestPatchMove(t *testing.T) {
got := applyPatchString(t,
`{"a": 1, "b": 2}`,
`[{"op": "move", "from": "/a", "path": "/c"}]`)
if !strings.Contains(got, `"c"`) || strings.Contains(got, `"a"`) {
t.Errorf("got %s", got)
}
}
func TestPatchMoveIntoDescendantRejected(t *testing.T) {
root := mustParseRoot(t, `{"a": {"b": 1}}`)
p, _ := ParsePatch([]byte(`[{"op": "move", "from": "/a", "path": "/a/b/c"}]`))
if _, err := p.Apply(root); err == nil {
t.Fatal("expected error moving into descendant")
}
}
func TestPatchCopy(t *testing.T) {
got := applyPatchString(t,
`{"a": 1}`,
`[{"op": "copy", "from": "/a", "path": "/b"}]`)
if !strings.Contains(got, `"a"`) || !strings.Contains(got, `"b"`) {
t.Errorf("got %s", got)
}
}
func TestPatchTestSucceeds(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/a", "value": 1}]`))
if _, err := p.Apply(root); err != nil {
t.Errorf("test should pass: %v", err)
}
}
func TestPatchTestFails(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/a", "value": 99}]`))
if _, err := p.Apply(root); !errors.Is(err, ErrPatchSyntax) {
t.Errorf("expected ErrPatchSyntax (test failed), got %v", err)
}
}
func TestPatchUnknownOpRejected(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "frobnicate", "path": "/a"}]`))
if _, err := p.Apply(root); !errors.Is(err, ErrPatchSyntax) {
t.Errorf("expected ErrPatchSyntax, got %v", err)
}
}
func TestPatchSequence(t *testing.T) {
// Series of operations form a single transformation.
got := applyPatchString(t,
`{"a": 1, "b": 2}`,
`[
{"op": "add", "path": "/c", "value": 3},
{"op": "remove", "path": "/a"},
{"op": "replace", "path": "/b", "value": 20}
]`)
if strings.Contains(got, `"a"`) {
t.Errorf("a should be removed: %s", got)
}
if !strings.Contains(got, `"c"`) || !strings.Contains(got, "20") {
t.Errorf("c added and b replaced: %s", got)
}
}
func TestPatchOriginalUnchanged(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "add", "path": "/b", "value": 2}]`))
if _, err := p.Apply(root); err != nil {
t.Fatal(err)
}
// Original should not have been mutated.
if len(root.Children) != 1 {
t.Errorf("Apply should not mutate input, got %d children", len(root.Children))
}
}
// ---------------------------------------------------------------------------
// Patch test op — deep equality across containers and kinds.
// ---------------------------------------------------------------------------
func TestPatchTestDeepEqualityArray(t *testing.T) {
root := mustParseRoot(t, `{"items": [1, 2, 3]}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/items", "value": [1, 2, 3]}]`))
if _, err := p.Apply(root); err != nil {
t.Errorf("array deep-equal test should pass: %v", err)
}
}
func TestPatchTestDeepEqualityArrayMismatchSize(t *testing.T) {
root := mustParseRoot(t, `{"items": [1, 2]}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/items", "value": [1, 2, 3]}]`))
if _, err := p.Apply(root); err == nil {
t.Error("test should fail on array length mismatch")
}
}
func TestPatchTestDeepEqualityObject(t *testing.T) {
root := mustParseRoot(t, `{"user": {"name": "alice", "age": 30}}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/user", "value": {"name": "alice", "age": 30}}]`))
if _, err := p.Apply(root); err != nil {
t.Errorf("object deep-equal test should pass: %v", err)
}
}
func TestPatchTestDeepEqualityObjectMismatchSize(t *testing.T) {
root := mustParseRoot(t, `{"user": {"name": "alice"}}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/user", "value": {"name": "alice", "age": 30}}]`))
if _, err := p.Apply(root); err == nil {
t.Error("test should fail on object size mismatch")
}
}
func TestPatchTestDeepEqualityObjectMismatchValue(t *testing.T) {
root := mustParseRoot(t, `{"user": {"name": "alice"}}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/user", "value": {"name": "bob"}}]`))
if _, err := p.Apply(root); err == nil {
t.Error("test should fail on value mismatch")
}
}
func TestPatchTestDeepEqualityObjectMissingKey(t *testing.T) {
root := mustParseRoot(t, `{"user": {"name": "alice"}}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/user", "value": {"email": "alice@example"}}]`))
if _, err := p.Apply(root); err == nil {
t.Error("test should fail on missing key")
}
}
func TestPatchTestKindMismatch(t *testing.T) {
root := mustParseRoot(t, `{"x": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/x", "value": "1"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("test should fail when kinds differ (number vs string)")
}
}
func TestPatchTestRoot(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "", "value": {"a": 1}}]`))
if _, err := p.Apply(root); err != nil {
t.Errorf("test against root should pass: %v", err)
}
}
// ---------------------------------------------------------------------------
// Patch traversal failure paths.
// ---------------------------------------------------------------------------
func TestPatchAddTraverseScalarRejected(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
// Try to add into /a/b — a is a scalar, not a container.
p, _ := ParsePatch([]byte(`[{"op": "add", "path": "/a/b", "value": 2}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: cannot descend into scalar")
}
}
func TestPatchRemoveOutOfRangeIndex(t *testing.T) {
root := mustParseRoot(t, `{"items": [1, 2]}`)
p, _ := ParsePatch([]byte(`[{"op": "remove", "path": "/items/99"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: index out of range")
}
}
func TestPatchRemoveMissingMember(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "remove", "path": "/missing"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: missing member")
}
}
func TestPatchRemoveRoot(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "remove", "path": ""}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: cannot remove root")
}
}
func TestPatchInvalidPointer(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "add", "path": "no-leading-slash", "value": 1}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: pointer must start with /")
}
}
func TestPatchAddArrayOutOfRange(t *testing.T) {
root := mustParseRoot(t, `{"items": [1, 2]}`)
p, _ := ParsePatch([]byte(`[{"op": "add", "path": "/items/99", "value": 3}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: insert index out of range")
}
}
func TestPatchAddMissingValue(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
// "add" requires a value field.
p, _ := ParsePatch([]byte(`[{"op": "add", "path": "/b"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: add without value")
}
}
// ---------------------------------------------------------------------------
// Move and copy edge cases.
// ---------------------------------------------------------------------------
func TestPatchMoveSamePath(t *testing.T) {
// from == path is a no-op.
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "move", "from": "/a", "path": "/a"}]`))
if _, err := p.Apply(root); err != nil {
t.Errorf("expected no-op, got %v", err)
}
}
func TestPatchMoveNonexistentSource(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "move", "from": "/missing", "path": "/b"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error moving from missing path")
}
}
func TestPatchCopyNonexistentSource(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "copy", "from": "/missing", "path": "/b"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error copying from missing path")
}
}
// ---------------------------------------------------------------------------
// Walking too-deep paths through scalars and missing-target test ops.
// ---------------------------------------------------------------------------
func TestPatchTestThroughScalarRejected(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/a/b", "value": 1}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: cannot descend into scalar")
}
}
func TestPatchCopyTooDeepInScalar(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "copy", "from": "/a/b", "path": "/c"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error from too-deep from-path")
}
}
func TestPatchTestNonexistentPath(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/missing", "value": 1}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error from missing path in test op")
}
}
func TestPatchTestMissingValue(t *testing.T) {
root := mustParseRoot(t, `{"a": 1}`)
// A test op without a value field should be rejected.
p, _ := ParsePatch([]byte(`[{"op": "test", "path": "/a"}]`))
if _, err := p.Apply(root); err == nil {
t.Error("expected error: test without value")
}
}
// ---------------------------------------------------------------------------
// ParsePatch rejects malformed value field.
// ---------------------------------------------------------------------------
func TestParsePatchInvalidValue(t *testing.T) {
src := `[{"op": "add", "path": "/a", "value": @@@}]`
_, err := ParsePatch([]byte(src))
if err == nil {
t.Error("expected ErrPatchSyntax on malformed value")
}
}