-
Notifications
You must be signed in to change notification settings - Fork 431
Expand file tree
/
Copy pathdiff_test.go
More file actions
227 lines (209 loc) · 6.02 KB
/
diff_test.go
File metadata and controls
227 lines (209 loc) · 6.02 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package diff
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDiff(t *testing.T) {
type pair struct {
left any
right any
}
tests := []struct {
name string
in pair
expecteds []string
}{
// Plain
{"plain - left nil, right nil",
pair{nil, nil},
nil,
},
{"plain - left nil, right set",
pair{nil, &plainStruct{}},
[]string{"<nil> => &{0} (*diff.plainStruct) (added)"},
},
{"plain - left set, right nil",
pair{&plainStruct{}, nil},
[]string{"&{0} (*diff.plainStruct) => <nil> (removed)"},
},
{"plain - equal",
pair{&plainStruct{}, &plainStruct{}},
nil,
},
{"plain - diff",
pair{&plainStruct{Field: 1}, &plainStruct{Field: 2}},
[]string{"1 (int) => 2 (int) (changed @ Field)"},
},
// Slice
{"slice - equal both nil",
pair{[]string(nil), []string(nil)},
nil,
},
{"slice - equal both length 0",
pair{[]string{}, []string{}},
nil,
},
{"slice - diff both length 1",
pair{[]string{"a"}, []string{"b"}},
[]string{
"a (string) => <nil> (removed @ [0])",
"<nil> => b (string) (added @ [0])",
},
},
{"slice - diff both length 2 re-ordered",
pair{[]string{"a", "b"}, []string{"b", "a"}},
[]string{
"a (string) => <nil> (removed @ [0])",
"<nil> => b (string) (added @ [0])",
"b (string) => <nil> (removed @ [1])",
"<nil> => a (string) (added @ [1])",
},
},
{"slice - diff left is longer than right, all different",
pair{[]string{"a", "b"}, []string{"c"}},
[]string{
"a (string) => <nil> (removed @ [0])",
"<nil> => c (string) (added @ [0])",
"b (string) => <nil> (removed @ [1->?])",
},
},
{"slice - diff left is longer than right, some equals",
pair{[]string{"a", "b"}, []string{"a"}},
[]string{
"b (string) => <nil> (removed @ [1->?])",
},
},
{"slice - diff left is smaller than right, all different",
pair{[]string{"a"}, []string{"b", "c"}},
[]string{
"a (string) => <nil> (removed @ [0])",
"<nil> => b (string) (added @ [0])",
"<nil> => c (string) (added @ [?->1])",
},
},
{"slice - diff left is smaller than right, some equals",
pair{[]string{"a"}, []string{"a", "b"}},
[]string{
"<nil> => b (string) (added @ [?->1])",
},
},
// Full
{"full - everything diff",
pair{
&topStruct{
Literal: "x",
Pointer: &plainStruct{Field: 1},
Array: []string{"a", "b"},
Child: &childStruct{Literal: "1", Pointer: &plainStruct{Field: 10}, Array: []string{"1", "2"}},
},
&topStruct{
Literal: "y",
Pointer: &plainStruct{Field: 2},
Array: []string{"b", "c"},
Child: &childStruct{Literal: "2", Pointer: &plainStruct{Field: 20}, Array: []string{"2"}},
},
},
[]string{
"x (string) => y (string) (changed @ Literal)",
"1 (int) => 2 (int) (changed @ Pointer.Field)",
"a (string) => <nil> (removed @ Array[0])",
"<nil> => b (string) (added @ Array[0])",
"b (string) => <nil> (removed @ Array[1])",
"<nil> => c (string) (added @ Array[1])",
"1 (string) => 2 (string) (changed @ Child.Literal)",
"10 (int) => 20 (int) (changed @ Child.Pointer.Field)",
"1 (string) => <nil> (removed @ Child.Array[0->?])",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actuals := accumulateDiffStrings(test.in.left, test.in.right)
assert.Equal(t, test.expecteds, actuals)
})
}
}
func eventToString(event *Event) string {
path := ""
if len(event.Path) > 1 {
path = " @ " + event.Path.String()
}
return fmt.Sprintf("%s => %s (%s%s)", reflectValueToString(event.Old), reflectValueToString(event.New), event.Kind, path)
}
func TestDiff_EventMatch(t *testing.T) {
tests := []struct {
name string
left any
right any
pattern string
expectedMatch bool
expectedGroups []string
}{
{
"deep array added one",
&topStruct{Child: &childStruct{Array: []string{"1"}}},
&topStruct{Child: &childStruct{Array: []string{"1", "2"}}},
"Child.Array[#]",
true,
[]string{"?->1"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
events := accumulateDiff(test.left, test.right)
require.Len(t, events, 1)
match, groups := events[0].Match(test.pattern)
if test.expectedMatch {
assert.True(t, match, "Expected pattern %q to match diff path %q", test.pattern, events[0].Path)
assert.Equal(t, test.expectedGroups, groups)
} else {
assert.False(t, match, "Expected pattern %q to NOT match diff path %q", test.pattern, events[0].Path)
}
})
}
}
// There is something inherently broken with this, the accumulation seems to broke leading to incorrect
// results. I assume it's a Golang thing related to slice and struct as value versus pointers. It works
// only single event but starts to act weirdly when there > 1, like the Event's Path is all wrong. It's
// better to try to avoid it when possible.
func accumulateDiff(left, right any) (out []Event) {
_ = Diff(left, right, OnEvent(func(event Event) {
out = append(out, event)
}))
return
}
func accumulateDiffStrings(left, right any) (out []string) {
_ = Diff(left, right, OnEvent(func(event Event) {
out = append(out, eventToString(&event))
}))
return
}
type topStruct struct {
Literal string
Pointer *plainStruct
Array []string
Child *childStruct
}
type childStruct struct {
Literal string
Pointer *plainStruct
Array []string
}
type plainStruct struct {
Field int
}