Skip to content

Commit 7b28236

Browse files
committed
feat: improve the way time.Time is stringified in spew
Now we vendored go-spew we can modify how time.Time values are handled. We can remove the imperfect fix we had in place before.
1 parent 65697ce commit 7b28236

File tree

7 files changed

+48
-13
lines changed

7 files changed

+48
-13
lines changed

assert/assertions.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,9 +1951,6 @@ func diff(expected interface{}, actual interface{}) string {
19511951
case reflect.TypeOf(""):
19521952
e = reflect.ValueOf(expected).String()
19531953
a = reflect.ValueOf(actual).String()
1954-
case reflect.TypeOf(time.Time{}):
1955-
e = spewConfigStringerEnabled.Sdump(expected)
1956-
a = spewConfigStringerEnabled.Sdump(actual)
19571954
default:
19581955
e = spewConfig.Sdump(expected)
19591956
a = spewConfig.Sdump(actual)
@@ -1985,14 +1982,7 @@ var spewConfig = spew.ConfigState{
19851982
DisableCapacities: true,
19861983
SortKeys: true,
19871984
DisableMethods: true,
1988-
MaxDepth: 10,
1989-
}
1990-
1991-
var spewConfigStringerEnabled = spew.ConfigState{
1992-
Indent: " ",
1993-
DisablePointerAddresses: true,
1994-
DisableCapacities: true,
1995-
SortKeys: true,
1985+
EnableTimeStringer: true,
19961986
MaxDepth: 10,
19971987
}
19981988

assert/assertions_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,6 +3036,30 @@ Diff:
30363036
time.Date(2020, 9, 25, 0, 0, 0, 0, time.UTC),
30373037
)
30383038
Equal(t, expected, actual)
3039+
3040+
expected = `
3041+
3042+
Diff:
3043+
--- Expected
3044+
+++ Actual
3045+
@@ -1,3 +1,3 @@
3046+
(assert.someStruct) {
3047+
- t: (time.Time) 2020-09-24 00:00:00 +0000 UTC
3048+
+ t: (time.Time) 2020-09-25 00:00:00 +0000 UTC
3049+
}
3050+
`
3051+
3052+
type someStruct struct {
3053+
t time.Time
3054+
}
3055+
3056+
actual = diff(
3057+
someStruct{t: time.Date(2020, 9, 24, 0, 0, 0, 0, time.UTC)},
3058+
someStruct{t: time.Date(2020, 9, 25, 0, 0, 0, 0, time.UTC)},
3059+
)
3060+
3061+
Equal(t, expected, actual)
3062+
30393063
}
30403064

30413065
func TestTimeEqualityErrorFormatting(t *testing.T) {

internal/spew/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ func canSortSimply(kind reflect.Kind) bool {
271271
case reflect.Array:
272272
return true
273273
}
274+
275+
if kind == reflect.Struct && kind.String() == "time.Time" {
276+
// time.Time values can be sorted directly, as it implements [sort.Interface].
277+
return true
278+
}
279+
274280
return false
275281
}
276282

internal/spew/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ type ConfigState struct {
5353
// invoked for types that implement them.
5454
DisableMethods bool
5555

56+
// EnableTimeStringer specifies whether to invoke the Stringer interface on
57+
// time.Time values even when DisableMethods is true. This is useful to get
58+
// human-readable output for time.Time values while keeping method calls
59+
// disabled for other types.
60+
EnableTimeStringer bool
61+
5662
// DisablePointerMethods specifies whether or not to check for and invoke
5763
// error and Stringer interfaces on types which only accept a pointer
5864
// receiver when the current type is not a pointer.

internal/spew/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (d *dumpState) dump(v reflect.Value) {
301301

302302
// Call Stringer/error interfaces if they exist and the handle methods flag
303303
// is enabled
304-
if !d.cs.DisableMethods {
304+
if !d.cs.DisableMethods || (d.cs.EnableTimeStringer && v.Type().String() == "time.Time") {
305305
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
306306
if handled := handleMethods(d.cs, d.w, v); handled {
307307
return

internal/spew/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (f *formatState) format(v reflect.Value) {
222222

223223
// Call Stringer/error interfaces if they exist and the handle methods
224224
// flag is enabled.
225-
if !f.cs.DisableMethods {
225+
if !f.cs.DisableMethods || (f.cs.EnableTimeStringer && v.Type().String() == "time.Time") {
226226
if (kind != reflect.Invalid) && (kind != reflect.Interface) {
227227
if handled := handleMethods(f.cs, f.fs, v); handled {
228228
return

internal/spew/spew_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/ioutil"
2323
"os"
2424
"testing"
25+
"time"
2526

2627
"github.com/stretchr/testify/internal/spew"
2728
)
@@ -127,6 +128,7 @@ func initSpewTests() {
127128
// Config states with various settings.
128129
scsDefault := spew.NewDefaultConfig()
129130
scsNoMethods := &spew.ConfigState{Indent: " ", DisableMethods: true}
131+
scsNoMethodsButTimeStringer := &spew.ConfigState{Indent: " ", DisableMethods: true, EnableTimeStringer: true}
130132
scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true}
131133
scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1}
132134
scsContinue := &spew.ConfigState{Indent: " ", ContinueOnMethod: true}
@@ -138,6 +140,8 @@ func initSpewTests() {
138140
ts := stringer("test")
139141
tps := pstringer("test")
140142

143+
tm := time.Date(2006, time.January, 2, 15, 4, 5, 999999999, time.UTC)
144+
141145
type ptrTester struct {
142146
s *struct{}
143147
}
@@ -203,6 +207,11 @@ func initSpewTests() {
203207
{scsNoPtrAddr, fCSSdump, "", tptr, "(*spew_test.ptrTester)({\ns: (*struct {})({\n})\n})\n"},
204208
{scsNoCap, fCSSdump, "", make([]string, 0, 10), "([]string) {\n}\n"},
205209
{scsNoCap, fCSSdump, "", make([]string, 1, 10), "([]string) (len=1) {\n(string) \"\"\n}\n"},
210+
211+
// time.Time formatting:
212+
{scsDefault, fCSFprint, "", tm, "2006-01-02 15:04:05.999999999 +0000 UTC"},
213+
{scsNoMethods, fCSFprint, "", tm, "{999999999 63271811045 <nil>}"},
214+
{scsNoMethodsButTimeStringer, fCSFprint, "", tm, "2006-01-02 15:04:05.999999999 +0000 UTC"},
206215
}
207216
}
208217

0 commit comments

Comments
 (0)