Fix bug with nil fmt.Stringers (#30)
diff --git a/cmp/compare_test.go b/cmp/compare_test.go
index fbe3d30..690bd32 100644
--- a/cmp/compare_test.go
+++ b/cmp/compare_test.go
@@ -79,6 +79,7 @@
tests = append(tests, transformerTests()...)
tests = append(tests, embeddedTests()...)
tests = append(tests, methodTests()...)
+ tests = append(tests, formatTests()...)
tests = append(tests, project1Tests()...)
tests = append(tests, project2Tests()...)
tests = append(tests, project3Tests()...)
@@ -1361,6 +1362,21 @@
}}
}
+func formatTests() []test {
+ const label = "Format/"
+ return []test{
+ {
+ label: label + "NilStringer",
+ x: new(fmt.Stringer),
+ y: nil,
+ wantDiff: `
+:
+ -: &<nil>
+ +: <non-existent>`,
+ },
+ }
+}
+
func project1Tests() []test {
const label = "Project1"
diff --git a/cmp/internal/value/format.go b/cmp/internal/value/format.go
index da9d6cf..a501e1b 100644
--- a/cmp/internal/value/format.go
+++ b/cmp/internal/value/format.go
@@ -44,7 +44,7 @@
return "<non-existent>"
}
if conf.useStringer && v.Type().Implements(stringerIface) {
- if v.Kind() == reflect.Ptr && v.IsNil() {
+ if (v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface) && v.IsNil() {
return "<nil>"
}
return fmt.Sprintf("%q", v.Interface().(fmt.Stringer).String())