Thanks to visit codestin.com
Credit goes to codereview.appspot.com

Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(56)

Delta Between Two Patch Sets: Lib/test/test_with.py

Issue 53094: Multi with statement Base URL: http://svn.python.org/view/*checkout*/python/trunk/
Left Patch Set: Created 16 years, 12 months ago
Right Patch Set: Version after review by Benjamin Created 16 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_parser.py ('k') | Modules/parsermodule.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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): 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 658
659 class Dummy(object): 659 class Dummy(object):
660 def __init__(self, value=None): 660 def __init__(self, value=None, gobble=False):
661 if value is None: 661 if value is None:
662 value = self 662 value = self
663 self.value = value 663 self.value = value
664 self.gobble = gobble
664 self.enter_called = False 665 self.enter_called = False
665 self.exit_called = False 666 self.exit_called = False
666 667
667 def __enter__(self): 668 def __enter__(self):
668 self.enter_called = True 669 self.enter_called = True
669 return self.value 670 return self.value
670 671
671 def __exit__(self, *exc_info): 672 def __exit__(self, *exc_info):
672 self.exit_called = True 673 self.exit_called = True
673 674 self.exc_info = exc_info
674 class CtorThrows(object): 675 if self.gobble:
676 return True
677
678 class CtorRaises(object):
675 def __init__(self): raise RuntimeError() 679 def __init__(self): raise RuntimeError()
676 680
677 class EnterThrows(object): 681 class EnterRaises(object):
678 def __enter__(self): raise RuntimeError() 682 def __enter__(self): raise RuntimeError()
679 def __exit__(self, *exc_info): pass 683 def __exit__(self, *exc_info): pass
684
685 class ExitRaises(object):
686 def __enter__(self): pass
687 def __exit__(self, *exc_info): raise RuntimeError()
680 688
681 def testNoExceptions(self): 689 def testNoExceptions(self):
682 with self.Dummy() as a, self.Dummy() as b: 690 with self.Dummy() as a, self.Dummy() as b:
683 self.assertTrue(a.enter_called) 691 self.assertTrue(a.enter_called)
684 self.assertTrue(b.enter_called) 692 self.assertTrue(b.enter_called)
685 self.assertTrue(a.exit_called) 693 self.assertTrue(a.exit_called)
686 self.assertTrue(b.exit_called) 694 self.assertTrue(b.exit_called)
687 695
688 def testExceptionInExprList(self): 696 def testExceptionInExprList(self):
689 try: 697 try:
690 with self.Dummy() as a, self.CtorThrows(): 698 with self.Dummy() as a, self.CtorRaises():
691 pass 699 pass
692 except: 700 except:
693 pass 701 pass
694 self.assertTrue(a.enter_called) 702 self.assertTrue(a.enter_called)
695 self.assertTrue(a.exit_called) 703 self.assertTrue(a.exit_called)
696 704
697 def testExceptionInEnter(self): 705 def testExceptionInEnter(self):
698 try: 706 try:
699 with self.Dummy() as a, self.EnterThrows(): 707 with self.Dummy() as a, self.EnterRaises():
700 pass 708 self.fail('body of bad with executed')
701 except: 709 except RuntimeError:
702 pass 710 pass
711 else:
712 self.fail('RuntimeError not reraised')
703 self.assertTrue(a.enter_called) 713 self.assertTrue(a.enter_called)
704 self.assertTrue(a.exit_called) 714 self.assertTrue(a.exit_called)
705 ········ 715
716 def testExceptionInExit(self):
717 body_executed = False
718 with self.Dummy(gobble=True) as a, self.ExitRaises():
719 body_executed = True
720 self.assertTrue(a.enter_called)
721 self.assertTrue(a.exit_called)
722 self.assertNotEqual(a.exc_info[0], None)
723
706 def testEnterReturnsTuple(self): 724 def testEnterReturnsTuple(self):
707 with self.Dummy(value=(1,2)) as (a1, a2), \ 725 with self.Dummy(value=(1,2)) as (a1, a2), \
708 self.Dummy(value=(10, 20)) as (b1, b2): 726 self.Dummy(value=(10, 20)) as (b1, b2):
709 self.assertEquals(1, a1) 727 self.assertEquals(1, a1)
710 self.assertEquals(2, a2) 728 self.assertEquals(2, a2)
711 self.assertEquals(10, b1) 729 self.assertEquals(10, b1)
712 self.assertEquals(20, b2) 730 self.assertEquals(20, b2)
713 731
714 def test_main(): 732 def test_main():
715 run_unittest(FailureTestCase, NonexceptionalTestCase, 733 run_unittest(FailureTestCase, NonexceptionalTestCase,
716 NestedNonexceptionalTestCase, ExceptionalTestCase, 734 NestedNonexceptionalTestCase, ExceptionalTestCase,
717 NonLocalFlowControlTestCase, 735 NonLocalFlowControlTestCase,
718 AssignmentTargetTestCase, 736 AssignmentTargetTestCase,
719 ExitSwallowsExceptionTestCase, 737 ExitSwallowsExceptionTestCase,
720 NestedWith) 738 NestedWith)
721 739
722 740
723 if __name__ == '__main__': 741 if __name__ == '__main__':
724 test_main() 742 test_main()
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b