From b2aad2c9a95d14eb978f29baa6e3a5c3c20eef30 Mon Sep 17 00:00:00 2001 From: motomux Date: Fri, 9 Dec 2016 12:52:51 -0800 Subject: [PATCH] Diff nil and empty for slice and map --- diff.go | 8 ++++++++ diff_test.go | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/diff.go b/diff.go index 6aa7f74..125e7eb 100644 --- a/diff.go +++ b/diff.go @@ -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(): @@ -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) diff --git a/diff_test.go b/diff_test.go index a951e4b..7e14798 100644 --- a/diff_test.go +++ b/diff_test.go @@ -40,6 +40,8 @@ var ( f1 = func() {} i0 = 0 i1 = 1 + s0 []int + m0 map[int]int ) var diffs = []difftest{ @@ -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) {