-
Notifications
You must be signed in to change notification settings - Fork 156
/
diff_test.go
257 lines (236 loc) · 7.68 KB
/
diff_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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package pretty
import (
"bytes"
"fmt"
"log"
"reflect"
"testing"
"unsafe"
)
var (
_ Logfer = (*testing.T)(nil)
_ Logfer = (*testing.B)(nil)
_ Printfer = (*log.Logger)(nil)
)
type difftest struct {
a interface{}
b interface{}
exp []string
}
type S struct {
A int
S *S
I interface{}
C []int
}
type (
N struct{ N int }
E interface{}
)
var (
c0 = make(chan int)
c1 = make(chan int)
f0 = func() {}
f1 = func() {}
i0 = 0
i1 = 1
)
var diffs = []difftest{
{a: nil, b: nil},
{a: S{A: 1}, b: S{A: 1}},
{0, "", []string{`int != string`}},
{0, 1, []string{`0 != 1`}},
{S{}, new(S), []string{`pretty.S != *pretty.S`}},
{"a", "b", []string{`"a" != "b"`}},
{S{}, S{A: 1}, []string{`A: 0 != 1`}},
{new(S), &S{A: 1}, []string{`A: 0 != 1`}},
{S{S: new(S)}, S{S: &S{A: 1}}, []string{`S.A: 0 != 1`}},
{S{}, S{I: 0}, []string{`I: nil != int(0)`}},
{S{I: 1}, S{I: "x"}, []string{`I: int != string`}},
{S{}, S{C: []int{1}}, []string{`C: []int[0] != []int[1]`}},
{S{C: []int{}}, S{C: []int{1}}, []string{`C: []int[0] != []int[1]`}},
{S{C: []int{1, 2, 3}}, S{C: []int{1, 2, 4}}, []string{`C[2]: 3 != 4`}},
{S{}, S{A: 1, S: new(S)}, []string{`A: 0 != 1`, `S: nil != &pretty.S{}`}},
// unexported fields of every reflect.Kind (both equal and unequal)
{struct{ x bool }{false}, struct{ x bool }{false}, nil},
{struct{ x bool }{false}, struct{ x bool }{true}, []string{`x: false != true`}},
{struct{ x int }{0}, struct{ x int }{0}, nil},
{struct{ x int }{0}, struct{ x int }{1}, []string{`x: 0 != 1`}},
{struct{ x int8 }{0}, struct{ x int8 }{0}, nil},
{struct{ x int8 }{0}, struct{ x int8 }{1}, []string{`x: 0 != 1`}},
{struct{ x int16 }{0}, struct{ x int16 }{0}, nil},
{struct{ x int16 }{0}, struct{ x int16 }{1}, []string{`x: 0 != 1`}},
{struct{ x int32 }{0}, struct{ x int32 }{0}, nil},
{struct{ x int32 }{0}, struct{ x int32 }{1}, []string{`x: 0 != 1`}},
{struct{ x int64 }{0}, struct{ x int64 }{0}, nil},
{struct{ x int64 }{0}, struct{ x int64 }{1}, []string{`x: 0 != 1`}},
{struct{ x uint }{0}, struct{ x uint }{0}, nil},
{struct{ x uint }{0}, struct{ x uint }{1}, []string{`x: 0 != 1`}},
{struct{ x uint8 }{0}, struct{ x uint8 }{0}, nil},
{struct{ x uint8 }{0}, struct{ x uint8 }{1}, []string{`x: 0 != 1`}},
{struct{ x uint16 }{0}, struct{ x uint16 }{0}, nil},
{struct{ x uint16 }{0}, struct{ x uint16 }{1}, []string{`x: 0 != 1`}},
{struct{ x uint32 }{0}, struct{ x uint32 }{0}, nil},
{struct{ x uint32 }{0}, struct{ x uint32 }{1}, []string{`x: 0 != 1`}},
{struct{ x uint64 }{0}, struct{ x uint64 }{0}, nil},
{struct{ x uint64 }{0}, struct{ x uint64 }{1}, []string{`x: 0 != 1`}},
{struct{ x uintptr }{0}, struct{ x uintptr }{0}, nil},
{struct{ x uintptr }{0}, struct{ x uintptr }{1}, []string{`x: 0 != 1`}},
{struct{ x float32 }{0}, struct{ x float32 }{0}, nil},
{struct{ x float32 }{0}, struct{ x float32 }{1}, []string{`x: 0 != 1`}},
{struct{ x float64 }{0}, struct{ x float64 }{0}, nil},
{struct{ x float64 }{0}, struct{ x float64 }{1}, []string{`x: 0 != 1`}},
{struct{ x complex64 }{0}, struct{ x complex64 }{0}, nil},
{struct{ x complex64 }{0}, struct{ x complex64 }{1}, []string{`x: (0+0i) != (1+0i)`}},
{struct{ x complex128 }{0}, struct{ x complex128 }{0}, nil},
{struct{ x complex128 }{0}, struct{ x complex128 }{1}, []string{`x: (0+0i) != (1+0i)`}},
{struct{ x [1]int }{[1]int{0}}, struct{ x [1]int }{[1]int{0}}, nil},
{struct{ x [1]int }{[1]int{0}}, struct{ x [1]int }{[1]int{1}}, []string{`x[0]: 0 != 1`}},
{struct{ x chan int }{c0}, struct{ x chan int }{c0}, nil},
{struct{ x chan int }{c0}, struct{ x chan int }{c1}, []string{fmt.Sprintf("x: %p != %p", c0, c1)}},
{struct{ x func() }{f0}, struct{ x func() }{f0}, nil},
{struct{ x func() }{f0}, struct{ x func() }{f1}, []string{fmt.Sprintf("x: %p != %p", f0, f1)}},
{struct{ x interface{} }{0}, struct{ x interface{} }{0}, nil},
{struct{ x interface{} }{0}, struct{ x interface{} }{1}, []string{`x: 0 != 1`}},
{struct{ x interface{} }{0}, struct{ x interface{} }{""}, []string{`x: int != string`}},
{struct{ x interface{} }{0}, struct{ x interface{} }{nil}, []string{`x: int(0) != nil`}},
{struct{ x interface{} }{nil}, struct{ x interface{} }{0}, []string{`x: nil != int(0)`}},
{struct{ x map[int]int }{map[int]int{0: 0}}, struct{ x map[int]int }{map[int]int{0: 0}}, nil},
{struct{ x map[int]int }{map[int]int{0: 0}}, struct{ x map[int]int }{map[int]int{0: 1}}, []string{`x[0]: 0 != 1`}},
{struct{ x *int }{new(int)}, struct{ x *int }{new(int)}, nil},
{struct{ x *int }{&i0}, struct{ x *int }{&i1}, []string{`x: 0 != 1`}},
{struct{ x *int }{nil}, struct{ x *int }{&i0}, []string{`x: nil != &int(0)`}},
{struct{ x *int }{&i0}, struct{ x *int }{nil}, []string{`x: &int(0) != nil`}},
{struct{ x []int }{[]int{0}}, struct{ x []int }{[]int{0}}, nil},
{struct{ x []int }{[]int{0}}, struct{ x []int }{[]int{1}}, []string{`x[0]: 0 != 1`}},
{struct{ x string }{"a"}, struct{ x string }{"a"}, nil},
{struct{ x string }{"a"}, struct{ x string }{"b"}, []string{`x: "a" != "b"`}},
{struct{ x N }{N{0}}, struct{ x N }{N{0}}, nil},
{struct{ x N }{N{0}}, struct{ x N }{N{1}}, []string{`x.N: 0 != 1`}},
{
struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(0))},
struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(0))},
nil,
},
{
struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(0))},
struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(1))},
[]string{`x: 0x0 != 0x1`},
},
}
func TestDiff(t *testing.T) {
for _, tt := range diffs {
expectDiffOutput(t, tt.a, tt.b, tt.exp)
}
}
func expectDiffOutput(t *testing.T, a, b interface{}, exp []string) {
got := Diff(a, b)
eq := len(got) == len(exp)
if eq {
for i := range got {
eq = eq && got[i] == exp[i]
}
}
if !eq {
t.Errorf("diffing % #v", a)
t.Errorf("with % #v", b)
diffdiff(t, got, exp)
}
}
func TestKeyEqual(t *testing.T) {
var emptyInterfaceZero interface{} = 0
cases := []interface{}{
new(bool),
new(int),
new(int8),
new(int16),
new(int32),
new(int64),
new(uint),
new(uint8),
new(uint16),
new(uint32),
new(uint64),
new(uintptr),
new(float32),
new(float64),
new(complex64),
new(complex128),
new([1]int),
new(chan int),
new(unsafe.Pointer),
new(interface{}),
&emptyInterfaceZero,
new(*int),
new(string),
new(struct{ int }),
}
for _, test := range cases {
rv := reflect.ValueOf(test).Elem()
if !keyEqual(rv, rv) {
t.Errorf("keyEqual(%s, %s) = false want true", rv.Type(), rv.Type())
}
}
}
func TestFdiff(t *testing.T) {
var buf bytes.Buffer
Fdiff(&buf, 0, 1)
want := "0 != 1\n"
if got := buf.String(); got != want {
t.Errorf("Fdiff(0, 1) = %q want %q", got, want)
}
}
func TestDiffCycle(t *testing.T) {
// Diff two cyclic structs
a := &I{i: 1, R: nil}
a.R = a
b := &I{i: 2, R: nil}
b.R = b
expectDiffOutput(t, a, b, []string{
`i: 1 != 2`,
})
// Diff two equal cyclic structs
b.i = 1
expectDiffOutput(t, a, b, []string{})
// Diff two structs with different cycles
b2 := &I{i: 1, R: b}
b.R = b2
expectDiffOutput(t, a, b, []string{`R: pretty.I{
i: 1,
R: &pretty.I{(CYCLIC REFERENCE)},
} (previously visited) != pretty.I{
i: 1,
R: &pretty.I{
i: 1,
R: &pretty.I{(CYCLIC REFERENCE)},
},
}`})
// ... and the same in the other direction
expectDiffOutput(t, b, a, []string{`R: pretty.I{
i: 1,
R: &pretty.I{
i: 1,
R: &pretty.I{(CYCLIC REFERENCE)},
},
} != pretty.I{
i: 1,
R: &pretty.I{(CYCLIC REFERENCE)},
} (previously visited)`})
}
func diffdiff(t *testing.T, got, exp []string) {
minus(t, "unexpected:", got, exp)
minus(t, "missing:", exp, got)
}
func minus(t *testing.T, s string, a, b []string) {
var i, j int
for i = 0; i < len(a); i++ {
for j = 0; j < len(b); j++ {
if a[i] == b[j] {
break
}
}
if j == len(b) {
t.Error(s, a[i])
}
}
}