-
Notifications
You must be signed in to change notification settings - Fork 0
/
belt_test.go
173 lines (143 loc) · 3 KB
/
belt_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package orrer
import (
"fmt"
"testing"
"time"
)
func TestGetAny(t *testing.T) {
msg1 := &Result{}
msg2 := &Result{}
msg3 := &Result{}
err1 := hello("You", msg1)
err2 := bye("You", msg2)
if GetAny(err1, err2) != nil {
t.Fail()
}
err3 := errrr("You", msg3)
if GetAny(err1, err2, err3) == nil {
t.Fail()
}
}
type Result struct {
Message string
}
func hello(name string, r *Result) error {
r.Message = fmt.Sprintf("Hello %s", name)
return nil
}
func bye(name string, r *Result) error {
r.Message = fmt.Sprintf("bye %s", name)
return nil
}
func errrr(name string, r *Result) error {
return fmt.Errorf("Et errorum, ergo sum")
}
// ------------------------------------------------------------------------------------
func TestGetValsOrError(t *testing.T) {
v, err := GetValsOrError(
func() (interface{}, error) {
return sayHi("John")
},
func() (interface{}, error) {
return sayBye("Jona")
},
func() (interface{}, error) {
return sayBye("David")
},
func() (interface{}, error) {
return sayHi("Jake")
},
func() (interface{}, error) {
return sayErr("Javad")
},
)
if err == nil || v != nil {
t.Fail()
}
v, err = GetValsOrError(
func() (interface{}, error) {
return sayHi("John")
},
func() (interface{}, error) {
return sayBye("Jona")
},
func() (interface{}, error) {
return sayBye("David")
},
func() (interface{}, error) {
return sayHi("Jake")
},
)
if err != nil {
t.Fail()
}
if v[0].(string) != "Hello John" {
t.Fail()
}
}
func sayHi(name string) (string, error) {
return fmt.Sprintf("Hello %s", name), nil
}
func sayBye(name string) (string, error) {
return fmt.Sprintf("Bye %s", name), nil
}
func sayErr(name string) (string, error) {
return "", fmt.Errorf("Something went wrong")
}
// ------------------------------------------------------------------------------------
func TestGoGetValsOrError(t *testing.T) {
v, err := GoGetValsOrError(
func() (interface{}, error) {
time.Sleep(5 * time.Millisecond)
return sayHi("John")
},
func() (interface{}, error) {
time.Sleep(4 * time.Millisecond)
return sayBye("Jona")
},
func() (interface{}, error) {
time.Sleep(3 * time.Millisecond)
return sayBye("David")
},
func() (interface{}, error) {
time.Sleep(1 * time.Millisecond)
return sayHi("Jake")
},
)
if err != nil {
t.Fail()
}
if v[0].(string) != "Hello John" {
t.Fail()
}
}
// ------------------------------------------------------------------------------------
func TestGetSeriesOrError(t *testing.T) {
v, err := GetSeriesOrError(
1,
func(i interface{}) (interface{}, error) {
return AddOne(i.(int))
},
func(i interface{}) (interface{}, error) {
return MulBy3(i.(int))
},
func(i interface{}) (interface{}, error) {
return AddTwo(i.(int))
},
)
if err != nil {
t.Fail()
}
if v != 8 {
t.Fail()
}
}
func AddOne(inp int) (int, error) {
return inp + 1, nil
}
func AddTwo(inp int) (int, error) {
return inp + 2, nil
}
func MulBy3(inp int) (int, error) {
return inp * 3, nil
}