stretchr / testify

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

wall clock #1510

Closed yonesko closed 5 months ago

yonesko commented 7 months ago

Have an annoying issue comparing structs with time:

package main

import (
    "encoding/json"
    "testing"
    "time"

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

type A struct {
    T time.Time
}

func Test(t *testing.T) {
    now := time.Now()
    a := A{T: now}
    marshal, _ := json.Marshal(a)
    var aParsed A
    json.Unmarshal(marshal, &aParsed)
    assert.Equal(t, a, aParsed)
}

Output:

            Error:          Not equal: 
                            expected: repository.A{T:time.Date(2023, time.November, 23, 12, 7, 10, 872457000, time.Local)}
                            actual  :     repository.A{T:time.Date(2023, time.November, 23, 12, 7, 10, 872457000, time.Local)}

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -2,4 +2,4 @@
                              T: (time.Time) {
                            -  wall: (uint64) 13929602241381049128,
                            -  ext: (int64) 3371626,
                            +  wall: (uint64) 872457000,
                            +  ext: (int64) 63836327230,
                               loc: (*time.Location)({
            Test:           Test
--- FAIL: Test (0.00s)

Expected :repository.A{T:time.Date(2023, time.November, 23, 12, 7, 10, 872457000, time.Local)}
Actual   :repository.A{T:time.Date(2023, time.November, 23, 12, 7, 10, 872457000, time.Local)}
<Click to see difference>

FAIL

Process finished with the exit code 1
dolmen commented 7 months ago

I don't understand your issue. Please post a self-contained example that reproduces your issue using this template: https://go.dev/play/p/AsRNcxCRdWx

yonesko commented 7 months ago

I have updated the first comment

dolmen commented 7 months ago

The Testify API is very comprehensive. Have you tried the other Equal functions? Please expand your test code to show the issue you have with the other functions.

By the way, this isn't a support forum. Issues are for reporting bugs or discussing enhancements. There are more appropriate places for asking for help.

yonesko commented 7 months ago

This is not support^ this is bug

tscales commented 7 months ago

similar issue as #1461. This should be resolved whenever a new release is cut.

KKKZOZ commented 6 months ago

i have met with this too, please release a new package asap

gabriel-vasile commented 5 months ago

similar issue as #1461. This should be resolved whenever a new release is cut.

It's a little different. Here T time is a field in a struct, meaning the equality comparison defaults to reflect.DeepEqual. New release will not fix this issue.

brackendawson commented 5 months ago

Testify is correct, they are not equal. The behaviour of actual is demonstrably different to the behaviour of expected.

If you only care that they have the same time, then use WithinDuration:

package main

import (
    "encoding/json"
    "testing"
    "time"

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

type A struct {
    T time.Time
}

func Test(t *testing.T) {
    now := time.Now()
    a := A{T: now}
    marshal, _ := json.Marshal(a)
    var aParsed A
    json.Unmarshal(marshal, &aParsed)
    assert.WithinDuration(t, a.T, aParsed.T, 0)
}
yonesko commented 5 months ago

How to compare not times but a structure with time fields ?

brackendawson commented 5 months ago

How to compare not times but a structure with time fields ?

You'll need to either test the fields individually or write an equality test helper function for your struct, for what you consider to be equal. time.Time values are not the only examply where this becomes necessary, another common one is slice type fields where you only care about the elements matching and not the order. The fields literally aren't equal but you can't use EqualElements on a struct.

Another option may be to use time.Now().Truncate(0) as your test input, this strips the wall time.

I'm afraid we can't call this a bug because the behaviour of Equal is as documented according to the package docs and the Go spec, and to change the bahaviour would be a breaking change. What if someone's test depends on the wall time difference between time.Time values?

If you can reason that this change should be made, or you can propose a good design for a compatible change that isn't assert.SortOfEqual please comment and I will re-open the issue.