Skip to content

assert: Equal does not consider Zone information in time.Time #1536

@brackendawson

Description

@brackendawson

This test should not pass:

package kata_test

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func ToSydney(t time.Time) time.Time {
	// zoneSyd, err := time.LoadLocation("Australia/Sydney")
	zoneSyd, err := time.LoadLocation("Europe/London")
	if err != nil {
		panic("Australia is gone")
	}
	return t.In(zoneSyd)
}

func TestToSydney(t *testing.T) {
	zoneSyd, err := time.LoadLocation("Australia/Sydney")
	require.NoError(t, err)
	now := time.Now()
	require.Equal(t, now.In(zoneSyd), ToSydney(now))
}

And on v1.8.4 it correctly fails, but on master it incorrectly passes.

This is a regression introduced by #1464. time.Time.Equal does not mean the two instances are equal, it means they represent the same instant. There is more information contained in a time.Time instance than the instant, this regression makes it impossible to test for state which is a best practice. If the user only cares about the instant represented then WithinDuration(t, expected, actual, 0) is available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpkg-assertChange related to package testify/assert

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions