Repository: stretchr/testify (go)

Issue:
time.Time comparison produces non-helpful diff
https://github.com/stretchr/testify/pull/895 broke diffs with time.Time:

In 1.5.1:
``` diff
-    Created: (xxx.Time) 2020-01-14T06:23:58Z,
-    Modified: (xxx.Time) 2020-01-14T06:23:58Z,
+    Created: (xxx.Time) 2018-08-05T09:30:53Z,
+    Modified: (xxx.Time) 2019-04-11T04:28:50Z,
```

In 1.6.1:
```diff
@@ -13,3 +13,3 @@
        wall: (uint64) 0,
-      ext: (int64) 63714579838,
+      ext: (int64) 63669058253,
        loc: (*time.Location)(<nil>)
@@ -20,3 +20,3 @@
        wall: (uint64) 0,
-      ext: (int64) 63714579838,
+      ext: (int64) 63690553730,
        loc: (*time.Location)(<nil>)
```


Pull request description:
Make sure time.Time comparison produces a helpful diff. closes #989
Make sure time.Time comparison produces a helpful diff. closes #989


## Summary
Make sure time.Time comparison produces a helpful diff
again. This partially reverses the changes in #895 for
time.Time and allows it to be easily expanded for more
types in the future if needed.

## Changes
Analyzes the type when diffing, and uses a different
Spew config for time.Time

Introduce a test for diffing time.Time

## Motivation
See bug #989

## Related issues
Closes #989 

Patch:
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()
 }


Test patch:
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) {