Skip to content

Commit

Permalink
Merge pull request #1 from arikkfir/eventually-with-assertions-no-return
Browse files Browse the repository at this point in the history
Change "EventuallyWithT" condition acceptance to no-errors raised
  • Loading branch information
tobikris authored Oct 24, 2022
2 parents 15ae495 + 0c7cc6b commit f9122dc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 171 deletions.
31 changes: 11 additions & 20 deletions assert/assertion_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,21 @@ func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// assert.EventuallyWithTf(t, func(mockT *assert.CollectT) (success bool, "error message %s", "formatted") {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func EventuallyWithTf(t TestingT, condition func(collect *CollectT) bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// assert.EventuallyWithTf(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still: %v", externalState)
func EventuallyWithTf(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
Expand Down
62 changes: 22 additions & 40 deletions assert/assertion_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,30 +292,21 @@ func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, ti
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// a.EventuallyWithT(func(mockT *assert.CollectT) (success bool) {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func (a *Assertions) EventuallyWithT(condition func(collect *CollectT) bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// a.EventuallyWithT(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")
func (a *Assertions) EventuallyWithT(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
Expand All @@ -326,30 +317,21 @@ func (a *Assertions) EventuallyWithT(condition func(collect *CollectT) bool, wai
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// a.EventuallyWithTf(func(mockT *assert.CollectT) (success bool, "error message %s", "formatted") {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func (a *Assertions) EventuallyWithTf(condition func(collect *CollectT) bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// a.EventuallyWithTf(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still: %v", externalState)
func (a *Assertions) EventuallyWithTf(condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
Expand Down
39 changes: 18 additions & 21 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,9 @@ func (c *CollectT) Reset() {

// Copy copies the collected errors to the supplied t.
func (c *CollectT) Copy(t TestingT) {
if tt, ok := t.(tHelper); ok {
tt.Helper()
}
for _, err := range c.errors {
t.Errorf("%v", err)
}
Expand All @@ -1787,30 +1790,21 @@ func (c *CollectT) Copy(t TestingT) {
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// assert.EventuallyWithT(t, func(mockT *assert.CollectT) (success bool) {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func EventuallyWithT(t TestingT, condition func(collect *CollectT) bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// assert.EventuallyWithT(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")
func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
Expand All @@ -1832,7 +1826,10 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT) bool, waitFor
case <-tick:
tick = nil
collect.Reset()
go func() { ch <- condition(collect) }()
go func() {
condition(collect)
ch <- len(collect.errors) == 0
}()
case v := <-ch:
if v {
return true
Expand Down
14 changes: 4 additions & 10 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2431,8 +2431,8 @@ func TestEventuallyTrue(t *testing.T) {
func TestEventuallyWithTFalse(t *testing.T) {
mockT := new(CollectT)

condition := func(collect *CollectT) bool {
return True(collect, false)
condition := func(collect *CollectT) {
True(collect, false)
}

False(t, EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
Expand All @@ -2443,17 +2443,11 @@ func TestEventuallyWithTTrue(t *testing.T) {
mockT := new(CollectT)

state := 0
condition := func(collect *CollectT) bool {
condition := func(collect *CollectT) {
defer func() {
state += 1
}()

if state == 2 {
True(collect, true)
return true
}

return True(collect, false)
True(collect, state == 2)
}

True(t, EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
Expand Down
62 changes: 22 additions & 40 deletions require/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,30 +368,21 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// assert.EventuallyWithT(t, func(mockT *assert.CollectT) (success bool) {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func EventuallyWithT(t TestingT, condition func(collect *assert.CollectT) bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// require.EventuallyWithT(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")
func EventuallyWithT(t TestingT, condition func(collect *assert.CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
Expand All @@ -405,30 +396,21 @@ func EventuallyWithT(t TestingT, condition func(collect *assert.CollectT) bool,
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// assert.EventuallyWithTf(t, func(mockT *assert.CollectT) (success bool, "error message %s", "formatted") {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func EventuallyWithTf(t TestingT, condition func(collect *assert.CollectT) bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// require.EventuallyWithTf(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still: %v", externalState)
func EventuallyWithTf(t TestingT, condition func(collect *assert.CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
Expand Down
62 changes: 22 additions & 40 deletions require/require_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,30 +293,21 @@ func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, ti
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// a.EventuallyWithT(func(mockT *assert.CollectT) (success bool) {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func (a *Assertions) EventuallyWithT(condition func(collect *assert.CollectT) bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// require.EventuallyWithT(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still false")
func (a *Assertions) EventuallyWithT(condition func(collect *assert.CollectT), waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
Expand All @@ -327,30 +318,21 @@ func (a *Assertions) EventuallyWithT(condition func(collect *assert.CollectT) bo
// periodically checking target function each tick. In contrast to Eventually,
// it supplies a CollectT to the condition function, so that the condition
// function can use the CollectT to call other assertions.
// The condition is considered "met" if no errors are raised in a tick.
// The supplied CollectT collects all errors from one tick (if there are any).
// If the condition is not met before waitFor, the collected errors of
// the last tick are copied to t.
//
// falseThenTrue := func(falses int) func() bool {
// count := 0
// return func() bool {
// if count < falses {
// count++
// return false
// }
// return true
// }
// }
// f := falseThenTrue(5)
// a.EventuallyWithTf(func(mockT *assert.CollectT) (success bool, "error message %s", "formatted") {
// defer func() {
// r := recover()
// success = (r == nil)
// }()
// assert.True(mockT, f())
// return
// }, 50*time.Millisecond, 10*time.Millisecond)
func (a *Assertions) EventuallyWithTf(condition func(collect *assert.CollectT) bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) {
// externalValue := false
// go func() {
// time.Sleep(8*time.Second)
// externalValue = true
// }()
// require.EventuallyWithTf(t, func(c *assert.CollectT) {
// // add assertions as needed; any assertion failure will fail the current tick
// assert.True(c, externalValue, "expected 'externalValue' to be true")
// }, 1*time.Second, 10*time.Second, "external state has not changed to 'true'; still: %v", externalState)
func (a *Assertions) EventuallyWithTf(condition func(collect *assert.CollectT), waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) {
if h, ok := a.t.(tHelper); ok {
h.Helper()
}
Expand Down

0 comments on commit f9122dc

Please sign in to comment.