Skip to content

Commit

Permalink
Adjust style of EquateApproxTime (#177)
Browse files Browse the repository at this point in the history
Adjust coding style of EquateApproxTime to match EquateApprox.
  • Loading branch information
dsnet committed Dec 16, 2019
1 parent e1f03df commit 3838af3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
25 changes: 11 additions & 14 deletions cmp/cmpopts/equate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
59 changes: 32 additions & 27 deletions cmp/cmpopts/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand All @@ -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}}}},
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 3838af3

Please sign in to comment.