Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 1 | # Copyright 2007 Google, Inc. All Rights Reserved. |
| 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | |
| 4 | """Tests for the raise statement.""" |
| 5 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | from test import support |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 7 | import sys |
| 8 | import types |
| 9 | import unittest |
| 10 | |
| 11 | |
| 12 | def get_tb(): |
| 13 | try: |
| 14 | raise OSError() |
| 15 | except: |
| 16 | return sys.exc_info()[2] |
| 17 | |
| 18 | |
| 19 | class TestRaise(unittest.TestCase): |
| 20 | def test_invalid_reraise(self): |
| 21 | try: |
| 22 | raise |
| 23 | except RuntimeError as e: |
| 24 | self.failUnless("No active exception" in str(e)) |
| 25 | else: |
| 26 | self.fail("No exception raised") |
| 27 | |
| 28 | def test_reraise(self): |
| 29 | try: |
| 30 | try: |
| 31 | raise IndexError() |
| 32 | except IndexError as e: |
| 33 | exc1 = e |
| 34 | raise |
| 35 | except IndexError as exc2: |
| 36 | self.failUnless(exc1 is exc2) |
| 37 | else: |
| 38 | self.fail("No exception raised") |
| 39 | |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 40 | def test_erroneous_exception(self): |
| 41 | class MyException(Exception): |
| 42 | def __init__(self): |
| 43 | raise RuntimeError() |
| 44 | |
| 45 | try: |
| 46 | raise MyException |
| 47 | except RuntimeError: |
| 48 | pass |
| 49 | else: |
| 50 | self.fail("No exception raised") |
| 51 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 52 | |
| 53 | class TestCause(unittest.TestCase): |
| 54 | def test_invalid_cause(self): |
| 55 | try: |
| 56 | raise IndexError from 5 |
| 57 | except TypeError as e: |
| 58 | self.failUnless("exception cause" in str(e)) |
| 59 | else: |
| 60 | self.fail("No exception raised") |
| 61 | |
| 62 | def test_class_cause(self): |
| 63 | try: |
| 64 | raise IndexError from KeyError |
| 65 | except IndexError as e: |
| 66 | self.failUnless(isinstance(e.__cause__, KeyError)) |
| 67 | else: |
| 68 | self.fail("No exception raised") |
| 69 | |
| 70 | def test_instance_cause(self): |
| 71 | cause = KeyError() |
| 72 | try: |
| 73 | raise IndexError from cause |
| 74 | except IndexError as e: |
| 75 | self.failUnless(e.__cause__ is cause) |
| 76 | else: |
| 77 | self.fail("No exception raised") |
| 78 | |
Collin Winter | 1966f1c | 2007-09-01 20:26:44 +0000 | [diff] [blame] | 79 | def test_erroneous_cause(self): |
| 80 | class MyException(Exception): |
| 81 | def __init__(self): |
| 82 | raise RuntimeError() |
| 83 | |
| 84 | try: |
| 85 | raise IndexError from MyException |
| 86 | except RuntimeError: |
| 87 | pass |
| 88 | else: |
| 89 | self.fail("No exception raised") |
| 90 | |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 91 | |
| 92 | class TestTraceback(unittest.TestCase): |
| 93 | def test_sets_traceback(self): |
| 94 | try: |
| 95 | raise IndexError() |
| 96 | except IndexError as e: |
| 97 | self.failUnless(isinstance(e.__traceback__, types.TracebackType)) |
| 98 | else: |
| 99 | self.fail("No exception raised") |
| 100 | |
| 101 | def test_accepts_traceback(self): |
| 102 | tb = get_tb() |
| 103 | try: |
| 104 | raise IndexError().with_traceback(tb) |
| 105 | except IndexError as e: |
| 106 | self.assertNotEqual(e.__traceback__, tb) |
| 107 | self.assertEqual(e.__traceback__.tb_next, tb) |
| 108 | else: |
| 109 | self.fail("No exception raised") |
| 110 | |
| 111 | |
| 112 | # Disabled until context is implemented |
| 113 | # class TestContext(object): |
| 114 | # def test_instance_context_bare_raise(self): |
| 115 | # context = IndexError() |
| 116 | # try: |
| 117 | # try: |
| 118 | # raise context |
| 119 | # except: |
| 120 | # raise OSError() |
| 121 | # except OSError as e: |
| 122 | # self.assertEqual(e.__context__, context) |
| 123 | # else: |
| 124 | # self.fail("No exception raised") |
| 125 | # |
| 126 | # def test_class_context_bare_raise(self): |
| 127 | # context = IndexError |
| 128 | # try: |
| 129 | # try: |
| 130 | # raise context |
| 131 | # except: |
| 132 | # raise OSError() |
| 133 | # except OSError as e: |
| 134 | # self.assertNotEqual(e.__context__, context) |
| 135 | # self.failUnless(isinstance(e.__context__, context)) |
| 136 | # else: |
| 137 | # self.fail("No exception raised") |
| 138 | |
| 139 | |
| 140 | class TestRemovedFunctionality(unittest.TestCase): |
| 141 | def test_tuples(self): |
| 142 | try: |
| 143 | raise (IndexError, KeyError) # This should be a tuple! |
| 144 | except TypeError: |
| 145 | pass |
| 146 | else: |
| 147 | self.fail("No exception raised") |
| 148 | |
| 149 | def test_strings(self): |
| 150 | try: |
| 151 | raise "foo" |
| 152 | except TypeError: |
| 153 | pass |
| 154 | else: |
| 155 | self.fail("No exception raised") |
| 156 | |
| 157 | |
| 158 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 159 | support.run_unittest(__name__) |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | if __name__ == "__main__": |
| 163 | unittest.main() |