generated from golang-templates/seed
/
flow_main_test.go
66 lines (60 loc) · 1.29 KB
/
flow_main_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
package goyek
import (
"context"
"io/ioutil"
"strings"
"testing"
)
func TestFlow_main(t *testing.T) {
flow := &Flow{}
flow.SetOutput(&strings.Builder{})
flow.Define(Task{Name: "task"})
flow.Define(Task{Name: "failing", Action: func(a *A) { a.Fail() }})
testCases := []struct {
desc string
want int
act func() int
}{
{
desc: "pass",
want: 0,
act: func() int { return flow.main(context.Background(), []string{"task"}) },
},
{
desc: "fail",
want: 1,
act: func() int { return flow.main(context.Background(), []string{"failing"}) },
},
{
desc: "invalid",
want: 2,
act: func() int { return flow.main(context.Background(), []string{"bad"}) },
},
{
desc: "canceled",
want: 1,
act: func() int {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return flow.main(ctx, []string{"task"})
},
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
if got := tc.act(); got != tc.want {
t.Errorf("got: %d; want: %d", got, tc.want)
}
})
}
}
func Test_main_usage(t *testing.T) {
flow := &Flow{}
flow.SetOutput(ioutil.Discard)
called := false
flow.SetUsage(func() { called = true })
flow.main(context.Background(), nil)
if !called {
t.Error("usage should be called for invalid input")
}
}