-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Struct containing a time.Time can't be compared correctly. #984
Comments
Could you please add a unit test to cover the missed case. If you have issues, please let me know so that I can revert the change |
I'm not sure what the success criteria are for this issue, what does "correctly" mean for comparing time.Time. v1 uses reflect.DeepEqual and to change this would be a breaking change to anyone relying on that behaviour. |
I do not think that anyone can be relying on that behavior. Because current behavior means that nested timestamps are randomly different. This is why
I think the same holds for "expected values" in tests. See also this comment. |
The location encoded in the time.Time object, which is not considered by the Equal method, is exported (public) information. Anyone writing calendar/invite software or flight booking software or any other application where the returned zone is important will be relying on the current comparison. |
Beyond location there is also monotonic clock reading possibly in the struct. That one will never be the same unless you literally made a copy of an existing struct. And sure, we might not want to break existing function, but there is definitely a need for having a deep-compare assertion which works on time structs. |
I agree with you on that, we already have |
Ooo, struct tags. I like it. |
is there a practical solution for this? |
With current testify, the most practical solution to this is to use a test helper function: package kata_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type MyStruct struct {
Name string
Birthday time.Time
}
func TestTrue(t *testing.T) {
now := time.Now()
alice1 := MyStruct{
Name: "Alice",
Birthday: now,
}
alice2 := MyStruct{
Name: "Alice",
Birthday: now.In(time.UTC),
}
assertMyStructSortOfEqual(t, alice1, alice2)
}
func assertMyStructSortOfEqual(t *testing.T, expected, actual MyStruct) bool {
t.Helper()
if !assert.Equal(t, expected.Name, actual.Name,
"expected Name to be %q but got %q", expected.Name, actual.Name) {
return false
}
if !assert.WithinDuration(t, expected.Birthday, actual.Birthday, 0,
"expected Birthday to be %dns but got %dns",
expected.Birthday.UnixNano(), actual.Birthday.UnixNano()) {
return false
}
return true
} |
#979 fixed comparison of
time.Time
, but struct containingtime.Time
still can't be compared correctly because still, we will usereflect.DeepEqual
.I'm working on a PR to fix this.
The text was updated successfully, but these errors were encountered: