mirror of https://github.com/stretchr/testify.git
added new assert WithinDuration
parent
5f6413d60a
commit
0378c681e8
|
@ -6,6 +6,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Comparison a custom function that returns true on success and false on failure
|
// Comparison a custom function that returns true on success and false on failure
|
||||||
|
@ -414,6 +415,21 @@ func NotPanics(t *testing.T, f PanicTestFunc, msgAndArgs ...interface{}) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithinDuration asserts that the two times are within duration delta of each other.
|
||||||
|
//
|
||||||
|
// assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second, "The difference should not be more than 10s")
|
||||||
|
//
|
||||||
|
// Returns whether the assertion was successful (true) or not (false).
|
||||||
|
func WithinDuration(t *testing.T, a, b time.Time, delta time.Duration, msgAndArgs ...interface{}) bool {
|
||||||
|
|
||||||
|
dt := a.Sub(b)
|
||||||
|
if dt < -delta || dt > delta {
|
||||||
|
return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", a, b, dt, delta), msgAndArgs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Errors
|
Errors
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -3,6 +3,7 @@ package assert
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AssertionTesterInterface defines an interface to be used for testing assertion methods
|
// AssertionTesterInterface defines an interface to be used for testing assertion methods
|
||||||
|
@ -361,3 +362,14 @@ func TestNotEmpty(t *testing.T) {
|
||||||
True(t, NotEmpty(mockT, true), "True value is not empty")
|
True(t, NotEmpty(mockT, true), "True value is not empty")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithinDuration(t *testing.T) {
|
||||||
|
|
||||||
|
mockT := new(testing.T)
|
||||||
|
a := time.Now()
|
||||||
|
b := a.Add(10 * time.Second)
|
||||||
|
|
||||||
|
True(t, WithinDuration(mockT, a, b, 10*time.Second), "A 10s difference is within a 10s time difference")
|
||||||
|
|
||||||
|
False(t, WithinDuration(mockT, a, b, 9*time.Second), "A 10s difference is not within a 9s time difference")
|
||||||
|
}
|
||||||
|
|
|
@ -68,4 +68,7 @@
|
||||||
// // call code that should not panic
|
// // call code that should not panic
|
||||||
//
|
//
|
||||||
// } [, message [, format-args]])
|
// } [, message [, format-args]])
|
||||||
|
//
|
||||||
|
// assert.WithinDuration(t, timeA, timeB, deltaTime, [, message [, format-args]])
|
||||||
|
|
||||||
package assert
|
package assert
|
||||||
|
|
Loading…
Reference in New Issue