@@ -130,20 +130,23 @@ var diffs = []difftest{
130
130
131
131
func TestDiff (t * testing.T ) {
132
132
for _ , tt := range diffs {
133
- got := Diff (tt .a , tt .b )
134
- eq := len (got ) == len (tt .exp )
135
- if eq {
136
- for i := range got {
137
- eq = eq && got [i ] == tt .exp [i ]
138
- }
139
- }
140
- if ! eq {
141
- t .Errorf ("diffing % #v" , tt .a )
142
- t .Errorf ("with % #v" , tt .b )
143
- diffdiff (t , got , tt .exp )
144
- continue
133
+ expectDiffOutput (t , tt .a , tt .b , tt .exp )
134
+ }
135
+ }
136
+
137
+ func expectDiffOutput (t * testing.T , a , b interface {}, exp []string ) {
138
+ got := Diff (a , b )
139
+ eq := len (got ) == len (exp )
140
+ if eq {
141
+ for i := range got {
142
+ eq = eq && got [i ] == exp [i ]
145
143
}
146
144
}
145
+ if ! eq {
146
+ t .Errorf ("diffing % #v" , a )
147
+ t .Errorf ("with % #v" , b )
148
+ diffdiff (t , got , exp )
149
+ }
147
150
}
148
151
149
152
func TestKeyEqual (t * testing.T ) {
@@ -193,6 +196,47 @@ func TestFdiff(t *testing.T) {
193
196
}
194
197
}
195
198
199
+ func TestDiffCycle (t * testing.T ) {
200
+ // Diff two cyclic structs
201
+ a := & I {i : 1 , R : nil }
202
+ a .R = a
203
+ b := & I {i : 2 , R : nil }
204
+ b .R = b
205
+ expectDiffOutput (t , a , b , []string {
206
+ `i: 1 != 2` ,
207
+ })
208
+
209
+ // Diff two equal cyclic structs
210
+ b .i = 1
211
+ expectDiffOutput (t , a , b , []string {})
212
+
213
+ // Diff two structs with different cycles
214
+ b2 := & I {i : 1 , R : b }
215
+ b .R = b2
216
+ expectDiffOutput (t , a , b , []string {`R: pretty.I{
217
+ i: 1,
218
+ R: &pretty.I{(CYCLIC REFERENCE)},
219
+ } (previously visited) != pretty.I{
220
+ i: 1,
221
+ R: &pretty.I{
222
+ i: 1,
223
+ R: &pretty.I{(CYCLIC REFERENCE)},
224
+ },
225
+ }` })
226
+
227
+ // ... and the same in the other direction
228
+ expectDiffOutput (t , b , a , []string {`R: pretty.I{
229
+ i: 1,
230
+ R: &pretty.I{
231
+ i: 1,
232
+ R: &pretty.I{(CYCLIC REFERENCE)},
233
+ },
234
+ } != pretty.I{
235
+ i: 1,
236
+ R: &pretty.I{(CYCLIC REFERENCE)},
237
+ } (previously visited)` })
238
+ }
239
+
196
240
func diffdiff (t * testing.T , got , exp []string ) {
197
241
minus (t , "unexpected:" , got , exp )
198
242
minus (t , "missing:" , exp , got )
0 commit comments