diff options
author | Joe Tsai <joetsai@digital-static.net> | 2019-12-16 08:58:15 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-16 08:58:15 -0800 |
commit | 3838af334ff48ab62a68998c9a4ee9d847017617 (patch) | |
tree | 6130153edce3c2f881f49f1c3353d5c83e365a3a /cmp/cmpopts | |
parent | e1f03df4dcb7b99f29424fd98901b73dc1102937 (diff) | |
download | go-cmp-3838af334ff48ab62a68998c9a4ee9d847017617.tar.gz |
Adjust style of EquateApproxTime (#177)
Adjust coding style of EquateApproxTime to match EquateApprox.
Diffstat (limited to 'cmp/cmpopts')
-rw-r--r-- | cmp/cmpopts/equate.go | 25 | ||||
-rw-r--r-- | cmp/cmpopts/util_test.go | 59 |
2 files changed, 43 insertions, 41 deletions
diff --git a/cmp/cmpopts/equate.go b/cmp/cmpopts/equate.go index fea57bc..343dcf9 100644 --- a/cmp/cmpopts/equate.go +++ b/cmp/cmpopts/equate.go @@ -89,23 +89,20 @@ func areNaNsF32s(x, y float32) bool { return areNaNsF64s(float64(x), float64(y)) } -// EquateApproxTime returns a Comparer options that -// determine two time.Time values to be equal if they -// are within the given time interval of one another. -// Note that if both times have a monotonic clock reading, -// the monotonic time difference will be used. -// -// The zero time is treated specially: it is only considered -// equal to another zero time value. -// -// It will panic if margin is negative. +// EquateApproxTime returns a Comparer option that determines two non-zero +// time.Time values to be equal if they are within some margin of one another. +// If both times have a monotonic clock reading, then the monotonic time +// difference will be used. The margin must be non-negative. func EquateApproxTime(margin time.Duration) cmp.Option { if margin < 0 { - panic("negative duration in EquateApproxTime") + panic("margin must be a non-negative number") } - return cmp.FilterValues(func(x, y time.Time) bool { - return !x.IsZero() && !y.IsZero() - }, cmp.Comparer(timeApproximator{margin}.compare)) + a := timeApproximator{margin} + return cmp.FilterValues(areNonZeroTimes, cmp.Comparer(a.compare)) +} + +func areNonZeroTimes(x, y time.Time) bool { + return !x.IsZero() && !y.IsZero() } type timeApproximator struct { diff --git a/cmp/cmpopts/util_test.go b/cmp/cmpopts/util_test.go index 3c01cac..e80c963 100644 --- a/cmp/cmpopts/util_test.go +++ b/cmp/cmpopts/util_test.go @@ -472,17 +472,19 @@ func TestOptions(t *testing.T) { wantEqual: true, reason: "equal because time is exactly at the allowed margin (negative)", }, { - label: "EquateApproxTime", - x: time.Date(2009, 11, 10, 23, 0, 3, 0, time.UTC), - y: time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), - opts: []cmp.Option{EquateApproxTime(3*time.Second - 1)}, - reason: "not equal because time is outside allowed margin", - }, { - label: "EquateApproxTime", - x: time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), - y: time.Date(2009, 11, 10, 23, 0, 3, 0, time.UTC), - opts: []cmp.Option{EquateApproxTime(3*time.Second - 1)}, - reason: "not equal because time is outside allowed margin (negative)", + label: "EquateApproxTime", + x: time.Date(2009, 11, 10, 23, 0, 3, 0, time.UTC), + y: time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), + opts: []cmp.Option{EquateApproxTime(3*time.Second - 1)}, + wantEqual: false, + reason: "not equal because time is outside allowed margin", + }, { + label: "EquateApproxTime", + x: time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC), + y: time.Date(2009, 11, 10, 23, 0, 3, 0, time.UTC), + opts: []cmp.Option{EquateApproxTime(3*time.Second - 1)}, + wantEqual: false, + reason: "not equal because time is outside allowed margin (negative)", }, { label: "EquateApproxTime", x: time.Time{}, @@ -491,23 +493,26 @@ func TestOptions(t *testing.T) { wantEqual: true, reason: "equal because both times are zero", }, { - label: "EquateApproxTime", - x: time.Time{}, - y: time.Time{}.Add(1), - opts: []cmp.Option{EquateApproxTime(3 * time.Second)}, - reason: "not equal because zero time is always not equal not non-zero", + label: "EquateApproxTime", + x: time.Time{}, + y: time.Time{}.Add(1), + opts: []cmp.Option{EquateApproxTime(3 * time.Second)}, + wantEqual: false, + reason: "not equal because zero time is always not equal not non-zero", }, { - label: "EquateApproxTime", - x: time.Time{}.Add(1), - y: time.Time{}, - opts: []cmp.Option{EquateApproxTime(3 * time.Second)}, - reason: "not equal because zero time is always not equal not non-zero", + label: "EquateApproxTime", + x: time.Time{}.Add(1), + y: time.Time{}, + opts: []cmp.Option{EquateApproxTime(3 * time.Second)}, + wantEqual: false, + reason: "not equal because zero time is always not equal not non-zero", }, { - label: "EquateApproxTime", - x: time.Date(2409, 11, 10, 23, 0, 0, 0, time.UTC), - y: time.Date(2000, 11, 10, 23, 0, 3, 0, time.UTC), - opts: []cmp.Option{EquateApproxTime(3 * time.Second)}, - reason: "time difference overflows time.Duration", + label: "EquateApproxTime", + x: time.Date(2409, 11, 10, 23, 0, 0, 0, time.UTC), + y: time.Date(2000, 11, 10, 23, 0, 3, 0, time.UTC), + opts: []cmp.Option{EquateApproxTime(3 * time.Second)}, + wantEqual: false, + reason: "time difference overflows time.Duration", }, { label: "IgnoreFields", x: Bar1{Foo3{&Foo2{&Foo1{Alpha: 5}}}}, @@ -950,7 +955,7 @@ func TestPanic(t *testing.T) { label: "EquateApproxTime", fnc: EquateApproxTime, args: args(time.Duration(-1)), - wantPanic: "negative duration in EquateApproxTime", + wantPanic: "margin must be a non-negative number", reason: "negative duration is invalid", }, { label: "SortSlices", |