Skip to content

Commit b533594

Browse files
committed
feat: allow the test function of Until does not have parameters.
1 parent 17a3cb8 commit b533594

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

until.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ func UntilWithContext(ctx context.Context, testFn, fn AsyncFn) ([]any, error) {
3232

3333
// until repeatedly calls the function until the test function returns true.
3434
func until(parent context.Context, testFn, fn AsyncFn) ([]any, error) {
35-
validateUntilFuncs(testFn, fn)
35+
isNoParam := validateUntilFuncs(testFn, fn)
3636

3737
ctx := getContext(parent)
3838

3939
for {
4040
out, _ := invokeAsyncFn(fn, ctx, nil)
4141

42-
testOut, testErr := invokeAsyncFn(testFn, ctx, out)
42+
params := out
43+
if isNoParam {
44+
params = nil
45+
}
46+
testOut, testErr := invokeAsyncFn(testFn, ctx, params)
4347
if testErr != nil {
4448
return out, testErr
4549
}
@@ -52,7 +56,7 @@ func until(parent context.Context, testFn, fn AsyncFn) ([]any, error) {
5256
}
5357

5458
// validateUntilFuncs validates the test function and the execution function.
55-
func validateUntilFuncs(testFn, fn AsyncFn) {
59+
func validateUntilFuncs(testFn, fn AsyncFn) (isNoParam bool) {
5660
if testFn == nil || fn == nil {
5761
panic(ErrNotFunction)
5862
}
@@ -74,9 +78,12 @@ func validateUntilFuncs(testFn, fn AsyncFn) {
7478
numIn--
7579
ii++
7680
}
77-
if numIn != ft.NumOut() {
81+
if numIn != 0 && numIn != ft.NumOut() {
7882
panic(ErrInvalidTestFunc)
7983
}
84+
if numIn == 0 {
85+
return true
86+
}
8087

8188
for oi < numIn {
8289
it := tft.In(ii) // type of the value in the test function input parameters list
@@ -89,4 +96,6 @@ func validateUntilFuncs(testFn, fn AsyncFn) {
8996
ii++
9097
oi++
9198
}
99+
100+
return false
92101
}

until_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ func TestUntilInvalidParameters(t *testing.T) {
4747
a.NotPanicNow(func() {
4848
Until(func(ctx context.Context, err error) bool { return true }, func() error { return nil })
4949
})
50+
a.NotPanicNow(func() {
51+
Until(func(ctx context.Context) bool { return true }, func() error { return nil })
52+
})
5053
a.PanicOfNow(func() {
5154
Until(func(ctx context.Context, i int) bool { return true }, func() error { return nil })
5255
}, ErrInvalidTestFunc)

0 commit comments

Comments
 (0)