Skip to content

Commit f94e52c

Browse files
mattdee123dsnet
authored andcommitted
Fix bug with nil fmt.Stringers (#30)
1 parent bf72641 commit f94e52c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cmp/compare_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func TestDiff(t *testing.T) {
7979
tests = append(tests, transformerTests()...)
8080
tests = append(tests, embeddedTests()...)
8181
tests = append(tests, methodTests()...)
82+
tests = append(tests, formatTests()...)
8283
tests = append(tests, project1Tests()...)
8384
tests = append(tests, project2Tests()...)
8485
tests = append(tests, project3Tests()...)
@@ -1361,6 +1362,21 @@ func methodTests() []test {
13611362
}}
13621363
}
13631364

1365+
func formatTests() []test {
1366+
const label = "Format/"
1367+
return []test{
1368+
{
1369+
label: label + "NilStringer",
1370+
x: new(fmt.Stringer),
1371+
y: nil,
1372+
wantDiff: `
1373+
:
1374+
-: &<nil>
1375+
+: <non-existent>`,
1376+
},
1377+
}
1378+
}
1379+
13641380
func project1Tests() []test {
13651381
const label = "Project1"
13661382

cmp/internal/value/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func formatAny(v reflect.Value, conf formatConfig, visited map[uintptr]bool) str
4444
return "<non-existent>"
4545
}
4646
if conf.useStringer && v.Type().Implements(stringerIface) {
47-
if v.Kind() == reflect.Ptr && v.IsNil() {
47+
if (v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface) && v.IsNil() {
4848
return "<nil>"
4949
}
5050
return fmt.Sprintf("%q", v.Interface().(fmt.Stringer).String())

0 commit comments

Comments
 (0)