From dc5c261377d971ad796211563f0e3e73954af5e6 Mon Sep 17 00:00:00 2001 From: Harald Nordgren Date: Sat, 24 Apr 2021 08:28:38 +0200 Subject: [PATCH] Make sure time.Time comparison produces a helpful diff. closes #989 --- assert/assertions.go | 21 +++++++++++++++++---- assert/assertions_test.go | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/assert/assertions.go b/assert/assertions.go index 00d97a5..6882692 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -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{ @@ -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() } diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 3bd418d..1350567 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -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) {