Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff nil and empty for slice and map #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func (w diffPrinter) diff(av, bv reflect.Value) {
w := w.relabel(fmt.Sprintf("[%#v]", k))
w.printf("(missing) != %q", bv.MapIndex(k))
}
if av.IsNil() != bv.IsNil() {
w.printf("%#v != %#v", av, bv)
break
}
case reflect.Ptr:
switch {
case av.IsNil() && !bv.IsNil():
Expand All @@ -161,6 +165,10 @@ func (w diffPrinter) diff(av, bv reflect.Value) {
for i := 0; i < lenA; i++ {
w.relabel(fmt.Sprintf("[%d]", i)).diff(av.Index(i), bv.Index(i))
}
if av.IsNil() != bv.IsNil() {
w.printf("%#v != %#v", av, bv)
break
}
case reflect.String:
if a, b := av.String(), bv.String(); a != b {
w.printf("%q != %q", a, b)
Expand Down
4 changes: 4 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var (
f1 = func() {}
i0 = 0
i1 = 1
s0 []int
m0 map[int]int
)

var diffs = []difftest{
Expand Down Expand Up @@ -126,6 +128,8 @@ var diffs = []difftest{
struct{ x unsafe.Pointer }{unsafe.Pointer(uintptr(1))},
[]string{`x: 0x0 != 0x1`},
},
{[]int{}, s0, []string{"[]int{} != []int(nil)"}},
{map[int]int{}, m0, []string{"map[int]int{} != map[int]int(nil)"}},
}

func TestDiff(t *testing.T) {
Expand Down