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.
pull/900/head
Luan Santos 2020-02-20 20:01:50 -08:00 committed by George Lesica
parent 9f1c28b404
commit afd4130c14
2 changed files with 46 additions and 0 deletions

View File

@ -1553,6 +1553,7 @@ var spewConfig = spew.ConfigState{
DisablePointerAddresses: true,
DisableCapacities: true,
SortKeys: true,
DisableMethods: true,
}
type tHelper interface {

View File

@ -1750,6 +1750,15 @@ func TestYAMLEq_ArraysOfDifferentOrder(t *testing.T) {
False(t, YAMLEq(mockT, `["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`))
}
type diffTestingStruct struct {
A string
B int
}
func (d *diffTestingStruct) String() string {
return d.A
}
func TestDiff(t *testing.T) {
expected := `
@ -1829,6 +1838,42 @@ Diff:
map[string]int{"one": 1, "three": 3, "five": 5, "seven": 7},
)
Equal(t, expected, actual)
expected = `
Diff:
--- Expected
+++ Actual
@@ -1,3 +1,3 @@
(*errors.errorString)({
- s: (string) (len=19) "some expected error"
+ s: (string) (len=12) "actual error"
})
`
actual = diff(
errors.New("some expected error"),
errors.New("actual error"),
)
Equal(t, expected, actual)
expected = `
Diff:
--- Expected
+++ Actual
@@ -2,3 +2,3 @@
A: (string) (len=11) "some string",
- B: (int) 10
+ B: (int) 15
}
`
actual = diff(
diffTestingStruct{A: "some string", B: 10},
diffTestingStruct{A: "some string", B: 15},
)
Equal(t, expected, actual)
}
func TestTimeEqualityErrorFormatting(t *testing.T) {