Skip to content

Commit 2fab6df

Browse files
authored
Add WithinTimeRange method (#1188)
* Add WithinTimeRange method * Run ./.ci.generate.sh * Rename WithinTimeRange to WithinRange * Rename WithinRange expected parameter to actual * Capitalise start parameter at start of error message * Improve WithinRange example
1 parent b5ce165 commit 2fab6df

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

assert/assertion_format.go

+10
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,16 @@ func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta tim
736736
return WithinDuration(t, expected, actual, delta, append([]interface{}{msg}, args...)...)
737737
}
738738

739+
// WithinRangef asserts that a time is within a time range (inclusive).
740+
//
741+
// assert.WithinRangef(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted")
742+
func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, msg string, args ...interface{}) bool {
743+
if h, ok := t.(tHelper); ok {
744+
h.Helper()
745+
}
746+
return WithinRange(t, actual, start, end, append([]interface{}{msg}, args...)...)
747+
}
748+
739749
// YAMLEqf asserts that two YAML strings are equivalent.
740750
func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool {
741751
if h, ok := t.(tHelper); ok {

assert/assertion_forward.go

+20
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,26 @@ func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta
14611461
return WithinDurationf(a.t, expected, actual, delta, msg, args...)
14621462
}
14631463

1464+
// WithinRange asserts that a time is within a time range (inclusive).
1465+
//
1466+
// a.WithinRange(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))
1467+
func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs ...interface{}) bool {
1468+
if h, ok := a.t.(tHelper); ok {
1469+
h.Helper()
1470+
}
1471+
return WithinRange(a.t, actual, start, end, msgAndArgs...)
1472+
}
1473+
1474+
// WithinRangef asserts that a time is within a time range (inclusive).
1475+
//
1476+
// a.WithinRangef(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted")
1477+
func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, args ...interface{}) bool {
1478+
if h, ok := a.t.(tHelper); ok {
1479+
h.Helper()
1480+
}
1481+
return WithinRangef(a.t, actual, start, end, msg, args...)
1482+
}
1483+
14641484
// YAMLEq asserts that two YAML strings are equivalent.
14651485
func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool {
14661486
if h, ok := a.t.(tHelper); ok {

assert/assertions.go

+21
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,27 @@ func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration,
11101110
return true
11111111
}
11121112

1113+
// WithinRange asserts that a time is within a time range (inclusive).
1114+
//
1115+
// assert.WithinRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))
1116+
func WithinRange(t TestingT, actual, start, end time.Time, msgAndArgs ...interface{}) bool {
1117+
if h, ok := t.(tHelper); ok {
1118+
h.Helper()
1119+
}
1120+
1121+
if end.Before(start) {
1122+
return Fail(t, "Start should be before end", msgAndArgs...)
1123+
}
1124+
1125+
if actual.Before(start) {
1126+
return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is before the range", actual, start, end), msgAndArgs...)
1127+
} else if actual.After(end) {
1128+
return Fail(t, fmt.Sprintf("Time %v expected to be in time range %v to %v, but is after the range", actual, start, end), msgAndArgs...)
1129+
}
1130+
1131+
return true
1132+
}
1133+
11131134
func toFloat(x interface{}) (float64, bool) {
11141135
var xf float64
11151136
xok := true

assert/assertions_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,25 @@ func TestWithinDuration(t *testing.T) {
13501350
False(t, WithinDuration(mockT, b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
13511351
}
13521352

1353+
func TestWithinRange(t *testing.T) {
1354+
1355+
mockT := new(testing.T)
1356+
n := time.Now()
1357+
s := n.Add(-time.Second)
1358+
e := n.Add(time.Second)
1359+
1360+
True(t, WithinRange(mockT, n, n, n), "Exact same actual, start, and end values return true")
1361+
1362+
True(t, WithinRange(mockT, n, s, e), "Time in range is within the time range")
1363+
True(t, WithinRange(mockT, s, s, e), "The start time is within the time range")
1364+
True(t, WithinRange(mockT, e, s, e), "The end time is within the time range")
1365+
1366+
False(t, WithinRange(mockT, s.Add(-time.Nanosecond), s, e, "Just before the start time is not within the time range"))
1367+
False(t, WithinRange(mockT, e.Add(time.Nanosecond), s, e, "Just after the end time is not within the time range"))
1368+
1369+
False(t, WithinRange(mockT, n, e, s, "Just after the end time is not within the time range"))
1370+
}
1371+
13531372
func TestInDelta(t *testing.T) {
13541373
mockT := new(testing.T)
13551374

require/require.go

+26
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,32 @@ func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta tim
18641864
t.FailNow()
18651865
}
18661866

1867+
// WithinRange asserts that a time is within a time range (inclusive).
1868+
//
1869+
// assert.WithinRange(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))
1870+
func WithinRange(t TestingT, actual time.Time, start time.Time, end time.Time, msgAndArgs ...interface{}) {
1871+
if h, ok := t.(tHelper); ok {
1872+
h.Helper()
1873+
}
1874+
if assert.WithinRange(t, actual, start, end, msgAndArgs...) {
1875+
return
1876+
}
1877+
t.FailNow()
1878+
}
1879+
1880+
// WithinRangef asserts that a time is within a time range (inclusive).
1881+
//
1882+
// assert.WithinRangef(t, time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted")
1883+
func WithinRangef(t TestingT, actual time.Time, start time.Time, end time.Time, msg string, args ...interface{}) {
1884+
if h, ok := t.(tHelper); ok {
1885+
h.Helper()
1886+
}
1887+
if assert.WithinRangef(t, actual, start, end, msg, args...) {
1888+
return
1889+
}
1890+
t.FailNow()
1891+
}
1892+
18671893
// YAMLEq asserts that two YAML strings are equivalent.
18681894
func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) {
18691895
if h, ok := t.(tHelper); ok {

require/require_forward.go

+20
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,26 @@ func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta
14621462
WithinDurationf(a.t, expected, actual, delta, msg, args...)
14631463
}
14641464

1465+
// WithinRange asserts that a time is within a time range (inclusive).
1466+
//
1467+
// a.WithinRange(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second))
1468+
func (a *Assertions) WithinRange(actual time.Time, start time.Time, end time.Time, msgAndArgs ...interface{}) {
1469+
if h, ok := a.t.(tHelper); ok {
1470+
h.Helper()
1471+
}
1472+
WithinRange(a.t, actual, start, end, msgAndArgs...)
1473+
}
1474+
1475+
// WithinRangef asserts that a time is within a time range (inclusive).
1476+
//
1477+
// a.WithinRangef(time.Now(), time.Now().Add(-time.Second), time.Now().Add(time.Second), "error message %s", "formatted")
1478+
func (a *Assertions) WithinRangef(actual time.Time, start time.Time, end time.Time, msg string, args ...interface{}) {
1479+
if h, ok := a.t.(tHelper); ok {
1480+
h.Helper()
1481+
}
1482+
WithinRangef(a.t, actual, start, end, msg, args...)
1483+
}
1484+
14651485
// YAMLEq asserts that two YAML strings are equivalent.
14661486
func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) {
14671487
if h, ok := a.t.(tHelper); ok {

0 commit comments

Comments
 (0)