| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 """Unit tests for the with statement specified in PEP 343.""" | 3 """Unit tests for the with statement specified in PEP 343.""" |
| 4 | 4 |
| 5 | 5 |
| 6 __author__ = "Mike Bland" | 6 __author__ = "Mike Bland" |
| 7 __email__ = "mbland at acm dot org" | 7 __email__ = "mbland at acm dot org" |
| 8 | 8 |
| 9 import sys | 9 import sys |
| 10 import unittest | 10 import unittest |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 647 def __exit__(self, t, v, tb): return False | 647 def __exit__(self, t, v, tb): return False |
| 648 try: | 648 try: |
| 649 with EuropeanSwallow(): | 649 with EuropeanSwallow(): |
| 650 1/0 | 650 1/0 |
| 651 except ZeroDivisionError: | 651 except ZeroDivisionError: |
| 652 pass | 652 pass |
| 653 else: | 653 else: |
| 654 self.fail("ZeroDivisionError should have been raised") | 654 self.fail("ZeroDivisionError should have been raised") |
| 655 | 655 |
| 656 | 656 |
| 657 class NestedWith(unittest.TestCase): | |
|
Benjamin
2009/05/02 18:22:06
You should test that the second's __exit__ is call
Georg
2009/05/02 18:48:00
Done.
| |
| 658 | |
| 659 class Dummy(object): | |
| 660 def __init__(self, value=None):· | |
| 661 if value is None: | |
| 662 value = self | |
| 663 self.value = value | |
| 664 self.enter_called = False | |
| 665 self.exit_called = False | |
| 666 | |
| 667 def __enter__(self): | |
| 668 self.enter_called = True | |
| 669 return self.value | |
| 670 | |
| 671 def __exit__(self, *exc_info):· | |
| 672 self.exit_called = True | |
| 673 | |
| 674 class CtorThrows(object): | |
| 675 def __init__(self): raise RuntimeError() | |
| 676 | |
| 677 class EnterThrows(object): | |
| 678 def __enter__(self): raise RuntimeError() | |
| 679 def __exit__(self, *exc_info): pass | |
| 680 | |
| 681 def testNoExceptions(self): | |
| 682 with self.Dummy() as a, self.Dummy() as b: | |
| 683 self.assertTrue(a.enter_called) | |
| 684 self.assertTrue(b.enter_called) | |
| 685 self.assertTrue(a.exit_called) | |
| 686 self.assertTrue(b.exit_called) | |
| 687 | |
| 688 def testExceptionInExprList(self): | |
| 689 try: | |
| 690 with self.Dummy() as a, self.CtorThrows(): | |
| 691 pass | |
| 692 except: | |
| 693 pass | |
| 694 self.assertTrue(a.enter_called) | |
| 695 self.assertTrue(a.exit_called) | |
| 696 | |
| 697 def testExceptionInEnter(self): | |
| 698 try: | |
| 699 with self.Dummy() as a, self.EnterThrows(): | |
| 700 pass | |
| 701 except: | |
| 702 pass | |
| 703 self.assertTrue(a.enter_called) | |
| 704 self.assertTrue(a.exit_called) | |
| 705 ········ | |
| 706 def testEnterReturnsTuple(self): | |
| 707 with self.Dummy(value=(1,2)) as (a1, a2), \ | |
| 708 self.Dummy(value=(10, 20)) as (b1, b2): | |
| 709 self.assertEquals(1, a1) | |
| 710 self.assertEquals(2, a2) | |
| 711 self.assertEquals(10, b1) | |
| 712 self.assertEquals(20, b2) | |
| 713 | |
| 657 def test_main(): | 714 def test_main(): |
| 658 run_unittest(FailureTestCase, NonexceptionalTestCase, | 715 run_unittest(FailureTestCase, NonexceptionalTestCase, |
| 659 NestedNonexceptionalTestCase, ExceptionalTestCase, | 716 NestedNonexceptionalTestCase, ExceptionalTestCase, |
| 660 NonLocalFlowControlTestCase, | 717 NonLocalFlowControlTestCase, |
| 661 AssignmentTargetTestCase, | 718 AssignmentTargetTestCase, |
| 662 ExitSwallowsExceptionTestCase) | 719 ExitSwallowsExceptionTestCase, |
| 720 NestedWith) | |
| 663 | 721 |
| 664 | 722 |
| 665 if __name__ == '__main__': | 723 if __name__ == '__main__': |
| 666 test_main() | 724 test_main() |
| OLD | NEW |