-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatch_test.go
171 lines (160 loc) · 4.1 KB
/
patch_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
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
package deepdiff
import (
"encoding/json"
"reflect"
"testing"
)
type PatchTestCase struct {
description string
tree, expect interface{}
patch Deltas
}
func TestPatch(t *testing.T) {
cases := []PatchTestCase{
{
"update bool",
[]interface{}{true},
[]interface{}{false},
Deltas{{Type: DTUpdate, Path: IndexAddr(0), Value: false}},
},
{
"update number",
[]interface{}{float64(1)},
[]interface{}{float64(2)},
Deltas{{Type: DTUpdate, Path: IndexAddr(0), Value: float64(2)}},
},
{
"update nested number",
map[string]interface{}{"a": []interface{}{float64(1)}},
map[string]interface{}{"a": []interface{}{float64(2)}},
Deltas{{Type: DTContext, Path: StringAddr("a"), Deltas: Deltas{
{Type: DTUpdate, Path: IndexAddr(0), Value: float64(2)}},
}},
},
{
"update string",
[]interface{}{"before"},
[]interface{}{"after"},
Deltas{{Type: DTUpdate, Path: IndexAddr(0), Value: "after"}},
},
{
"insert number to end of array",
[]interface{}{},
[]interface{}{float64(1)},
Deltas{{Type: DTInsert, Path: IndexAddr(0), Value: float64(1)}},
},
{
"insert number in slice",
[]interface{}{float64(0), float64(2)},
[]interface{}{float64(0), float64(1), float64(2)},
Deltas{
{Type: DTContext, Path: IndexAddr(0), Value: float64(0)},
{Type: DTInsert, Path: IndexAddr(1), Value: float64(1)},
},
},
{
"insert false into object",
map[string]interface{}{},
map[string]interface{}{"a": false},
Deltas{{Type: DTInsert, Path: StringAddr("a"), Value: false}},
},
{
"delete from end of array",
[]interface{}{"a", "b", "c"},
[]interface{}{"a", "b"},
Deltas{
{Type: DTContext, Path: IndexAddr(0), Value: "a"},
{Type: DTContext, Path: IndexAddr(1), Value: "b"},
{Type: DTDelete, Path: IndexAddr(2), Value: "c"},
},
},
{
"delete from array",
[]interface{}{"a", "b", "c"},
[]interface{}{"a", "c"},
Deltas{
{Type: DTContext, Path: IndexAddr(0), Value: "a"},
{Type: DTDelete, Path: IndexAddr(1), Value: "b"},
{Type: DTContext, Path: IndexAddr(1), Value: "c"},
},
},
{
"delete from object",
map[string]interface{}{"a": false},
map[string]interface{}{},
Deltas{
{Type: DTDelete, Path: StringAddr("a")},
},
},
{
"delete from nested object",
map[string]interface{}{
"a": []interface{}{
map[string]interface{}{
"b": false,
},
},
},
map[string]interface{}{
"a": []interface{}{
map[string]interface{}{},
},
},
Deltas{
{Type: DTContext, Path: StringAddr("a"), Deltas: Deltas{
{Type: DTContext, Path: IndexAddr(0), Deltas: Deltas{
{Type: DTDelete, Path: StringAddr("b")},
}},
}},
},
},
{
"insert, update, then delete",
map[string]interface{}{"a": true, "b": float64(2)},
map[string]interface{}{"a": false, "c": float64(3)},
Deltas{
{Type: DTInsert, Path: StringAddr("c"), Value: float64(3)},
{Type: DTUpdate, Path: StringAddr("a"), Value: false},
{Type: DTDelete, Path: StringAddr("b"), Value: false},
},
},
{
"remove scalar from array in object",
map[string]interface{}{"a": []interface{}{false, "yep"}, "b": true},
map[string]interface{}{"a": []interface{}{"yep"}, "b": true},
Deltas{
{Type: DTContext, Path: StringAddr("a"), Deltas: Deltas{
{Type: DTDelete, Path: IndexAddr(0), Value: false},
}},
},
},
}
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
if err := Patch(c.patch, &c.tree); err != nil {
t.Fatalf("patch error: %s", err)
}
if !reflect.DeepEqual(c.tree, c.expect) {
t.Errorf("result mismatch")
if data, err := json.Marshal(c.tree); err == nil {
t.Log("got :", string(data))
}
if data, err := json.Marshal(c.expect); err == nil {
t.Log("expect:", string(data))
}
}
})
}
}
type PatchErrorTestCase struct {
description string
tree interface{}
dlt *Delta
err error
}
func RunPatchErrorTestCases(t *testing.T, cases []PatchErrorTestCase) {
}
func TestPatchErrors(t *testing.T) {
errCases := []PatchErrorTestCase{}
RunPatchErrorTestCases(t, errCases)
}