autogold
autogold copied to clipboard
valast: feature Request: custom time.Time/datatype result generation and comparison
Background: saving to database and generating the result giving different diff because of timezone difference
minimal code to reproduce:
package foo
import "time"
import "github.com/hexops/autogold"
import "testing"
func TestFoo(t *testing.T) {
const layout = "Jan 2, 2006 at 3:04pm (MST)"
tm, _ := time.Parse(layout, "Feb 4, 2014 at 6:05pm (PST)")
t2 := tm.UTC() // assuming this result queried from database from column that doesn't store timezone
println(tm.UnixNano(), t2.UnixNano(), tm.UnixNano() == t2.UnixNano()) // both are inherently equal
want := autogold.Want("internally equal", t2)
want.Equal(t, tm)
}
it shows:
1391537100000000000 1391537100000000000 true
--- FAIL: TestFoo (0.06s)
want.go:200: mismatch (-want +got):
--- want
+++ got
@@ -1 +1,10 @@
-time.Time{ext: 63527133900}
\ No newline at end of file
+time.Time{ext: 63527133900, loc: &time.Location{
+ name: "PST",
+ zone: []time.zone{time.zone{
+ name: "PST",
+ }},
+ tx: []time.zoneTrans{time.zoneTrans{when: -9223372036854775808}},
+ cacheStart: -9223372036854775808,
+ cacheEnd: 9223372036854775807,
+ cacheZone: &time.zone{name: "PST"},
+}}
\ No newline at end of file
FAIL
FAIL command-line-arguments 0.060s
FAIL
where it inherently the same.
- Is there a way to customize the generated struct to be using
time.Unix(sec, nanosec)
instead of full struct since&time.Time{ext:123}
has unexported field? - Is there a way to customize the comparison of value so it would consider inherently equal time ignoring the timezone as equal?
This is a bug in valast, which is what does the struct formatting: https://github.com/hexops/valast/issues/11#issuecomment-894741682
In practice, though, I suggest that when your data structures become this complex you bail out of the default formatting and instead create a simpler struct (or string) just for passing to valast.