Skip to content
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

Add WithinTimeRange method #1188

Merged
merged 6 commits into from Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions assert/assertion_format.go
Expand Up @@ -736,6 +736,16 @@ func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta tim
return WithinDuration(t, expected, actual, delta, append([]interface{}{msg}, args...)...)
}

// WithinTimeRangef asserts that a time is within a time range (inclusive).
//
// assert.WithinTimeRangef(t, time.Now(), time.Now(), time.Now(), "error message %s", "formatted")
func WithinTimeRangef(t TestingT, expected time.Time, start time.Time, end time.Time, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return WithinTimeRange(t, expected, start, end, append([]interface{}{msg}, args...)...)
}

// YAMLEqf asserts that two YAML strings are equivalent.
func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
Expand Down
20 changes: 20 additions & 0 deletions assert/assertion_forward.go
Expand Up @@ -1461,6 +1461,26 @@ func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta
return WithinDurationf(a.t, expected, actual, delta, msg, args...)
}

// WithinTimeRange asserts that a time is within a time range (inclusive).
//
// a.WithinTimeRange(time.Now(), time.Now(), time.Now())
func (a *Assertions) WithinTimeRange(expected time.Time, start time.Time, end time.Time, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return WithinTimeRange(a.t, expected, start, end, msgAndArgs...)
}

// WithinTimeRangef asserts that a time is within a time range (inclusive).
//
// a.WithinTimeRangef(time.Now(), time.Now(), time.Now(), "error message %s", "formatted")
func (a *Assertions) WithinTimeRangef(expected time.Time, start time.Time, end time.Time, msg string, args ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
return WithinTimeRangef(a.t, expected, start, end, msg, args...)
}

// YAMLEq asserts that two YAML strings are equivalent.
func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
Expand Down
21 changes: 21 additions & 0 deletions assert/assertions.go
Expand Up @@ -1109,6 +1109,27 @@ func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration,
return true
}

// WithinTimeRange asserts that a time is within a time range (inclusive).
//
// assert.WithinTimeRange(t, time.Now(), time.Now(), time.Now())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm of the opinion that this example isn't the most intuitive. Would it make more sense if we changed it to something like:

assert.WithinTimeRange(t, time.Now(), time.Now().Sub(time.Second()), time.Now().Add(time.Second()))

If you can think of a simpler example that would explicitly demonstrate the differences between start and end, that would be awesome :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll think about it a bit and have a look at other examples to see if there's a better way to present it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to assert.WithinTimeRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second)).

func WithinTimeRange(t TestingT, expected, start, end time.Time, msgAndArgs ...interface{}) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. For consistency with WithinDuration, should this function not be named WithinRange, the time.Time arguments explicitly state that the range being tested is a time range?
  2. Semantically, the 2nd argument should be called actual, not expected since it's the value we're testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Sure. I added the Time to be more specific (since duration is a time related word, but range is not), but if it's not required I'm more than happy to change the naming to WithinRange.
  2. You are right, will make the change.

if h, ok := t.(tHelper); ok {
h.Helper()
}

if end.Before(start) {
return Fail(t, "start should be before end", msgAndArgs...)
moolmanruan marked this conversation as resolved.
Show resolved Hide resolved
}

if expected.Before(start) {
return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is before the range", expected, start, end), msgAndArgs...)
} else if expected.After(end) {
return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is after the range", expected, start, end), msgAndArgs...)
}

return true
}

func toFloat(x interface{}) (float64, bool) {
var xf float64
xok := true
Expand Down
19 changes: 19 additions & 0 deletions assert/assertions_test.go
Expand Up @@ -1345,6 +1345,25 @@ func TestWithinDuration(t *testing.T) {
False(t, WithinDuration(mockT, b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
}

func TestWithinTimeRange(t *testing.T) {

mockT := new(testing.T)
n := time.Now()
s := n.Add(-time.Second)
e := n.Add(time.Second)

True(t, WithinTimeRange(mockT, n, n, n), "Exact same expected, start, and end values return true")

True(t, WithinTimeRange(mockT, n, s, e), "Time in range is within the time range")
True(t, WithinTimeRange(mockT, s, s, e), "The start time is within the time range")
True(t, WithinTimeRange(mockT, e, s, e), "The end time is within the time range")

False(t, WithinTimeRange(mockT, s.Add(-time.Nanosecond), s, e, "Just before the start time is not within the time range"))
False(t, WithinTimeRange(mockT, e.Add(time.Nanosecond), s, e, "Just after the end time is not within the time range"))

False(t, WithinTimeRange(mockT, n, e, s, "Just after the end time is not within the time range"))
}

func TestInDelta(t *testing.T) {
mockT := new(testing.T)

Expand Down
26 changes: 26 additions & 0 deletions require/require.go
Expand Up @@ -1864,6 +1864,32 @@ func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta tim
t.FailNow()
}

// WithinTimeRange asserts that a time is within a time range (inclusive).
//
// assert.WithinTimeRange(t, time.Now(), time.Now(), time.Now())
func WithinTimeRange(t TestingT, expected time.Time, start time.Time, end time.Time, msgAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if assert.WithinTimeRange(t, expected, start, end, msgAndArgs...) {
return
}
t.FailNow()
}

// WithinTimeRangef asserts that a time is within a time range (inclusive).
//
// assert.WithinTimeRangef(t, time.Now(), time.Now(), time.Now(), "error message %s", "formatted")
func WithinTimeRangef(t TestingT, expected time.Time, start time.Time, end time.Time, msg string, args ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if assert.WithinTimeRangef(t, expected, start, end, msg, args...) {
return
}
t.FailNow()
}

// YAMLEq asserts that two YAML strings are equivalent.
func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
Expand Down
20 changes: 20 additions & 0 deletions require/require_forward.go
Expand Up @@ -1462,6 +1462,26 @@ func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta
WithinDurationf(a.t, expected, actual, delta, msg, args...)
}

// WithinTimeRange asserts that a time is within a time range (inclusive).
//
// a.WithinTimeRange(time.Now(), time.Now(), time.Now())
func (a *Assertions) WithinTimeRange(expected time.Time, start time.Time, end time.Time, msgAndArgs ...interface{}) {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
WithinTimeRange(a.t, expected, start, end, msgAndArgs...)
}

// WithinTimeRangef asserts that a time is within a time range (inclusive).
//
// a.WithinTimeRangef(time.Now(), time.Now(), time.Now(), "error message %s", "formatted")
func (a *Assertions) WithinTimeRangef(expected time.Time, start time.Time, end time.Time, msg string, args ...interface{}) {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
WithinTimeRangef(a.t, expected, start, end, msg, args...)
}

// YAMLEq asserts that two YAML strings are equivalent.
func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) {
if h, ok := a.t.(tHelper); ok {
Expand Down