|
| 1 | +"""Tests for exception objects — adapted from CPython test_baseexception.py""" |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | + |
| 6 | +class ExceptionClassTests(unittest.TestCase): |
| 7 | + |
| 8 | + def test_builtins_new_style(self): |
| 9 | + self.assertTrue(issubclass(Exception, object)) |
| 10 | + |
| 11 | + def test_interface_single_arg(self): |
| 12 | + arg = "spam" |
| 13 | + exc = Exception(arg) |
| 14 | + self.assertEqual(len(exc.args), 1) |
| 15 | + self.assertEqual(exc.args[0], arg) |
| 16 | + self.assertEqual(str(exc), arg) |
| 17 | + |
| 18 | + def test_interface_multi_arg(self): |
| 19 | + args = (1, 2, 3) |
| 20 | + exc = Exception(*args) |
| 21 | + self.assertEqual(len(exc.args), 3) |
| 22 | + self.assertEqual(exc.args, args) |
| 23 | + |
| 24 | + def test_interface_no_arg(self): |
| 25 | + exc = Exception() |
| 26 | + self.assertEqual(len(exc.args), 0) |
| 27 | + self.assertEqual(exc.args, ()) |
| 28 | + |
| 29 | + def test_exception_hierarchy(self): |
| 30 | + # Basic hierarchy checks |
| 31 | + self.assertTrue(issubclass(Exception, BaseException)) |
| 32 | + self.assertTrue(issubclass(TypeError, Exception)) |
| 33 | + self.assertTrue(issubclass(ValueError, Exception)) |
| 34 | + self.assertTrue(issubclass(KeyError, Exception)) |
| 35 | + self.assertTrue(issubclass(IndexError, Exception)) |
| 36 | + self.assertTrue(issubclass(AttributeError, Exception)) |
| 37 | + self.assertTrue(issubclass(NameError, Exception)) |
| 38 | + self.assertTrue(issubclass(RuntimeError, Exception)) |
| 39 | + self.assertTrue(issubclass(StopIteration, Exception)) |
| 40 | + self.assertTrue(issubclass(ZeroDivisionError, Exception)) |
| 41 | + self.assertTrue(issubclass(ImportError, Exception)) |
| 42 | + self.assertTrue(issubclass(OverflowError, Exception)) |
| 43 | + self.assertTrue(issubclass(KeyboardInterrupt, BaseException)) |
| 44 | + |
| 45 | + def test_exception_subclass(self): |
| 46 | + self.assertTrue(issubclass(KeyError, LookupError)) |
| 47 | + self.assertTrue(issubclass(IndexError, LookupError)) |
| 48 | + self.assertTrue(issubclass(NotImplementedError, RuntimeError)) |
| 49 | + self.assertTrue(issubclass(UnboundLocalError, NameError)) |
| 50 | + |
| 51 | + |
| 52 | +class UsageTests(unittest.TestCase): |
| 53 | + |
| 54 | + def raise_fails(self, object_): |
| 55 | + try: |
| 56 | + raise object_ |
| 57 | + except TypeError: |
| 58 | + return |
| 59 | + self.fail("TypeError expected for raising %s" % type(object_)) |
| 60 | + |
| 61 | + def test_raise_non_exception_class(self): |
| 62 | + class NotAnException: |
| 63 | + pass |
| 64 | + self.raise_fails(NotAnException) |
| 65 | + |
| 66 | + @unittest.skip("raise non-exception instance segfaults") |
| 67 | + def test_raise_string(self): |
| 68 | + self.raise_fails("spam") |
| 69 | + |
| 70 | + @unittest.skip("raise non-exception instance segfaults") |
| 71 | + def test_raise_int(self): |
| 72 | + self.raise_fails(42) |
| 73 | + |
| 74 | + def test_catch_specific(self): |
| 75 | + try: |
| 76 | + raise ValueError("test") |
| 77 | + except ValueError as e: |
| 78 | + self.assertEqual(str(e), "test") |
| 79 | + else: |
| 80 | + self.fail("ValueError not caught") |
| 81 | + |
| 82 | + def test_catch_base_class(self): |
| 83 | + try: |
| 84 | + raise ValueError("val") |
| 85 | + except Exception: |
| 86 | + pass |
| 87 | + else: |
| 88 | + self.fail("Exception didn't catch ValueError") |
| 89 | + |
| 90 | + def test_catch_tuple(self): |
| 91 | + try: |
| 92 | + raise KeyError("key") |
| 93 | + except (ValueError, KeyError): |
| 94 | + pass |
| 95 | + else: |
| 96 | + self.fail("tuple catch failed") |
| 97 | + |
| 98 | + def test_exception_args(self): |
| 99 | + try: |
| 100 | + raise ValueError("a", "b", "c") |
| 101 | + except ValueError as e: |
| 102 | + self.assertEqual(e.args, ("a", "b", "c")) |
| 103 | + |
| 104 | + def test_custom_exception(self): |
| 105 | + class MyError(Exception): |
| 106 | + def __init__(self, code): |
| 107 | + self.code = code |
| 108 | + try: |
| 109 | + raise MyError(404) |
| 110 | + except MyError as e: |
| 111 | + self.assertEqual(e.code, 404) |
| 112 | + |
| 113 | + def test_exception_chaining_basic(self): |
| 114 | + try: |
| 115 | + try: |
| 116 | + raise ValueError("original") |
| 117 | + except ValueError: |
| 118 | + raise TypeError("replacement") |
| 119 | + except TypeError as e: |
| 120 | + self.assertEqual(str(e), "replacement") |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + unittest.main() |
0 commit comments