-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_base.py
More file actions
326 lines (281 loc) · 10.1 KB
/
Copy pathtest_base.py
File metadata and controls
326 lines (281 loc) · 10.1 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import traceback
from testtools import TestCase
from testtools.matchers import MatchesListwise
from ._base import Effect, NoPerformerFoundError, catch, perform
from ._test_utils import MatchesException, raise_
def func_dispatcher(intent):
"""
Simple effect dispatcher that takes callables taking a box,
and calls them with the given box.
"""
def performer(dispatcher, intent, box):
intent(box)
return performer
class EffectPerformTests(TestCase):
"""Tests for perform."""
def test_no_performer(self):
"""
When a dispatcher returns None, :class:`NoPerformerFoundError` is
provided as an error to the Effect's callbacks.
"""
dispatcher = lambda i: None
calls = []
intent = object()
eff = Effect(intent).on(error=calls.append)
perform(dispatcher, eff)
self.assertThat(
calls, MatchesListwise([MatchesException(NoPerformerFoundError(intent))])
)
def test_dispatcher(self):
"""
``perform`` calls the provided dispatcher, with the intent of the
provided effect. The returned perform is called with the dispatcher and
intent.
"""
calls = []
def dispatcher(intent):
calls.append(intent)
def performer(dispatcher, intent, box):
calls.append((dispatcher, intent))
return performer
intent = object()
perform(dispatcher, Effect(intent))
self.assertEquals(calls, [intent, (dispatcher, intent)])
def test_success_with_callback(self):
"""
perform uses the result given to the box as the argument of an
effect's callback
"""
calls = []
intent = lambda box: box.succeed("dispatched")
perform(func_dispatcher, Effect(intent).on(calls.append))
self.assertEqual(calls, ["dispatched"])
def test_error_with_callback(self):
"""
When an effect performer calls ``box.fail``, the exception is passed to
the error callback.
"""
calls = []
intent = lambda box: box.fail(ValueError("dispatched"))
perform(func_dispatcher, Effect(intent).on(error=calls.append))
self.assertThat(
calls, MatchesListwise([MatchesException(ValueError("dispatched"))])
)
def test_dispatcher_raises(self):
"""
When a dispatcher raises an exception, the exception is passed to the
error handler.
"""
calls = []
eff = Effect("meaningless").on(error=calls.append)
dispatcher = lambda i: raise_(ValueError("oh dear"))
perform(dispatcher, eff)
self.assertThat(
calls, MatchesListwise([MatchesException(ValueError("oh dear"))])
)
def test_performer_raises(self):
"""
When a performer raises an exception, it is passed to the
error handler.
"""
calls = []
eff = Effect("meaningless").on(error=calls.append)
performer = lambda d, i, box: raise_(ValueError("oh dear"))
dispatcher = lambda i: performer
perform(dispatcher, eff)
self.assertThat(
calls, MatchesListwise([MatchesException(ValueError("oh dear"))])
)
def test_success_propagates_effect_exception(self):
"""
If an success callback is specified, but a exception result occurs,
the exception is passed to the next callback.
"""
calls = []
intent = lambda box: box.fail(ValueError("dispatched"))
perform(
func_dispatcher,
Effect(intent)
.on(success=lambda box: calls.append("foo"))
.on(error=calls.append),
)
self.assertThat(
calls, MatchesListwise([MatchesException(ValueError("dispatched"))])
)
def test_error_propagates_effect_result(self):
"""
If an error callback is specified, but a successful result occurs,
the success is passed to the next callback.
"""
calls = []
intent = lambda box: box.succeed("dispatched")
perform(
func_dispatcher,
Effect(intent)
.on(error=lambda box: calls.append("foo"))
.on(success=calls.append),
)
self.assertEqual(calls, ["dispatched"])
def test_callback_success_exception(self):
"""
If a success callback raises an error, the exception is passed to the
error callback.
"""
calls = []
perform(
func_dispatcher,
Effect(lambda box: box.succeed("foo"))
.on(success=lambda _: raise_(ValueError("oh dear")))
.on(error=calls.append),
)
self.assertThat(
calls, MatchesListwise([MatchesException(ValueError("oh dear"))])
)
def test_callback_error_exception(self):
"""
If a error callback raises an error, the exception is passed to the
error callback.
"""
calls = []
intent = lambda box: box.fail(ValueError("dispatched"))
perform(
func_dispatcher,
Effect(intent)
.on(error=lambda _: raise_(ValueError("oh dear")))
.on(error=calls.append),
)
self.assertThat(
calls, MatchesListwise([MatchesException(ValueError("oh dear"))])
)
def test_effects_returning_effects(self):
"""
When the effect performer returns another effect,
- that effect is immediately performed with the same dispatcher,
- the result of that is returned.
"""
calls = []
perform(
func_dispatcher,
Effect(lambda box: box.succeed(Effect(lambda box: calls.append("foo")))),
)
self.assertEqual(calls, ["foo"])
def test_effects_returning_effects_returning_effects(self):
"""
If an effect returns an effect which immediately returns an effect
with no callbacks in between, the result of the innermost effect is
returned from the outermost effect's perform.
"""
calls = []
perform(
func_dispatcher,
Effect(
lambda box: box.succeed(
Effect(
lambda box: box.succeed(Effect(lambda box: calls.append("foo")))
)
)
),
)
self.assertEqual(calls, ["foo"])
def test_nested_effect_exception_passes_to_outer_error_handler(self):
"""
If an inner effect raises an exception, it bubbles up and the
exception is passed to the outer effect's error handlers.
"""
calls = []
intent = lambda box: box.fail(ValueError("oh dear"))
perform(
func_dispatcher,
Effect(lambda box: box.succeed(Effect(intent))).on(error=calls.append),
)
self.assertThat(
calls, MatchesListwise([MatchesException(ValueError("oh dear"))])
)
def test_bounced(self):
"""
The callbacks of a performer are called after the performer returns.
"""
calls = []
def out_of_order(box):
box.succeed("foo")
calls.append("bar")
perform(func_dispatcher, Effect(out_of_order).on(success=calls.append))
self.assertEqual(calls, ["bar", "foo"])
def test_callbacks_bounced(self):
"""
Multiple callbacks don't increase the stack depth.
"""
calls = []
def get_stack(_):
calls.append(traceback.extract_stack())
perform(
func_dispatcher,
Effect(lambda box: box.succeed(None))
.on(success=get_stack)
.on(success=get_stack),
)
self.assertEqual(calls[0], calls[1])
def test_effect_bounced(self):
"""
When an effect returns another effect, the effects are performed at the
same stack depth.
"""
calls = []
def get_stack(box):
calls.append(traceback.extract_stack())
box.succeed(None)
perform(
func_dispatcher, Effect(get_stack).on(success=lambda _: Effect(get_stack))
)
self.assertEqual(calls[0], calls[1])
def test_asynchronous_callback_invocation(self):
"""
When an Effect that is returned by a callback is resolved
*asynchronously*, the callbacks will run.
"""
results = []
boxes = []
eff = Effect(boxes.append).on(success=results.append)
perform(func_dispatcher, eff)
boxes[0].succeed("foo")
self.assertEqual(results, ["foo"])
def test_asynchronous_callback_bounced(self):
"""
When an Effect that is returned by a callback is resolved
*asynchronously*, the callbacks are performed at the same stack depth.
"""
calls = []
def get_stack(_):
calls.append(traceback.extract_stack())
boxes = []
eff = Effect(boxes.append).on(success=get_stack).on(success=get_stack)
perform(func_dispatcher, eff)
boxes[0].succeed("foo")
self.assertEqual(calls[0], calls[1])
class CatchTests(TestCase):
"""Tests for :func:`catch`."""
def test_caught(self):
"""
When the exception type matches the type of the raised exception, the
callable is invoked and its result is returned.
"""
try:
raise RuntimeError("foo")
except Exception as e:
error = e
result = catch(RuntimeError, lambda e: ("caught", e))(error)
self.assertEqual(result, ("caught", error))
def test_missed(self):
"""
When the exception type does not match the type of the raised exception,
the callable is not invoked and the original exception is reraised.
"""
try:
raise ZeroDivisionError("foo")
except Exception as e:
error = e
e = self.assertRaises(
ZeroDivisionError,
lambda: catch(RuntimeError, lambda e: ("caught", e))(error),
)
self.assertEqual(str(e), "foo")