From 9f9d80b6de7f44de21e00cc4e6168da2d2fe380c Mon Sep 17 00:00:00 2001 From: Haoyu Bai Date: Thu, 31 Mar 2011 01:06:37 +0800 Subject: [PATCH] more test for raise ... from ... --- tests/run/test_raisefrom.pyx | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/run/test_raisefrom.pyx diff --git a/tests/run/test_raisefrom.pyx b/tests/run/test_raisefrom.pyx new file mode 100644 index 00000000..3a6110f3 --- /dev/null +++ b/tests/run/test_raisefrom.pyx @@ -0,0 +1,39 @@ +import unittest +# adapted from pyregr +class TestCause(unittest.TestCase): + def test_invalid_cause(self): + try: + raise IndexError from 5 + except TypeError as e: + self.assertIn("exception cause", str(e)) + else: + self.fail("No exception raised") + + def test_class_cause(self): + try: + raise IndexError from KeyError + except IndexError as e: + self.assertIsInstance(e.__cause__, KeyError) + else: + self.fail("No exception raised") + + def test_instance_cause(self): + cause = KeyError() + try: + raise IndexError from cause + except IndexError as e: + self.assertTrue(e.__cause__ is cause) + else: + self.fail("No exception raised") + + def test_erroneous_cause(self): + class MyException(Exception): + def __init__(self): + raise RuntimeError() + + try: + raise IndexError from MyException + except RuntimeError: + pass + else: + self.fail("No exception raised") -- 2.26.2