|
1 | 1 | from __future__ import print_function, absolute_import |
2 | 2 |
|
| 3 | +import sys |
3 | 4 | import traceback |
4 | 5 |
|
5 | 6 | from testtools import TestCase |
6 | 7 | from testtools.matchers import MatchesException, MatchesListwise |
7 | 8 |
|
8 | | -from ._base import Effect, NoPerformerFoundError, perform |
| 9 | +from ._base import Effect, NoPerformerFoundError, catch, perform |
9 | 10 | from ._test_utils import raise_ |
10 | 11 |
|
11 | 12 |
|
@@ -288,3 +289,34 @@ def get_stack(_): |
288 | 289 | perform(func_dispatcher, eff) |
289 | 290 | boxes[0].succeed('foo') |
290 | 291 | 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') |
0 commit comments