-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathold_array_test.go
107 lines (91 loc) · 2.85 KB
/
old_array_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
package patchwerk
import (
"encoding/json"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
var (
arrayBase = `{
"persons": [{"name":"Ed"},{}]
}`
arrayUpdated = `{
"persons": [{"name":"Ed"},{},{}]
}`
)
func TestArrayAddMultipleEmptyObjects(t *testing.T) {
patch, e := Diff([]byte(arrayBase), []byte(arrayUpdated))
assert.NoError(t, e)
t.Log("Patch:", patch)
assert.Equal(t, 1, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/persons/2", change.Path, "they should be equal")
assert.Equal(t, map[string]interface{}{}, change.Value, "they should be equal")
}
func TestArrayRemoveMultipleEmptyObjects(t *testing.T) {
patch, e := Diff([]byte(arrayUpdated), []byte(arrayBase))
assert.NoError(t, e)
t.Log("Patch:", patch)
assert.Equal(t, 1, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/persons/2", change.Path, "they should be equal")
assert.Equal(t, nil, change.Value, "they should be equal")
}
var (
arrayWithSpacesBase = `{
"persons": [{"name":"Ed"},{},{},{"name":"Sally"},{}]
}`
arrayWithSpacesUpdated = `{
"persons": [{"name":"Ed"},{},{"name":"Sally"},{}]
}`
)
// TestArrayRemoveSpaceInbetween tests removing one blank item from a group blanks which is in between non blank items which also end with a blank item. This tests that the correct index is removed
func TestArrayRemoveSpaceInbetween(t *testing.T) {
t.Skip("This test fails. TODO change compareArray algorithm to match by index instead of by object equality")
patch, e := Diff([]byte(arrayWithSpacesBase), []byte(arrayWithSpacesUpdated))
assert.NoError(t, e)
t.Log("Patch:", patch)
assert.Equal(t, 1, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/persons/2", change.Path, "they should be equal")
assert.Equal(t, nil, change.Value, "they should be equal")
}
func TestArrayPatchCreate(t *testing.T) {
cases := map[string]struct {
a string
b string
diff string
}{
"add element as last to array": {
`[1, 2, 3]`,
`[1, 2, 3, 4]`,
`[{"op":"add","path":"/3","value":4}]`,
},
"add element as first to array": {
`[1, 2, 3]`,
`[0, 1, 2, 3]`,
`[{"op":"add","path":"/0","value":0}]`,
},
"remove last element from array": {
`[1, 2, 3, 4]`,
`[1, 2, 3]`,
`[{"op":"remove","path":"/3"}]`,
},
}
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))
})
}
}