Skip to content

Commit 04aa750

Browse files
committed
Address @cyli's feedback
1 parent d160e4f commit 04aa750

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

effect/test_base.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import print_function, absolute_import
22

3+
import sys
34
import traceback
45

56
from testtools import TestCase
67
from testtools.matchers import MatchesException, MatchesListwise
78

8-
from ._base import Effect, NoPerformerFoundError, perform
9+
from ._base import Effect, NoPerformerFoundError, catch, perform
910
from ._test_utils import raise_
1011

1112

@@ -288,3 +289,34 @@ def get_stack(_):
288289
perform(func_dispatcher, eff)
289290
boxes[0].succeed('foo')
290291
self.assertEqual(calls[0], calls[1])
292+
293+
294+
class CatchTests(TestCase):
295+
"""Tests for :func:`catch`."""
296+
297+
def test_caught(self):
298+
"""
299+
When the exception type matches the type in the ``exc_info`` tuple, the
300+
callable is invoked and its result is returned.
301+
"""
302+
try:
303+
raise RuntimeError('foo')
304+
except:
305+
exc_info = sys.exc_info()
306+
result = catch(RuntimeError, lambda e: ('caught', e))(exc_info)
307+
self.assertEqual(result, ('caught', exc_info))
308+
309+
def test_missed(self):
310+
"""
311+
When the exception type does not match the type in the ``exc_info``
312+
tuple, the callable is not invoked and the original exception is
313+
reraised.
314+
"""
315+
try:
316+
raise ZeroDivisionError('foo')
317+
except:
318+
exc_info = sys.exc_info()
319+
e = self.assertRaises(
320+
ZeroDivisionError,
321+
lambda: catch(RuntimeError, lambda e: ('caught', e))(exc_info))
322+
self.assertEqual(str(e), 'foo')

effect/testing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ class EQFDispatcher(object):
207207
208208
Users provide a mapping of intents to functions, where the intents are
209209
matched against the intents being performed with a simple equality check
210-
(not a type check!).
210+
(not a type check!). The functions in the mapping will be passed only the
211+
intent and are expected to return the result or raise an exception.
211212
212213
e.g.::
213214

0 commit comments

Comments
 (0)