-
Notifications
You must be signed in to change notification settings - Fork 0
/
chan.go
59 lines (54 loc) · 1.15 KB
/
chan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package assert
import (
"fmt"
"testing"
)
// TODO: find a way to support receive/send only chans. (more functions or use type parameters ?)
// ChanEmpty asserts that c is empty.
//
//nolint:thelper // It's called below.
func ChanEmpty[T any](tb testing.TB, c chan T, opts ...Option) bool {
ok := len(c) == 0
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("chan_empty[%s]", typeName[T]()),
fmt.Sprintf("not empty:\nlength = %d", len(c)),
opts...,
)
}
return ok
}
// ChanNotEmpty asserts that c is not empty.
//
//nolint:thelper // It's called below.
func ChanNotEmpty[T any](tb testing.TB, c chan T, opts ...Option) bool {
ok := len(c) != 0
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("chan_not_empty[%s]", typeName[T]()),
"empty",
opts...,
)
}
return ok
}
// ChanLen asserts that c has length l.
//
//nolint:thelper // It's called below.
func ChanLen[T any](tb testing.TB, c chan T, l int, opts ...Option) bool {
ok := len(c) == l
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("chan_len[%s]", typeName[T]()),
fmt.Sprintf("unexpected length:\nexpected = %d\nactual = %d", l, len(c)),
opts...,
)
}
return ok
}