Skip to content

Commit

Permalink
Make sure time.Time comparison produces a helpful diff. closes #989
Browse files Browse the repository at this point in the history
  • Loading branch information
HaraldNordgren committed Apr 24, 2021
1 parent 6990a05 commit 6e874f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
21 changes: 17 additions & 4 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,12 +1609,17 @@ func diff(expected interface{}, actual interface{}) string {
}

var e, a string
if et != reflect.TypeOf("") {
e = spewConfig.Sdump(expected)
a = spewConfig.Sdump(actual)
} else {

switch et {
case reflect.TypeOf(""):
e = reflect.ValueOf(expected).String()
a = reflect.ValueOf(actual).String()
case reflect.TypeOf(time.Time{}):
e = spewConfigStringerEnabled.Sdump(expected)
a = spewConfigStringerEnabled.Sdump(actual)
default:
e = spewConfig.Sdump(expected)
a = spewConfig.Sdump(actual)
}

diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
Expand Down Expand Up @@ -1646,6 +1651,14 @@ var spewConfig = spew.ConfigState{
MaxDepth: 10,
}

var spewConfigStringerEnabled = spew.ConfigState{
Indent: " ",
DisablePointerAddresses: true,
DisableCapacities: true,
SortKeys: true,
MaxDepth: 10,
}

type tHelper interface {
Helper()
}
Expand Down
17 changes: 17 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,23 @@ Diff:
diffTestingStruct{A: "some string", B: 15},
)
Equal(t, expected, actual)

expected = `
Diff:
--- Expected
+++ Actual
@@ -1,2 +1,2 @@
-(time.Time) 2020-09-24 00:00:00 +0000 UTC
+(time.Time) 2020-09-25 00:00:00 +0000 UTC
`

actual = diff(
time.Date(2020, 9, 24, 0, 0, 0, 0, time.UTC),
time.Date(2020, 9, 25, 0, 0, 0, 0, time.UTC),
)
Equal(t, expected, actual)
}

func TestTimeEqualityErrorFormatting(t *testing.T) {
Expand Down

0 comments on commit 6e874f7

Please sign in to comment.