-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipe_test.go
90 lines (81 loc) · 1.96 KB
/
pipe_test.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package pipe
import (
"errors"
"reflect"
"testing"
)
func TestPipe(t *testing.T) {
type input struct {
val int
}
permittedErr := errors.New("permitted error")
cases := []struct {
description string
initFns []func(*input)
actions []actionFunc[input]
opts [][]func(cfg *runCfg)
want input
}{
{
description: "no actions",
actions: []actionFunc[input]{},
want: input{val: 0},
},
{
description: "Initializer",
initFns: []func(*input){func(i *input) { i.val = 1 }},
want: input{val: 1},
},
{
description: "All functions run till the end",
actions: []actionFunc[input]{
func(i *input) error { i.val = 1; return nil },
func(i *input) error { i.val++; return nil },
func(i *input) error { i.val *= 2; return nil },
},
want: input{val: 4},
},
{
description: "An error stops the pipe",
actions: []actionFunc[input]{
func(i *input) error { i.val = 1; return nil },
func(i *input) error { return errors.New("Oops") },
func(i *input) error { i.val *= 2; return nil },
},
want: input{val: 1},
},
{
description: "A permitted error does not stop the pipe",
actions: []actionFunc[input]{
func(i *input) error { i.val = 1; return nil },
func(i *input) error { return permittedErr },
func(i *input) error { i.val *= 2; return nil },
},
want: input{val: 2},
opts: [][]func(opts *runCfg){
1: {
PermitErrors(permittedErr),
},
},
},
}
for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
p := New(c.initFns...)
for idx, f := range c.actions {
var opts []func(cfg *runCfg)
if len(c.opts) > idx && c.opts[idx] != nil {
opts = c.opts[idx]
}
p.Next(f, opts...)
}
out, err := p.Do()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if got, want := out, c.want; reflect.DeepEqual(got, want) == false {
t.Errorf("got %v, want %v", got, want)
}
})
}
}