-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_middleware.go
66 lines (60 loc) · 1.6 KB
/
check_middleware.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
60
61
62
63
64
65
66
package subtest
import (
"fmt"
)
// OnFloat64 returns a check function where the test value is converted to
// float64 before it's passed to c.
func OnFloat64(c Check) CheckFunc {
return func(got interface{}) error {
err := c.Check(Float64(got))
if err != nil {
return fmt.Errorf("on float64: %w", err)
}
return nil
}
}
// OnLen returns a check function where the length of the test value is
// extracted and passed to c. Accepted input types are arrays, slices, maps,
// channels and strings.
func OnLen(c Check) CheckFunc {
return func(got interface{}) error {
err := c.Check(Len(got))
if err != nil {
return fmt.Errorf("on len: %w", err)
}
return nil
}
}
// OnCap returns a check function where the capacity of the test value is
// extracted and passed to c. Accepted input types are arrays, slices and
// channels.
func OnCap(c Check) CheckFunc {
return func(got interface{}) error {
err := c.Check(Cap(got))
if err != nil {
return fmt.Errorf("on cap: %w", err)
}
return nil
}
}
// OnIndex returns a check function where the item at index i of the test value
// is passed on to c. Accepted input types are arrays, slices and strings.
func OnIndex(i int, c Check) CheckFunc {
return func(got interface{}) error {
vf := Index(got, i)
err := c.Check(vf)
if err != nil {
return fmt.Errorf("on index %d: %w", i, err)
}
return nil
}
}
// Iterate runs the first check on index 0, the second on index 1 etc, and
// returns an aggregated error.
func Iterate(cs ...Check) Check {
all := make(AllOf, 0, len(cs))
for i, c := range cs {
all = append(all, OnIndex(i, c))
}
return all
}