Skip to content

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

@shawc71

Description

@shawc71

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 #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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions