stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
23.02k stars 1.58k forks source link

Diffs on structs with nested time.Time objects produce hard to read diffs #1078

Open shawc71 opened 3 years ago

shawc71 commented 3 years ago

We recently observed tough to read diffs produced by testify on assertion failures on structs when the structs contain nested time.Time objects.

I have created this repo with this testfile that does a minimal repro of the issue https://github.com/shawc71/testify-issue-demo/blob/master/example_test.go, this uses the latest testify commit on master.

In summary, given a struct like:

type structWithTime struct {
            someTime   *time.Time
            someString string
}

the following produces a diff that is 1220 lines+ and unparsable for humans:

    t.Run("different tzs", func(t *testing.T) {
        expectedTime, err := time.Parse("2006-01-02", "2020-05-22")
        assert.NoError(t, err)
        expected := &structWithTime{
            someTime:   &expectedTime,
            someString: "some val",
        }

        loc, err := time.LoadLocation("America/New_York")
        assert.NoError(t, err)
        actualTime, err := time.ParseInLocation("2006-01-02", "2020-05-21", loc)
        actual := &structWithTime{
            someTime:   &actualTime,
            someString: "some val",
        }
        assert.Equal(t, expected, actual)
    })

I believe this was an unintended side-effect of https://github.com/stretchr/testify/pull/895.

This makes doing assertions on structs with time.Time pretty rough. I imagine having a time.Time in a struct and being able to do assertions on those structs is a pretty common use case so I think we should make this nicer. :)

I am working on a PR to address this :)

shawc71 commented 3 years ago

Here's my attempt at fixing it: https://github.com/stretchr/testify/pull/1079