Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1951,9 +1951,6 @@ func diff(expected interface{}, actual interface{}) string {
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)
Expand Down Expand Up @@ -1985,14 +1982,7 @@ var spewConfig = spew.ConfigState{
DisableCapacities: true,
SortKeys: true,
DisableMethods: true,
MaxDepth: 10,
}

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

Expand Down
24 changes: 24 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3036,6 +3036,30 @@ Diff:
time.Date(2020, 9, 25, 0, 0, 0, 0, time.UTC),
)
Equal(t, expected, actual)

expected = `

Diff:
--- Expected
+++ Actual
@@ -1,3 +1,3 @@
(assert.someStruct) {
- t: (time.Time) 2020-09-24 00:00:00 +0000 UTC
+ t: (time.Time) 2020-09-25 00:00:00 +0000 UTC
}
`

type someStruct struct {
t time.Time
}

actual = diff(
someStruct{t: time.Date(2020, 9, 24, 0, 0, 0, 0, time.UTC)},
someStruct{t: time.Date(2020, 9, 25, 0, 0, 0, 0, time.UTC)},
)

Equal(t, expected, actual)

}

func TestTimeEqualityErrorFormatting(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions internal/spew/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ func canSortSimply(kind reflect.Kind) bool {
case reflect.Array:
return true
}

if kind == reflect.Struct && kind.String() == "time.Time" {
// time.Time values can be sorted directly, as it implements [sort.Interface].
return true
}

return false
}

Expand Down
6 changes: 6 additions & 0 deletions internal/spew/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ type ConfigState struct {
// invoked for types that implement them.
DisableMethods bool

// EnableTimeStringer specifies whether to invoke the Stringer interface on
// time.Time values even when DisableMethods is true. This is useful to get
// human-readable output for time.Time values while keeping method calls
// disabled for other types.
EnableTimeStringer bool

// DisablePointerMethods specifies whether or not to check for and invoke
// error and Stringer interfaces on types which only accept a pointer
// receiver when the current type is not a pointer.
Expand Down
2 changes: 1 addition & 1 deletion internal/spew/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (d *dumpState) dump(v reflect.Value) {

// Call Stringer/error interfaces if they exist and the handle methods flag
// is enabled
if !d.cs.DisableMethods {
if !d.cs.DisableMethods || (d.cs.EnableTimeStringer && v.Type().String() == "time.Time") {
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
if handled := handleMethods(d.cs, d.w, v); handled {
return
Expand Down
2 changes: 1 addition & 1 deletion internal/spew/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (f *formatState) format(v reflect.Value) {

// Call Stringer/error interfaces if they exist and the handle methods
// flag is enabled.
if !f.cs.DisableMethods {
if !f.cs.DisableMethods || (f.cs.EnableTimeStringer && v.Type().String() == "time.Time") {
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
if handled := handleMethods(f.cs, f.fs, v); handled {
return
Expand Down
9 changes: 9 additions & 0 deletions internal/spew/spew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/ioutil"
"os"
"testing"
"time"

"github.com/stretchr/testify/internal/spew"
)
Expand Down Expand Up @@ -127,6 +128,7 @@ func initSpewTests() {
// Config states with various settings.
scsDefault := spew.NewDefaultConfig()
scsNoMethods := &spew.ConfigState{Indent: " ", DisableMethods: true}
scsNoMethodsButTimeStringer := &spew.ConfigState{Indent: " ", DisableMethods: true, EnableTimeStringer: true}
scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true}
scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1}
scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
Expand All @@ -138,6 +140,8 @@ func initSpewTests() {
ts := stringer("test")
tps := pstringer("test")

tm := time.Date(2006, time.January, 2, 15, 4, 5, 999999999, time.UTC)

type ptrTester struct {
s *struct{}
}
Expand Down Expand Up @@ -203,6 +207,11 @@ func initSpewTests() {
{scsNoPtrAddr, fCSSdump, "", tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})\n"},
{scsNoCap, fCSSdump, "", make([]string, 0, 10), "([]string) {\n}\n"},
{scsNoCap, fCSSdump, "", make([]string, 1, 10), "([]string) (len=1) {\n(string) \"\"\n}\n"},

// time.Time formatting:
{scsDefault, fCSFprint, "", tm, "2006-01-02 15:04:05.999999999 +0000 UTC"},
{scsNoMethods, fCSFprint, "", tm, "{999999999 63271811045 <nil>}"},
{scsNoMethodsButTimeStringer, fCSFprint, "", tm, "2006-01-02 15:04:05.999999999 +0000 UTC"},
}
}

Expand Down