Make sure time.Time comparison produces a helpful diff. closes #989

pull/906/merge
Harald Nordgren 2021-04-24 08:28:38 +02:00 committed by Boyan Soubachov
parent 6990a05d54
commit dc5c261377
2 changed files with 34 additions and 4 deletions

View File

@ -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()
}

View File

@ -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) {