-
Notifications
You must be signed in to change notification settings - Fork 18
/
run_test.go
150 lines (116 loc) · 3.67 KB
/
run_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package floc
import (
"fmt"
"testing"
)
func TestRun_NilJob(t *testing.T) {
result, data, err := Run(nil)
if result.IsNone() == false {
t.Fatalf("%s expects result to be None but has %s", t.Name(), result.String())
}
if data != nil {
t.Fatalf("%s expects data to be nil but has %v", t.Name(), data)
}
if err == nil {
t.Fatalf("%s expects error to be not nil", t.Name())
}
}
func TestRun_None(t *testing.T) {
flow := func(Context, Control) error {
return nil
}
result, data, err := Run(flow)
if result.IsNone() == false {
t.Fatalf("%s expects result to be None but has %s", t.Name(), result.String())
}
if data != nil {
t.Fatalf("%s expects data to be nil but has %v", t.Name(), data)
}
if err != nil {
t.Fatalf("%s expects error to be nil but has %s", t.Name(), err.Error())
}
}
func TestRun_Completed(t *testing.T) {
const tpl int = 1
flow := func(ctx Context, ctrl Control) error {
ctrl.Complete(tpl)
return nil
}
result, data, err := Run(flow)
if result.IsCompleted() == false {
t.Fatalf("%s expects result to be Completed but has %s", t.Name(), result.String())
}
if data == nil {
t.Fatalf("%s expects data to be not nil", t.Name())
} else if d, ok := data.(int); !ok {
t.Fatalf("%s expects data to be of type int but has %T", t.Name(), data)
} else if d != tpl {
t.Fatalf("%s expects data to be %d but has %d", t.Name(), tpl, d)
}
if err != nil {
t.Fatalf("%s expects error to be nil but has %s", t.Name(), err.Error())
}
}
func TestRun_Canceled(t *testing.T) {
const tpl float64 = 3.1415
flow := func(ctx Context, ctrl Control) error {
ctrl.Cancel(tpl)
return nil
}
result, data, err := Run(flow)
if result.IsCanceled() == false {
t.Fatalf("%s expects result to be Canceled but has %s", t.Name(), result.String())
}
if data == nil {
t.Fatalf("%s expects data to be not nil", t.Name())
} else if f, ok := data.(float64); !ok {
t.Fatalf("%s expects data to be of type float64 but has %T", t.Name(), data)
} else if f < tpl-0.0001 || f > tpl+0.0001 {
t.Fatalf("%s expects data to be %f but has %f", t.Name(), tpl, f)
}
if err != nil {
t.Fatalf("%s expects error to be nil but has %s", t.Name(), err.Error())
}
}
func TestRun_Failed(t *testing.T) {
const tplData string = "REASON"
var tplError error = fmt.Errorf("failed because of %s", tplData)
flow := func(ctx Context, ctrl Control) error {
ctrl.Fail(tplData, tplError)
return nil
}
result, data, err := Run(flow)
if result.IsFailed() == false {
t.Fatalf("%s expects result to be Failed but has %s", t.Name(), result.String())
}
if data == nil {
t.Fatalf("%s expects data to be not nil", t.Name())
} else if s, ok := data.(string); !ok {
t.Fatalf("%s expects data to be of type string but has %T", t.Name(), data)
} else if s != tplData {
t.Fatalf("%s expects data to be %s but has %s", t.Name(), tplData, s)
}
if err == nil {
t.Fatalf("%s expects error to be not nil", t.Name())
} else if err.Error() != tplError.Error() {
t.Fatalf("%s expects error message to be %s but has %s", t.Name(), tplError.Error(), err.Error())
}
}
func TestRun_UnhandledError(t *testing.T) {
var tplError error = fmt.Errorf("something happened")
flow := func(ctx Context, ctrl Control) error {
return tplError
}
result, data, err := Run(flow)
if result.IsFailed() == false {
t.Fatalf("%s expects result to be Failed but has %s", t.Name(), result.String())
}
if data != nil {
t.Fatalf("%s expects data to be nil but has %v", t.Name(), data)
}
if err == nil {
t.Fatalf("%s expects error to be not nil", t.Name())
} else if err.Error() != tplError.Error() {
t.Fatalf("%s expects error message to be %s but has %s", t.Name(), tplError.Error(), err.Error())
}
}