Skip to content

Commit afd4130

Browse files
luanglesica
authored andcommitted
Disable using Stringer/error interfaces for diffing
`spew`'s default formatter will call `String()` or `Error()` in structs that implement the `fmt.Stringer` or `error` interfaces. Depending on the implementation of those, the diff can become quite useless to read (see the example struct I used for the test case in this commit). This changes `spew`'s configuration to `DisableMethods` so that it will always use it's own pretty printer. This makes testing structs less surprising and generally more useful, without tying the tests to the implementation of `String()` (the user here can always chose to `require.Equal(a.String(), b.String())` if testing those is important to them.
1 parent 9f1c28b commit afd4130

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

assert/assertions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,7 @@ var spewConfig = spew.ConfigState{
15531553
DisablePointerAddresses: true,
15541554
DisableCapacities: true,
15551555
SortKeys: true,
1556+
DisableMethods: true,
15561557
}
15571558

15581559
type tHelper interface {

assert/assertions_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,15 @@ func TestYAMLEq_ArraysOfDifferentOrder(t *testing.T) {
17501750
False(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`))
17511751
}
17521752

1753+
type diffTestingStruct struct {
1754+
A string
1755+
B int
1756+
}
1757+
1758+
func (d *diffTestingStruct) String() string {
1759+
return d.A
1760+
}
1761+
17531762
func TestDiff(t *testing.T) {
17541763
expected := `
17551764
@@ -1829,6 +1838,42 @@ Diff:
18291838
map[string]int{"one": 1, "three": 3, "five": 5, "seven": 7},
18301839
)
18311840
Equal(t, expected, actual)
1841+
1842+
expected = `
1843+
1844+
Diff:
1845+
--- Expected
1846+
+++ Actual
1847+
@@ -1,3 +1,3 @@
1848+
(*errors.errorString)({
1849+
- s: (string) (len=19) "some expected error"
1850+
+ s: (string) (len=12) "actual error"
1851+
})
1852+
`
1853+
1854+
actual = diff(
1855+
errors.New("some expected error"),
1856+
errors.New("actual error"),
1857+
)
1858+
Equal(t, expected, actual)
1859+
1860+
expected = `
1861+
1862+
Diff:
1863+
--- Expected
1864+
+++ Actual
1865+
@@ -2,3 +2,3 @@
1866+
A: (string) (len=11) "some string",
1867+
- B: (int) 10
1868+
+ B: (int) 15
1869+
}
1870+
`
1871+
1872+
actual = diff(
1873+
diffTestingStruct{A: "some string", B: 10},
1874+
diffTestingStruct{A: "some string", B: 15},
1875+
)
1876+
Equal(t, expected, actual)
18321877
}
18331878

18341879
func TestTimeEqualityErrorFormatting(t *testing.T) {

0 commit comments

Comments
 (0)