|
| 1 | +package async |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ghosind/go-assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestWhile(t *testing.T) { |
| 13 | + a := assert.New(t) |
| 14 | + count := 0 |
| 15 | + |
| 16 | + out, err := While(func() bool { |
| 17 | + return count == 5 |
| 18 | + }, func() int { |
| 19 | + count++ |
| 20 | + return count |
| 21 | + }) |
| 22 | + a.NilNow(err) |
| 23 | + a.EqualNow(out, []any{5}) |
| 24 | +} |
| 25 | + |
| 26 | +func TestWhileInvalidParameters(t *testing.T) { |
| 27 | + a := assert.New(t) |
| 28 | + |
| 29 | + a.PanicOfNow(func() { |
| 30 | + While(nil, func() {}) |
| 31 | + }, ErrNotFunction) |
| 32 | + a.PanicOfNow(func() { |
| 33 | + While(func() {}, nil) |
| 34 | + }, ErrNotFunction) |
| 35 | + a.PanicOfNow(func() { |
| 36 | + While(1, "hello") |
| 37 | + }, ErrNotFunction) |
| 38 | + a.PanicOfNow(func() { |
| 39 | + While(func() {}, func() {}) |
| 40 | + }, ErrInvalidTestFunc) |
| 41 | + a.NotPanicNow(func() { |
| 42 | + While(func() bool { return true }, func() {}) |
| 43 | + }) |
| 44 | + a.NotPanicNow(func() { |
| 45 | + While(func(ctx context.Context) bool { return true }, func() {}) |
| 46 | + }) |
| 47 | + a.PanicOfNow(func() { |
| 48 | + While(func(ctx context.Context, i int) bool { return true }, func() {}) |
| 49 | + }, ErrInvalidTestFunc) |
| 50 | +} |
| 51 | + |
| 52 | +func TestWhileWithTestFunctionError(t *testing.T) { |
| 53 | + a := assert.New(t) |
| 54 | + expectedErr := errors.New("expected error") |
| 55 | + |
| 56 | + out, err := While(func() bool { |
| 57 | + panic(expectedErr) |
| 58 | + }, func() int { |
| 59 | + return 0 |
| 60 | + }) |
| 61 | + a.NotNilNow(err) |
| 62 | + a.EqualNow(err, expectedErr) |
| 63 | + a.EqualNow(out, []any{}) |
| 64 | +} |
| 65 | + |
| 66 | +func TestWhileWithFunctionError(t *testing.T) { |
| 67 | + a := assert.New(t) |
| 68 | + expectedErr := errors.New("expected error") |
| 69 | + |
| 70 | + out, err := While(func() bool { |
| 71 | + return false |
| 72 | + }, func() (int, error) { |
| 73 | + return 0, expectedErr |
| 74 | + }) |
| 75 | + a.NotNilNow(err) |
| 76 | + a.EqualNow(err, expectedErr) |
| 77 | + a.EqualNow(out, []any{0, expectedErr}) |
| 78 | +} |
| 79 | + |
| 80 | +func TestWhileWithContext(t *testing.T) { |
| 81 | + a := assert.New(t) |
| 82 | + ctx, canFunc := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 83 | + defer canFunc() |
| 84 | + |
| 85 | + start := time.Now() |
| 86 | + out, err := WhileWithContext(ctx, func(ctx context.Context) bool { |
| 87 | + select { |
| 88 | + case <-ctx.Done(): |
| 89 | + return true |
| 90 | + default: |
| 91 | + return false |
| 92 | + } |
| 93 | + }, func() { |
| 94 | + }) |
| 95 | + a.NilNow(err) |
| 96 | + a.EqualNow(out, []any{}) |
| 97 | + dur := time.Since(start) |
| 98 | + a.GteNow(dur, 100*time.Millisecond) |
| 99 | + a.LteNow(dur, 150*time.Millisecond) |
| 100 | +} |
0 commit comments