-
Notifications
You must be signed in to change notification settings - Fork 5
/
old_complex_test.go
102 lines (90 loc) · 3.93 KB
/
old_complex_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
package patchwerk
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
var complexBase = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}}`
var complexA = `{"a":100, "b":[{"c1":"goodbye", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}}`
var complexB = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":100, "g":"h", "i":"j"}}`
var complexC = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}, "k":[{"l":"m"}, {"l":"o"}]}`
var complexD = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"}, {"c3":"hello3", "d3":"foo3"} ], "e":{"f":200, "g":"h", "i":"j"}}`
var complexE = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"foo2"} ], "e":{"f":200, "g":"h", "i":"j"}}`
func TestComplexSame(t *testing.T) {
patch, e := Diff([]byte(complexBase), []byte(complexBase))
assert.NoError(t, e)
assert.Equal(t, 0, len(patch), "they should be equal")
}
func TestComplexOneStringReplaceInArray(t *testing.T) {
patch, e := Diff([]byte(complexBase), []byte(complexA))
assert.NoError(t, e)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "/b/0/c1", change.Path, "they should be equal")
assert.Equal(t, "goodbye", change.Value, "they should be equal")
}
func TestComplexOneIntReplace(t *testing.T) {
patch, e := Diff([]byte(complexBase), []byte(complexB))
assert.NoError(t, e)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "/e/f", change.Path, "they should be equal")
var expected float64 = 100
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestComplexOneAdd(t *testing.T) {
patch, e := Diff([]byte(complexBase), []byte(complexC))
assert.NoError(t, e)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/k", change.Path, "they should be equal")
a := make(map[string]interface{})
b := make(map[string]interface{})
a["l"] = "m"
b["l"] = "o"
expected := []interface{}{a, b}
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestComplexOneAddToArray(t *testing.T) {
patch, e := Diff([]byte(complexBase), []byte(complexC))
assert.NoError(t, e)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/k", change.Path, "they should be equal")
a := make(map[string]interface{})
b := make(map[string]interface{})
a["l"] = "m"
b["l"] = "o"
expected := []interface{}{a, b}
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestComplexVsEmpty(t *testing.T) {
patch, e := Diff([]byte(complexBase), []byte(`{}`))
t.Log("base", complexBase)
t.Log("target", `{}`)
for i, p := range patch {
b, _ := json.Marshal(p)
t.Log("patch", i, string(b))
}
assert.NoError(t, e)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "", change.Path, "they should be equal")
// assert.NoError(t, e)
// assert.Equal(t, 3, 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, "/a", change.Path, "they should be equal")
// change = patch[1]
// assert.Equal(t, "remove", change.Operation, "they should be equal")
// assert.Equal(t, "/b", change.Path, "they should be equal")
// change = patch[2]
// assert.Equal(t, "remove", change.Operation, "they should be equal")
// assert.Equal(t, "/e", change.Path, "they should be equal")
}