From ae5876d09ad6d0d68dff8d969e518db26e0e7969 Mon Sep 17 00:00:00 2001 From: Miles Steele Date: Mon, 20 May 2019 14:29:29 -0400 Subject: [PATCH 1/2] change report wording, fix int stringification, fix doc --- README.md | 2 ++ assert/assertion_format.go | 6 +++--- assert/assertion_forward.go | 12 ++++++------ assert/assertion_order.go | 14 +++++++------- require/require.go | 12 ++++++------ require/require_forward.go | 12 ++++++------ 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 11951d4..ef0197e 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,8 @@ Please feel free to submit issues, fork the repository and send pull requests! When submitting an issue, we ask that you please include a complete test function that demonstrates the issue. Extra credit for those using Testify to write the test code that demonstrates it. +Code generation is used. Look for `CODE GENERATED AUTOMATICALLY` at the top of some files. Run `go generate ./...` to update generated files. + ------ License diff --git a/assert/assertion_format.go b/assert/assertion_format.go index 8a68b60..b9f1dca 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -169,7 +169,7 @@ func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...in return Greater(t, e1, e2, append([]interface{}{msg}, args...)...) } -// GreaterOrEqualf asserts that the first element in greater or equal than the second +// GreaterOrEqualf asserts that the first element is greater than or equal to the second // // assert.GreaterOrEqualf(t, 2, 1, "error message %s", "formatted") // assert.GreaterOrEqualf(t, 2, 2, "error message %s", "formatted") @@ -325,7 +325,7 @@ func Lenf(t TestingT, object interface{}, length int, msg string, args ...interf return Len(t, object, length, append([]interface{}{msg}, args...)...) } -// Lessf asserts that the first element in less than the second +// Lessf asserts that the first element is less than the second // // assert.Lessf(t, 1, 2, "error message %s", "formatted") // assert.Lessf(t, float64(1, "error message %s", "formatted"), float64(2)) @@ -337,7 +337,7 @@ func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...inter return Less(t, e1, e2, append([]interface{}{msg}, args...)...) } -// LessOrEqualf asserts that the first element in greater or equal than the second +// LessOrEqualf asserts that the first element is less than or equal to the second // // assert.LessOrEqualf(t, 1, 2, "error message %s", "formatted") // assert.LessOrEqualf(t, 2, 2, "error message %s", "formatted") diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index 3c8012a..de0863b 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -315,7 +315,7 @@ func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...inter return Greater(a.t, e1, e2, msgAndArgs...) } -// GreaterOrEqual asserts that the first element in greater or equal than the second +// GreaterOrEqual asserts that the first element is greater than or equal to the second // // a.GreaterOrEqual(2, 1) // a.GreaterOrEqual(2, 2) @@ -328,7 +328,7 @@ func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs . return GreaterOrEqual(a.t, e1, e2, msgAndArgs...) } -// GreaterOrEqualf asserts that the first element in greater or equal than the second +// GreaterOrEqualf asserts that the first element is greater than or equal to the second // // a.GreaterOrEqualf(2, 1, "error message %s", "formatted") // a.GreaterOrEqualf(2, 2, "error message %s", "formatted") @@ -639,7 +639,7 @@ func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...in return Lenf(a.t, object, length, msg, args...) } -// Less asserts that the first element in less than the second +// Less asserts that the first element is less than the second // // a.Less(1, 2) // a.Less(float64(1), float64(2)) @@ -651,7 +651,7 @@ func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interfac return Less(a.t, e1, e2, msgAndArgs...) } -// LessOrEqual asserts that the first element in greater or equal than the second +// LessOrEqual asserts that the first element is less than or equal to the second // // a.LessOrEqual(1, 2) // a.LessOrEqual(2, 2) @@ -664,7 +664,7 @@ func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...i return LessOrEqual(a.t, e1, e2, msgAndArgs...) } -// LessOrEqualf asserts that the first element in greater or equal than the second +// LessOrEqualf asserts that the first element is less than or equal to the second // // a.LessOrEqualf(1, 2, "error message %s", "formatted") // a.LessOrEqualf(2, 2, "error message %s", "formatted") @@ -677,7 +677,7 @@ func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, ar return LessOrEqualf(a.t, e1, e2, msg, args...) } -// Lessf asserts that the first element in less than the second +// Lessf asserts that the first element is less than the second // // a.Lessf(1, 2, "error message %s", "formatted") // a.Lessf(float64(1, "error message %s", "formatted"), float64(2)) diff --git a/assert/assertion_order.go b/assert/assertion_order.go index 8c4f347..15a486c 100644 --- a/assert/assertion_order.go +++ b/assert/assertion_order.go @@ -216,13 +216,13 @@ func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface } if res != -1 { - return Fail(t, fmt.Sprintf("\"%s\" is not greater than \"%s\"", e1, e2), msgAndArgs...) + return Fail(t, fmt.Sprintf("\"%v\" is not greater than \"%v\"", e1, e2), msgAndArgs...) } return true } -// GreaterOrEqual asserts that the first element in greater or equal than the second +// GreaterOrEqual asserts that the first element is greater than or equal to the second // // assert.GreaterOrEqual(t, 2, 1) // assert.GreaterOrEqual(t, 2, 2) @@ -245,13 +245,13 @@ func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...in } if res != -1 && res != 0 { - return Fail(t, fmt.Sprintf("\"%s\" is not greater or equal than \"%s\"", e1, e2), msgAndArgs...) + return Fail(t, fmt.Sprintf("\"%v\" is not greater than or equal to \"%v\"", e1, e2), msgAndArgs...) } return true } -// Less asserts that the first element in less than the second +// Less asserts that the first element is less than the second // // assert.Less(t, 1, 2) // assert.Less(t, float64(1), float64(2)) @@ -273,13 +273,13 @@ func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) } if res != 1 { - return Fail(t, fmt.Sprintf("\"%s\" is not less than \"%s\"", e1, e2), msgAndArgs...) + return Fail(t, fmt.Sprintf("\"%v\" is not less than \"%v\"", e1, e2), msgAndArgs...) } return true } -// LessOrEqual asserts that the first element in greater or equal than the second +// LessOrEqual asserts that the first element is less than or equal to the second // // assert.LessOrEqual(t, 1, 2) // assert.LessOrEqual(t, 2, 2) @@ -302,7 +302,7 @@ func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...inter } if res != 1 && res != 0 { - return Fail(t, fmt.Sprintf("\"%s\" is not less or equal than \"%s\"", e1, e2), msgAndArgs...) + return Fail(t, fmt.Sprintf("\"%v\" is not less than or equal to \"%v\"", e1, e2), msgAndArgs...) } return true diff --git a/require/require.go b/require/require.go index 5491907..73fedeb 100644 --- a/require/require.go +++ b/require/require.go @@ -403,7 +403,7 @@ func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface t.FailNow() } -// GreaterOrEqual asserts that the first element in greater or equal than the second +// GreaterOrEqual asserts that the first element is greater than or equal to the second // // assert.GreaterOrEqual(t, 2, 1) // assert.GreaterOrEqual(t, 2, 2) @@ -419,7 +419,7 @@ func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...in t.FailNow() } -// GreaterOrEqualf asserts that the first element in greater or equal than the second +// GreaterOrEqualf asserts that the first element is greater than or equal to the second // // assert.GreaterOrEqualf(t, 2, 1, "error message %s", "formatted") // assert.GreaterOrEqualf(t, 2, 2, "error message %s", "formatted") @@ -820,7 +820,7 @@ func Lenf(t TestingT, object interface{}, length int, msg string, args ...interf t.FailNow() } -// Less asserts that the first element in less than the second +// Less asserts that the first element is less than the second // // assert.Less(t, 1, 2) // assert.Less(t, float64(1), float64(2)) @@ -835,7 +835,7 @@ func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) t.FailNow() } -// LessOrEqual asserts that the first element in greater or equal than the second +// LessOrEqual asserts that the first element is less than or equal to the second // // assert.LessOrEqual(t, 1, 2) // assert.LessOrEqual(t, 2, 2) @@ -851,7 +851,7 @@ func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...inter t.FailNow() } -// LessOrEqualf asserts that the first element in greater or equal than the second +// LessOrEqualf asserts that the first element is less than or equal to the second // // assert.LessOrEqualf(t, 1, 2, "error message %s", "formatted") // assert.LessOrEqualf(t, 2, 2, "error message %s", "formatted") @@ -867,7 +867,7 @@ func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args . t.FailNow() } -// Lessf asserts that the first element in less than the second +// Lessf asserts that the first element is less than the second // // assert.Lessf(t, 1, 2, "error message %s", "formatted") // assert.Lessf(t, float64(1, "error message %s", "formatted"), float64(2)) diff --git a/require/require_forward.go b/require/require_forward.go index 1637c6c..2b81987 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -316,7 +316,7 @@ func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...inter Greater(a.t, e1, e2, msgAndArgs...) } -// GreaterOrEqual asserts that the first element in greater or equal than the second +// GreaterOrEqual asserts that the first element is greater than or equal to the second // // a.GreaterOrEqual(2, 1) // a.GreaterOrEqual(2, 2) @@ -329,7 +329,7 @@ func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs . GreaterOrEqual(a.t, e1, e2, msgAndArgs...) } -// GreaterOrEqualf asserts that the first element in greater or equal than the second +// GreaterOrEqualf asserts that the first element is greater than or equal to the second // // a.GreaterOrEqualf(2, 1, "error message %s", "formatted") // a.GreaterOrEqualf(2, 2, "error message %s", "formatted") @@ -640,7 +640,7 @@ func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...in Lenf(a.t, object, length, msg, args...) } -// Less asserts that the first element in less than the second +// Less asserts that the first element is less than the second // // a.Less(1, 2) // a.Less(float64(1), float64(2)) @@ -652,7 +652,7 @@ func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interfac Less(a.t, e1, e2, msgAndArgs...) } -// LessOrEqual asserts that the first element in greater or equal than the second +// LessOrEqual asserts that the first element is less than or equal to the second // // a.LessOrEqual(1, 2) // a.LessOrEqual(2, 2) @@ -665,7 +665,7 @@ func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...i LessOrEqual(a.t, e1, e2, msgAndArgs...) } -// LessOrEqualf asserts that the first element in greater or equal than the second +// LessOrEqualf asserts that the first element is less than or equal to the second // // a.LessOrEqualf(1, 2, "error message %s", "formatted") // a.LessOrEqualf(2, 2, "error message %s", "formatted") @@ -678,7 +678,7 @@ func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, ar LessOrEqualf(a.t, e1, e2, msg, args...) } -// Lessf asserts that the first element in less than the second +// Lessf asserts that the first element is less than the second // // a.Lessf(1, 2, "error message %s", "formatted") // a.Lessf(float64(1, "error message %s", "formatted"), float64(2)) From d84e815d441de9c64a3a2f9336c32acf1e7349d1 Mon Sep 17 00:00:00 2001 From: Grzegorz Miejski Date: Thu, 31 Jan 2019 01:33:30 +0100 Subject: [PATCH 2/2] Introduce Eventually assertion. --- assert/assertion_format.go | 11 +++++++++++ assert/assertion_forward.go | 22 ++++++++++++++++++++++ assert/assertions.go | 35 +++++++++++++++++++++++++++++++++-- assert/assertions_test.go | 22 ++++++++++++++++++++++ require/require.go | 28 ++++++++++++++++++++++++++++ require/require_forward.go | 22 ++++++++++++++++++++++ 6 files changed, 138 insertions(+), 2 deletions(-) diff --git a/assert/assertion_format.go b/assert/assertion_format.go index b9f1dca..e97c3d0 100644 --- a/assert/assertion_format.go +++ b/assert/assertion_format.go @@ -113,6 +113,17 @@ func Errorf(t TestingT, err error, msg string, args ...interface{}) bool { return Error(t, err, append([]interface{}{msg}, args...)...) } +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventuallyf(t, func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Eventually(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...) +} + // Exactlyf asserts that two objects are equal in value and type. // // assert.Exactlyf(t, int32(123, "error message %s", "formatted"), int64(123)) diff --git a/assert/assertion_forward.go b/assert/assertion_forward.go index de0863b..e11c070 100644 --- a/assert/assertion_forward.go +++ b/assert/assertion_forward.go @@ -215,6 +215,28 @@ func (a *Assertions) Errorf(err error, msg string, args ...interface{}) bool { return Errorf(a.t, err, msg, args...) } +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventually(func() bool { return true; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Eventually(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventuallyf(func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Eventuallyf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Eventuallyf(a.t, condition, waitFor, tick, msg, args...) +} + // Exactly asserts that two objects are equal in value and type. // // a.Exactly(int32(123), int64(123)) diff --git a/assert/assertions.go b/assert/assertions.go index eb3b39b..880e950 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -510,14 +510,14 @@ func isEmpty(object interface{}) bool { // collection types are empty when they have no element case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: return objValue.Len() == 0 - // pointers are empty if nil or if the value they point to is empty + // pointers are empty if nil or if the value they point to is empty case reflect.Ptr: if objValue.IsNil() { return true } deref := objValue.Elem().Interface() return isEmpty(deref) - // for all other types, compare against the zero value + // for all other types, compare against the zero value default: zero := reflect.Zero(objValue.Type()) return reflect.DeepEqual(object, zero.Interface()) @@ -1446,3 +1446,34 @@ var spewConfig = spew.ConfigState{ type tHelper interface { Helper() } + +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond) +func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + timer := time.NewTimer(waitFor) + ticker := time.NewTicker(tick) + checkPassed := make(chan bool) + defer timer.Stop() + defer ticker.Stop() + defer close(checkPassed) + for { + select { + case <-timer.C: + return Fail(t, "Condition never satisfied", msgAndArgs...) + case result := <-checkPassed: + if result { + return true + } + case <-ticker.C: + go func() { + checkPassed <- condition() + }() + } + } +} diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 332271e..b3981e2 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -1824,3 +1824,25 @@ func TestErrorAssertionFunc(t *testing.T) { }) } } + +func TestEventuallyFalse(t *testing.T) { + mockT := new(testing.T) + + condition := func() bool { + return false + } + + False(t, Eventually(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)) +} + +func TestEventuallyTrue(t *testing.T) { + state := 0 + condition := func() bool { + defer func() { + state = state + 1 + }() + return state == 2 + } + + True(t, Eventually(t, condition, 100*time.Millisecond, 20*time.Millisecond)) +} diff --git a/require/require.go b/require/require.go index 73fedeb..2c19547 100644 --- a/require/require.go +++ b/require/require.go @@ -270,6 +270,34 @@ func Errorf(t TestingT, err error, msg string, args ...interface{}) { t.FailNow() } +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond) +func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { + if assert.Eventually(t, condition, waitFor, tick, msgAndArgs...) { + return + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + t.FailNow() +} + +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventuallyf(t, func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) { + if assert.Eventuallyf(t, condition, waitFor, tick, msg, args...) { + return + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + t.FailNow() +} + // Exactly asserts that two objects are equal in value and type. // // assert.Exactly(t, int32(123), int64(123)) diff --git a/require/require_forward.go b/require/require_forward.go index 2b81987..19cedd1 100644 --- a/require/require_forward.go +++ b/require/require_forward.go @@ -216,6 +216,28 @@ func (a *Assertions) Errorf(err error, msg string, args ...interface{}) { Errorf(a.t, err, msg, args...) } +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventually(func() bool { return true; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + Eventually(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventuallyf(func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Eventuallyf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + Eventuallyf(a.t, condition, waitFor, tick, msg, args...) +} + // Exactly asserts that two objects are equal in value and type. // // a.Exactly(int32(123), int64(123))