Skip to content

Commit 10746a3

Browse files
committed
Lazy
1 parent 3cd153a commit 10746a3

File tree

8 files changed

+286
-213
lines changed

8 files changed

+286
-213
lines changed

License

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
The MIT License (MIT)
2+
13
Copyright 2012 Keith Rarick
24

35
Permission is hereby granted, free of charge, to any person obtaining a copy

diff.go

+1-31
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ type Printfer interface {
4141
// It calls Printf once for each difference, with no trailing newline.
4242
// The standard library log.Logger is a Printfer.
4343
func Pdiff(p Printfer, a, b interface{}) {
44-
d := diffPrinter{
45-
w: p,
46-
aVisited: make(map[visit]visit),
47-
bVisited: make(map[visit]visit),
48-
}
49-
d.diff(reflect.ValueOf(a), reflect.ValueOf(b))
44+
diffPrinter{w: p}.diff(reflect.ValueOf(a), reflect.ValueOf(b))
5045
}
5146

5247
type Logfer interface {
@@ -71,9 +66,6 @@ func Ldiff(l Logfer, a, b interface{}) {
7166
type diffPrinter struct {
7267
w Printfer
7368
l string // label
74-
75-
aVisited map[visit]visit
76-
bVisited map[visit]visit
7769
}
7870

7971
func (w diffPrinter) printf(f string, a ...interface{}) {
@@ -104,28 +96,6 @@ func (w diffPrinter) diff(av, bv reflect.Value) {
10496
return
10597
}
10698

107-
if av.CanAddr() && bv.CanAddr() {
108-
avis := visit{av.UnsafeAddr(), at}
109-
bvis := visit{bv.UnsafeAddr(), bt}
110-
var cycle bool
111-
112-
// Have we seen this value before?
113-
if vis, ok := w.aVisited[avis]; ok {
114-
cycle = true
115-
if vis != bvis {
116-
w.printf("%# v (previously visited) != %# v", formatter{v: av, quote: true}, formatter{v: bv, quote: true})
117-
}
118-
} else if _, ok := w.bVisited[bvis]; ok {
119-
cycle = true
120-
w.printf("%# v != %# v (previously visited)", formatter{v: av, quote: true}, formatter{v: bv, quote: true})
121-
}
122-
w.aVisited[avis] = bvis
123-
w.bVisited[bvis] = avis
124-
if cycle {
125-
return
126-
}
127-
}
128-
12999
switch kind := at.Kind(); kind {
130100
case reflect.Bool:
131101
if a, b := av.Bool(), bv.Bool(); a != b {

diff_test.go

+12-56
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,19 @@ var diffs = []difftest{
130130

131131
func TestDiff(t *testing.T) {
132132
for _, tt := range diffs {
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]
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
143145
}
144-
}
145-
if !eq {
146-
t.Errorf("diffing % #v", a)
147-
t.Errorf("with % #v", b)
148-
diffdiff(t, got, exp)
149146
}
150147
}
151148

@@ -196,47 +193,6 @@ func TestFdiff(t *testing.T) {
196193
}
197194
}
198195

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-
240196
func diffdiff(t *testing.T, got, exp []string) {
241197
minus(t, "unexpected:", got, exp)
242198
minus(t, "missing:", exp, got)

0 commit comments

Comments
 (0)