-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchaining_test.go
205 lines (180 loc) · 4.53 KB
/
chaining_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package go_promise
import (
"errors"
"testing"
)
func TestThen(t *testing.T) {
t.Run("it should return float64 from int", func(t *testing.T) {
promise := Function(func() (int, error) {
return 10, nil
}).With(Then(func(value int) (float64, error) {
return float64(value), nil
}))
result, err := Await[float64](promise)
if err != nil {
t.Error("error is not expected")
}
if result != 10.0 {
t.Error("result is not 10")
}
})
t.Run("it should reject with error", func(t *testing.T) {
expected := errors.New("error")
promise := Function(func() (int, error) {
return 10, nil
}).With(Then(func(value int) (float64, error) {
return 0, expected
}))
result, err := Await[float64](promise)
if err != expected {
t.Error("error is not as expected")
}
if result != 0.0 {
t.Error("result is not 0")
}
})
t.Run("it should reject with invalid type from then method", func(t *testing.T) {
promise := Function(func() (int, error) {
return 10, nil
}).With(Then(func(value float64) (float64, error) {
return value, nil
}))
result, err := Await[float64](promise)
if err != InvalidTypeErr {
t.Error("error is not as expected")
}
if result != 0.0 {
t.Error("result is not 0")
}
})
t.Run("it should reject with invalid type from wrong final casting", func(t *testing.T) {
promise := Function(func() (int, error) {
return 10, nil
}).With(Then(func(value int) (float64, error) {
return float64(value), nil
}))
result, err := Await[int](promise)
if err != InvalidTypeErr {
t.Error("error is not as expected")
}
if result != 0.0 {
t.Error("result is not 0")
}
})
t.Run("it should go through second then", func(t *testing.T) {
expected := errors.New("expected")
passed := false
promise := Function(func() (int, error) {
return 0, expected
}).With(Then(func(value int) (float64, error) {
passed = true
return 10, nil
}))
result, err := Await[int](promise)
if err != expected {
t.Error("error is not as expected")
}
if result != 0 {
t.Error("result is not 0")
}
if passed {
t.Error("then block was not meant to be called")
}
})
}
func TestCatch(t *testing.T) {
t.Run("it should not be catch", func(t *testing.T) {
promise := Function(func() (int, error) {
return 20, nil
}).With(Catch(func(error) int {
return 10
}))
result, err := Await[int](promise)
if err != nil {
t.Error("error is not expected")
}
if result != 20 {
t.Error("result is not 20")
}
})
t.Run("it should recover from the error", func(t *testing.T) {
promise := Function(func() (int, error) {
return 0, errors.New("error")
}).With(Catch(func(error) int {
return 10
}))
result, err := Await[int](promise)
if err != nil {
t.Error("error is not expected")
}
if result != 10 {
t.Error("result is not 10")
}
})
t.Run("it should fail internal casting", func(t *testing.T) {
promise := Function(func() (int, error) {
return 10, nil
}).With(Catch(func(error) float64 {
return 10
}))
result, err := Await[float64](promise)
if err != InvalidTypeErr {
t.Error("error is expected")
}
if result != 0 {
t.Error("result is not 0")
}
})
t.Run("it should fail casting", func(t *testing.T) {
promise := Function(func() (int, error) {
return 0, errors.New("error")
}).With(Catch(func(error) int {
return 10
}))
result, err := Await[float64](promise)
if err != InvalidTypeErr {
t.Error("error is expected")
}
if result != 0 {
t.Error("result is not 0")
}
})
}
func TestChaining(t *testing.T) {
t.Run("it should go through catch", func(t *testing.T) {
promise := Function(func() (int, error) {
return 10, nil
}).With(Then(func(value int) (float64, error) {
return 0, errors.New("error")
})).With(Catch(func(err error) float64 {
return 11.1
})).With(Then(func(value float64) (bool, error) {
return value > 11, nil
}))
value, err := Await[bool](promise)
if err != nil {
t.Error("error is not expected")
}
if value != true {
t.Error("value is not true")
}
})
t.Run("it should not go through catch", func(t *testing.T) {
promise := Function(func() (int, error) {
return 10, nil
}).With(Then(func(value int) (float64, error) {
return float64(value), nil
})).With(Catch(func(err error) float64 {
return 11.1
})).With(Then(func(value float64) (bool, error) {
return value > 10 && value < 10, nil
}))
value, err := Await[bool](promise)
if err != nil {
t.Error("error is not expected")
}
if value != false {
t.Error("value is not false")
}
})
}