feat(assert): use String() for Stringer types in diff output

Values whose type, or slice or array element type, implements
fmt.Stringer are now rendered through their String method in the diff
section of a failed assertion. Previously a named string such as
peer.ID was dumped as raw escaped bytes, which is unreadable.

This affects diff presentation only. Equality is still decided by
ObjectsAreEqual and is unaffected. The default spew config keeps
DisableMethods for every other type, matching the existing time.Time
handling in diff.

Refs #1215
This commit is contained in:
dualfroz committed 2026-09-02 09:43:14 +02:00
1 parent 08fef859fb
commit 9337ead2bc
2 files changed
+50 -2

No files matched your search

+24
View File
@@ -2902,6 +2902,12 @@ func (d *diffTestingStruct) String() string {
return d.A
}
type diffTestingStringer string
func (id diffTestingStringer) String() string {
return "stringer:" + string(id)
}
func TestDiff(t *testing.T) {
t.Parallel()
@@ -3036,6 +3042,24 @@ Diff:
time.Date(2020, 9, 25, 0, 0, 0, 0, time.UTC),
)
Equal(t, expected, actual)
expected = `
Diff:
--- Expected
+++ Actual
@@ -2,3 +2,3 @@
(assert.diffTestingStringer) (len=3) stringer:one,
- (assert.diffTestingStringer) (len=3) stringer:two
+ (assert.diffTestingStringer) (len=5) stringer:three
}
`
actual = diff(
[]diffTestingStringer{"one", "two"},
[]diffTestingStringer{"one", "three"},
)
Equal(t, expected, actual)
}
func TestTimeEqualityErrorFormatting(t *testing.T) {