-
Notifications
You must be signed in to change notification settings - Fork 5
/
old_root_test.go
55 lines (49 loc) · 1.16 KB
/
old_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
package patchwerk
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJSONPatchCreate(t *testing.T) {
cases := map[string]struct {
a string
b string
diff string
}{
"object": {
`{"asdf":"qwerty"}`,
`{"asdf":"zzz"}`,
`[{"op":"replace","path":"/asdf","value":"zzz"}]`,
},
"object with array": {
`{"items":[{"asdf":"qwerty"}]}`,
`{"items":[{"asdf":"bla"},{"asdf":"zzz"}]}`,
`[{"op":"replace","path":"/items","value":[{"asdf":"bla"},{"asdf":"zzz"}]}]`,
},
"from empty array": {
`[]`,
`[{"asdf":"bla"}]`,
`[{"op":"add","path":"/0","value":{"asdf":"bla"}}]`,
},
"to empty array": {
`[{"asdf":"bla"}]`,
`[]`,
`[{"op":"remove","path":"/0"}]`,
},
"from object to array": {
`{"foo":"bar"}`,
`[{"foo":"bar"}]`,
`[{"op":"replace","path":"","value":[{"foo":"bar"}]}]`,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
t.Logf(`Running test: "%s"`, name)
patch, err := Diff([]byte(tc.a), []byte(tc.b))
assert.NoError(t, err)
patchBytes, err := json.Marshal(patch)
assert.NoError(t, err)
assert.Equal(t, tc.diff, string(patchBytes))
})
}
}