Skip to content

Commit 6e874f7

Browse files
Make sure time.Time comparison produces a helpful diff. closes #989
1 parent 6990a05 commit 6e874f7

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

assert/assertions.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,12 +1609,17 @@ func diff(expected interface{}, actual interface{}) string {
16091609
}
16101610

16111611
var e, a string
1612-
if et != reflect.TypeOf("") {
1613-
e = spewConfig.Sdump(expected)
1614-
a = spewConfig.Sdump(actual)
1615-
} else {
1612+
1613+
switch et {
1614+
case reflect.TypeOf(""):
16161615
e = reflect.ValueOf(expected).String()
16171616
a = reflect.ValueOf(actual).String()
1617+
case reflect.TypeOf(time.Time{}):
1618+
e = spewConfigStringerEnabled.Sdump(expected)
1619+
a = spewConfigStringerEnabled.Sdump(actual)
1620+
default:
1621+
e = spewConfig.Sdump(expected)
1622+
a = spewConfig.Sdump(actual)
16181623
}
16191624

16201625
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
@@ -1646,6 +1651,14 @@ var spewConfig = spew.ConfigState{
16461651
MaxDepth: 10,
16471652
}
16481653

1654+
var spewConfigStringerEnabled = spew.ConfigState{
1655+
Indent: " ",
1656+
DisablePointerAddresses: true,
1657+
DisableCapacities: true,
1658+
SortKeys: true,
1659+
MaxDepth: 10,
1660+
}
1661+
16491662
type tHelper interface {
16501663
Helper()
16511664
}

assert/assertions_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,23 @@ Diff:
19911991
diffTestingStruct{A: "some string", B: 15},
19921992
)
19931993
Equal(t, expected, actual)
1994+
1995+
expected = `
1996+
1997+
Diff:
1998+
--- Expected
1999+
+++ Actual
2000+
@@ -1,2 +1,2 @@
2001+
-(time.Time) 2020-09-24 00:00:00 +0000 UTC
2002+
+(time.Time) 2020-09-25 00:00:00 +0000 UTC
2003+
2004+
`
2005+
2006+
actual = diff(
2007+
time.Date(2020, 9, 24, 0, 0, 0, 0, time.UTC),
2008+
time.Date(2020, 9, 25, 0, 0, 0, 0, time.UTC),
2009+
)
2010+
Equal(t, expected, actual)
19942011
}
19952012

19962013
func TestTimeEqualityErrorFormatting(t *testing.T) {

0 commit comments

Comments
 (0)