-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathjsonpatch_array_at_root_test.go
59 lines (51 loc) · 1.13 KB
/
jsonpatch_array_at_root_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
package jsonpatch
import (
"encoding/json"
"fmt"
"github.com/davecgh/go-spew/spew"
jsonpatch "github.com/evanphx/json-patch"
"github.com/stretchr/testify/assert"
"testing"
)
func TestJSONPatchCreate(t *testing.T) {
cases := map[string]struct {
a string
b string
}{
"object": {
`{"asdf":"qwerty"}`,
`{"asdf":"zzz"}`,
},
"object with array": {
`{"items":[{"asdf":"qwerty"}]}`,
`{"items":[{"asdf":"bla"},{"asdf":"zzz"}]}`,
},
"array": {
`[{"asdf":"qwerty"}]`,
`[{"asdf":"bla"},{"asdf":"zzz"}]`,
},
"from empty array": {
`[]`,
`[{"asdf":"bla"},{"asdf":"zzz"}]`,
},
"to empty array": {
`[{"asdf":"bla"},{"asdf":"zzz"}]`,
`[]`,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
patch, err := CreatePatch([]byte(tc.a), []byte(tc.b))
assert.NoError(t, err)
patchBytes, err := json.Marshal(patch)
assert.NoError(t, err)
fmt.Printf("%s\n", string(patchBytes))
p, err := jsonpatch.DecodePatch(patchBytes)
assert.NoError(t, err)
res, err := p.Apply([]byte(tc.a))
assert.NoError(t, err)
spew.Dump(res)
assert.Equal(t, tc.b, string(res))
})
}
}