-import sys, itertools
-
-def to_tuple(t):
- if t is None or isinstance(t, (basestring, int, long, complex)):
- return t
- elif isinstance(t, list):
- return [to_tuple(e) for e in t]
- result = [t.__class__.__name__]
- if t._fields is None:
- return tuple(result)
- for f in t._fields:
- result.append(to_tuple(getattr(t, f)))
- return tuple(result)
-
-# These tests are compiled through "exec"
-# There should be atleast one test per statement
-exec_tests = [
- # FunctionDef
- "def f(): pass",
- # ClassDef
- "class C:pass",
- # Return
- "def f():return 1",
- # Delete
- "del v",
- # Assign
- "v = 1",
- # AugAssign
- "v += 1",
- # Print
- "print >>f, 1, ",
- # For
- "for v in v:pass",
- # While
- "while v:pass",
- # If
- "if v:pass",
- # Raise
- "raise Exception, 'string'",
- # TryExcept
- "try:\n pass\nexcept Exception:\n pass",
- # TryFinally
- "try:\n pass\nfinally:\n pass",
- # Assert
- "assert v",
- # Import
- "import sys",
- # ImportFrom
- "from sys import v",
- # Exec
- "exec 'v'",
- # Global
- "global v",
- # Expr
- "1",
- # Pass,
- "pass",
- # Break
- "break",
- # Continue
- "continue",
-]
-
-# These are compiled through "single"
-# because of overlap with "eval", it just tests what
-# can't be tested with "eval"
-single_tests = [
- "1+2"
-]
-
-# These are compiled through "eval"
-# It should test all expressions
-eval_tests = [
- # BoolOp
- "a and b",
- # BinOp
- "a + b",
- # UnaryOp
- "not v",
- # Lambda
- "lambda:None",
- # Dict
- "{ 1:2 }",
- # ListComp
- "[a for b in c if d]",
- # GeneratorExp
- "(a for b in c if d)",
- # Yield - yield expressions can't work outside a function
- #
- # Compare
- "1 < 2 < 3",
- # Call
- "f(1,2,c=3,*d,**e)",
- # Repr
- "`v`",
- # Num
- "10L",
- # Str
- "'string'",
- # Attribute
- "a.b",
- # Subscript
- "a[b:c]",
- # Name
- "v",
- # List
- "[1,2,3]",
- # Tuple
- "1,2,3"
-]
-
-# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension
-# excepthandler, arguments, keywords, alias
-
-if __name__=='__main__' and sys.argv[1:] == ['-g']:
- for statements, kind in ((exec_tests, "exec"), (single_tests, "single"), (eval_tests, "eval")):
- print kind+"_results = ["
- for s in statements:
- print repr(to_tuple(compile(s, "?", kind, 0x400)))+","
- print "]"
- print "run_tests()"
- raise SystemExit
-
-def run_tests():
- for input, output, kind in ((exec_tests, exec_results, "exec"),
- (single_tests, single_results, "single"),
- (eval_tests, eval_results, "eval")):
- for i, o in itertools.izip(input, output):
- assert to_tuple(compile(i, "?", kind, 0x400)) == o
-
-#### EVERYTHING BELOW IS GENERATED #####
-exec_results = [
-('Module', [('FunctionDef', 'f', ('arguments', [], None, None, []), [('Pass',)], [])]),
-('Module', [('ClassDef', 'C', [], [('Pass',)])]),
-('Module', [('FunctionDef', 'f', ('arguments', [], None, None, []), [('Return', ('Num', 1))], [])]),
-('Module', [('Delete', [('Name', 'v', ('Del',))])]),
-('Module', [('Assign', [('Name', 'v', ('Store',))], ('Num', 1))]),
-('Module', [('AugAssign', ('Name', 'v', ('Load',)), ('Add',), ('Num', 1))]),
-('Module', [('Print', ('Name', 'f', ('Load',)), [('Num', 1)], False)]),
-('Module', [('For', ('Name', 'v', ('Store',)), ('Name', 'v', ('Load',)), [('Pass',)], [])]),
-('Module', [('While', ('Name', 'v', ('Load',)), [('Pass',)], [])]),
-('Module', [('If', ('Name', 'v', ('Load',)), [('Pass',)], [])]),
-('Module', [('Raise', ('Name', 'Exception', ('Load',)), ('Str', 'string'), None)]),
-('Module', [('TryExcept', [('Pass',)], [('excepthandler', ('Name', 'Exception', ('Load',)), None, [('Pass',)])], [])]),
-('Module', [('TryFinally', [('Pass',)], [('Pass',)])]),
-('Module', [('Assert', ('Name', 'v', ('Load',)), None)]),
-('Module', [('Import', [('alias', 'sys', None)])]),
-('Module', [('ImportFrom', 'sys', [('alias', 'v', None)], 0)]),
-('Module', [('Exec', ('Str', 'v'), None, None)]),
-('Module', [('Global', ['v'])]),
-('Module', [('Expr', ('Num', 1))]),
-('Module', [('Pass',)]),
-('Module', [('Break',)]),
-('Module', [('Continue',)]),
-]
-single_results = [
-('Interactive', [('Expr', ('BinOp', ('Num', 1), ('Add',), ('Num', 2)))]),
-]
-eval_results = [
-('Expression', ('BoolOp', ('And',), [('Name', 'a', ('Load',)), ('Name', 'b', ('Load',))])),
-('Expression', ('BinOp', ('Name', 'a', ('Load',)), ('Add',), ('Name', 'b', ('Load',)))),
-('Expression', ('UnaryOp', ('Not',), ('Name', 'v', ('Load',)))),
-('Expression', ('Lambda', ('arguments', [], None, None, []), ('Name', 'None', ('Load',)))),
-('Expression', ('Dict', [('Num', 1)], [('Num', 2)])),
-('Expression', ('ListComp', ('Name', 'a', ('Load',)), [('comprehension', ('Name', 'b', ('Store',)), ('Name', 'c', ('Load',)), [('Name', 'd', ('Load',))])])),
-('Expression', ('GeneratorExp', ('Name', 'a', ('Load',)), [('comprehension', ('Name', 'b', ('Store',)), ('Name', 'c', ('Load',)), [('Name', 'd', ('Load',))])])),
-('Expression', ('Compare', ('Num', 1), [('Lt',), ('Lt',)], [('Num', 2), ('Num', 3)])),
-('Expression', ('Call', ('Name', 'f', ('Load',)), [('Num', 1), ('Num', 2)], [('keyword', 'c', ('Num', 3))], ('Name', 'd', ('Load',)), ('Name', 'e', ('Load',)))),
-('Expression', ('Repr', ('Name', 'v', ('Load',)))),
-('Expression', ('Num', 10L)),
-('Expression', ('Str', 'string')),
-('Expression', ('Attribute', ('Name', 'a', ('Load',)), 'b', ('Load',))),
-('Expression', ('Subscript', ('Name', 'a', ('Load',)), ('Slice', ('Name', 'b', ('Load',)), ('Name', 'c', ('Load',)), None), ('Load',))),
-('Expression', ('Name', 'v', ('Load',))),
-('Expression', ('List', [('Num', 1), ('Num', 2), ('Num', 3)], ('Load',))),
-('Expression', ('Tuple', [('Num', 1), ('Num', 2), ('Num', 3)], ('Load',))),
-]
-run_tests()
-
+import sys, itertools\r
+\r
+def to_tuple(t):\r
+ if t is None or isinstance(t, (basestring, int, long, complex)):\r
+ return t\r
+ elif isinstance(t, list):\r
+ return [to_tuple(e) for e in t]\r
+ result = [t.__class__.__name__]\r
+ if t._fields is None:\r
+ return tuple(result)\r
+ for f in t._fields:\r
+ result.append(to_tuple(getattr(t, f)))\r
+ return tuple(result)\r
+\r
+# These tests are compiled through "exec"\r
+# There should be atleast one test per statement\r
+exec_tests = [\r
+ # FunctionDef\r
+ "def f(): pass",\r
+ # ClassDef\r
+ "class C:pass",\r
+ # Return\r
+ "def f():return 1",\r
+ # Delete\r
+ "del v",\r
+ # Assign\r
+ "v = 1",\r
+ # AugAssign\r
+ "v += 1",\r
+ # Print\r
+ "print >>f, 1, ",\r
+ # For\r
+ "for v in v:pass",\r
+ # While\r
+ "while v:pass",\r
+ # If\r
+ "if v:pass",\r
+ # Raise\r
+ "raise Exception, 'string'",\r
+ # TryExcept\r
+ "try:\n pass\nexcept Exception:\n pass",\r
+ # TryFinally\r
+ "try:\n pass\nfinally:\n pass",\r
+ # Assert\r
+ "assert v",\r
+ # Import\r
+ "import sys",\r
+ # ImportFrom\r
+ "from sys import v",\r
+ # Exec\r
+ "exec 'v'",\r
+ # Global\r
+ "global v",\r
+ # Expr\r
+ "1",\r
+ # Pass,\r
+ "pass",\r
+ # Break\r
+ "break",\r
+ # Continue\r
+ "continue",\r
+]\r
+\r
+# These are compiled through "single"\r
+# because of overlap with "eval", it just tests what\r
+# can't be tested with "eval"\r
+single_tests = [\r
+ "1+2"\r
+]\r
+\r
+# These are compiled through "eval"\r
+# It should test all expressions\r
+eval_tests = [\r
+ # BoolOp\r
+ "a and b",\r
+ # BinOp\r
+ "a + b",\r
+ # UnaryOp\r
+ "not v",\r
+ # Lambda\r
+ "lambda:None",\r
+ # Dict\r
+ "{ 1:2 }",\r
+ # ListComp\r
+ "[a for b in c if d]",\r
+ # GeneratorExp\r
+ "(a for b in c if d)",\r
+ # Yield - yield expressions can't work outside a function\r
+ #\r
+ # Compare\r
+ "1 < 2 < 3",\r
+ # Call\r
+ "f(1,2,c=3,*d,**e)",\r
+ # Repr\r
+ "`v`",\r
+ # Num\r
+ "10L",\r
+ # Str\r
+ "'string'",\r
+ # Attribute\r
+ "a.b",\r
+ # Subscript\r
+ "a[b:c]",\r
+ # Name\r
+ "v",\r
+ # List\r
+ "[1,2,3]",\r
+ # Tuple\r
+ "1,2,3"\r
+]\r
+\r
+# TODO: expr_context, slice, boolop, operator, unaryop, cmpop, comprehension\r
+# excepthandler, arguments, keywords, alias\r
+\r
+if __name__=='__main__' and sys.argv[1:] == ['-g']:\r
+ for statements, kind in ((exec_tests, "exec"), (single_tests, "single"), (eval_tests, "eval")):\r
+ print kind+"_results = ["\r
+ for s in statements:\r
+ print repr(to_tuple(compile(s, "?", kind, 0x400)))+","\r
+ print "]"\r
+ print "run_tests()"\r
+ raise SystemExit\r
+\r
+def run_tests():\r
+ for input, output, kind in ((exec_tests, exec_results, "exec"),\r
+ (single_tests, single_results, "single"),\r
+ (eval_tests, eval_results, "eval")):\r
+ for i, o in itertools.izip(input, output):\r
+ assert to_tuple(compile(i, "?", kind, 0x400)) == o\r
+\r
+#### EVERYTHING BELOW IS GENERATED #####\r
+exec_results = [\r
+('Module', [('FunctionDef', 'f', ('arguments', [], None, None, []), [('Pass',)], [])]),\r
+('Module', [('ClassDef', 'C', [], [('Pass',)])]),\r
+('Module', [('FunctionDef', 'f', ('arguments', [], None, None, []), [('Return', ('Num', 1))], [])]),\r
+('Module', [('Delete', [('Name', 'v', ('Del',))])]),\r
+('Module', [('Assign', [('Name', 'v', ('Store',))], ('Num', 1))]),\r
+('Module', [('AugAssign', ('Name', 'v', ('Load',)), ('Add',), ('Num', 1))]),\r
+('Module', [('Print', ('Name', 'f', ('Load',)), [('Num', 1)], False)]),\r
+('Module', [('For', ('Name', 'v', ('Store',)), ('Name', 'v', ('Load',)), [('Pass',)], [])]),\r
+('Module', [('While', ('Name', 'v', ('Load',)), [('Pass',)], [])]),\r
+('Module', [('If', ('Name', 'v', ('Load',)), [('Pass',)], [])]),\r
+('Module', [('Raise', ('Name', 'Exception', ('Load',)), ('Str', 'string'), None)]),\r
+('Module', [('TryExcept', [('Pass',)], [('excepthandler', ('Name', 'Exception', ('Load',)), None, [('Pass',)])], [])]),\r
+('Module', [('TryFinally', [('Pass',)], [('Pass',)])]),\r
+('Module', [('Assert', ('Name', 'v', ('Load',)), None)]),\r
+('Module', [('Import', [('alias', 'sys', None)])]),\r
+('Module', [('ImportFrom', 'sys', [('alias', 'v', None)], 0)]),\r
+('Module', [('Exec', ('Str', 'v'), None, None)]),\r
+('Module', [('Global', ['v'])]),\r
+('Module', [('Expr', ('Num', 1))]),\r
+('Module', [('Pass',)]),\r
+('Module', [('Break',)]),\r
+('Module', [('Continue',)]),\r
+]\r
+single_results = [\r
+('Interactive', [('Expr', ('BinOp', ('Num', 1), ('Add',), ('Num', 2)))]),\r
+]\r
+eval_results = [\r
+('Expression', ('BoolOp', ('And',), [('Name', 'a', ('Load',)), ('Name', 'b', ('Load',))])),\r
+('Expression', ('BinOp', ('Name', 'a', ('Load',)), ('Add',), ('Name', 'b', ('Load',)))),\r
+('Expression', ('UnaryOp', ('Not',), ('Name', 'v', ('Load',)))),\r
+('Expression', ('Lambda', ('arguments', [], None, None, []), ('Name', 'None', ('Load',)))),\r
+('Expression', ('Dict', [('Num', 1)], [('Num', 2)])),\r
+('Expression', ('ListComp', ('Name', 'a', ('Load',)), [('comprehension', ('Name', 'b', ('Store',)), ('Name', 'c', ('Load',)), [('Name', 'd', ('Load',))])])),\r
+('Expression', ('GeneratorExp', ('Name', 'a', ('Load',)), [('comprehension', ('Name', 'b', ('Store',)), ('Name', 'c', ('Load',)), [('Name', 'd', ('Load',))])])),\r
+('Expression', ('Compare', ('Num', 1), [('Lt',), ('Lt',)], [('Num', 2), ('Num', 3)])),\r
+('Expression', ('Call', ('Name', 'f', ('Load',)), [('Num', 1), ('Num', 2)], [('keyword', 'c', ('Num', 3))], ('Name', 'd', ('Load',)), ('Name', 'e', ('Load',)))),\r
+('Expression', ('Repr', ('Name', 'v', ('Load',)))),\r
+('Expression', ('Num', 10L)),\r
+('Expression', ('Str', 'string')),\r
+('Expression', ('Attribute', ('Name', 'a', ('Load',)), 'b', ('Load',))),\r
+('Expression', ('Subscript', ('Name', 'a', ('Load',)), ('Slice', ('Name', 'b', ('Load',)), ('Name', 'c', ('Load',)), None), ('Load',))),\r
+('Expression', ('Name', 'v', ('Load',))),\r
+('Expression', ('List', [('Num', 1), ('Num', 2), ('Num', 3)], ('Load',))),\r
+('Expression', ('Tuple', [('Num', 1), ('Num', 2), ('Num', 3)], ('Load',))),\r
+]\r
+run_tests()\r
-#!/usr/bin/env python
-
-"""Unit tests for the with statement specified in PEP 343."""
-
-__author__ = "Mike Bland"
-__email__ = "mbland at acm dot org"
-
-import unittest
-from test.contextmanager import GeneratorContextManager
-from test.nested import nested
-from test.test_support import run_unittest
-
-
-class MockContextManager(GeneratorContextManager):
- def __init__(self, gen):
- GeneratorContextManager.__init__(self, gen)
- self.context_called = False
- self.enter_called = False
- self.exit_called = False
- self.exit_args = None
-
- def __context__(self):
- self.context_called = True
- return GeneratorContextManager.__context__(self)
-
- def __enter__(self):
- self.enter_called = True
- return GeneratorContextManager.__enter__(self)
-
- def __exit__(self, type, value, traceback):
- self.exit_called = True
- self.exit_args = (type, value, traceback)
- return GeneratorContextManager.__exit__(self, type, value, traceback)
-
-
-def mock_contextmanager(func):
- def helper(*args, **kwds):
- return MockContextManager(func(*args, **kwds))
- return helper
-
-
-class MockResource(object):
- def __init__(self):
- self.yielded = False
- self.stopped = False
-
-
-@mock_contextmanager
-def mock_contextmanager_generator():
- mock = MockResource()
- try:
- mock.yielded = True
- yield mock
- finally:
- mock.stopped = True
-
-
-class MockNested(nested):
- def __init__(self, *contexts):
- nested.__init__(self, *contexts)
- self.context_called = False
- self.enter_called = False
- self.exit_called = False
- self.exit_args = None
-
- def __context__(self):
- self.context_called = True
- return nested.__context__(self)
-
- def __enter__(self):
- self.enter_called = True
- return nested.__enter__(self)
-
- def __exit__(self, *exc_info):
- self.exit_called = True
- self.exit_args = exc_info
- return nested.__exit__(self, *exc_info)
-
-
-class FailureTestCase(unittest.TestCase):
- def testNameError(self):
- def fooNotDeclared():
- with foo: pass
- self.assertRaises(NameError, fooNotDeclared)
-
- def testContextAttributeError(self):
- class LacksContext(object):
- def __enter__(self):
- pass
-
- def __exit__(self, type, value, traceback):
- pass
-
- def fooLacksContext():
- foo = LacksContext()
- with foo: pass
- self.assertRaises(AttributeError, fooLacksContext)
-
- def testEnterAttributeError(self):
- class LacksEnter(object):
- def __context__(self):
- pass
-
- def __exit__(self, type, value, traceback):
- pass
-
- def fooLacksEnter():
- foo = LacksEnter()
- with foo: pass
- self.assertRaises(AttributeError, fooLacksEnter)
-
- def testExitAttributeError(self):
- class LacksExit(object):
- def __context__(self):
- pass
-
- def __enter__(self):
- pass
-
- def fooLacksExit():
- foo = LacksExit()
- with foo: pass
- self.assertRaises(AttributeError, fooLacksExit)
-
- def assertRaisesSyntaxError(self, codestr):
- def shouldRaiseSyntaxError(s):
- compile(s, '', 'single')
- self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr)
-
- def testAssignmentToNoneError(self):
- self.assertRaisesSyntaxError('with mock as None:\n pass')
- self.assertRaisesSyntaxError(
- 'with mock as (None):\n'
- ' pass')
-
- def testAssignmentToEmptyTupleError(self):
- self.assertRaisesSyntaxError(
- 'with mock as ():\n'
- ' pass')
-
- def testAssignmentToTupleOnlyContainingNoneError(self):
- self.assertRaisesSyntaxError('with mock as None,:\n pass')
- self.assertRaisesSyntaxError(
- 'with mock as (None,):\n'
- ' pass')
-
- def testAssignmentToTupleContainingNoneError(self):
- self.assertRaisesSyntaxError(
- 'with mock as (foo, None, bar):\n'
- ' pass')
-
- def testContextThrows(self):
- class ContextThrows(object):
- def __context__(self):
- raise RuntimeError("Context threw")
-
- def shouldThrow():
- ct = ContextThrows()
- self.foo = None
- with ct as self.foo:
- pass
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertEqual(self.foo, None)
-
- def testEnterThrows(self):
- class EnterThrows(object):
- def __context__(self):
- return self
-
- def __enter__(self):
- raise RuntimeError("Context threw")
-
- def __exit__(self, *args):
- pass
-
- def shouldThrow():
- ct = EnterThrows()
- self.foo = None
- with ct as self.foo:
- pass
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertEqual(self.foo, None)
-
- def testExitThrows(self):
- class ExitThrows(object):
- def __context__(self):
- return self
- def __enter__(self):
- return
- def __exit__(self, *args):
- raise RuntimeError(42)
- def shouldThrow():
- with ExitThrows():
- pass
- self.assertRaises(RuntimeError, shouldThrow)
-
-class ContextmanagerAssertionMixin(object):
- TEST_EXCEPTION = RuntimeError("test exception")
-
- def assertInWithManagerInvariants(self, mock_manager):
- self.assertTrue(mock_manager.context_called)
- self.assertTrue(mock_manager.enter_called)
- self.assertFalse(mock_manager.exit_called)
- self.assertEqual(mock_manager.exit_args, None)
-
- def assertAfterWithManagerInvariants(self, mock_manager, exit_args):
- self.assertTrue(mock_manager.context_called)
- self.assertTrue(mock_manager.enter_called)
- self.assertTrue(mock_manager.exit_called)
- self.assertEqual(mock_manager.exit_args, exit_args)
-
- def assertAfterWithManagerInvariantsNoError(self, mock_manager):
- self.assertAfterWithManagerInvariants(mock_manager,
- (None, None, None))
-
- def assertInWithGeneratorInvariants(self, mock_generator):
- self.assertTrue(mock_generator.yielded)
- self.assertFalse(mock_generator.stopped)
-
- def assertAfterWithGeneratorInvariantsNoError(self, mock_generator):
- self.assertTrue(mock_generator.yielded)
- self.assertTrue(mock_generator.stopped)
-
- def raiseTestException(self):
- raise self.TEST_EXCEPTION
-
- def assertAfterWithManagerInvariantsWithError(self, mock_manager):
- self.assertTrue(mock_manager.context_called)
- self.assertTrue(mock_manager.enter_called)
- self.assertTrue(mock_manager.exit_called)
- self.assertEqual(mock_manager.exit_args[0], RuntimeError)
- self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
-
- def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):
- self.assertTrue(mock_generator.yielded)
- self.assertTrue(mock_generator.stopped)
-
-
-class NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
- def testInlineGeneratorSyntax(self):
- with mock_contextmanager_generator():
- pass
-
- def testUnboundGenerator(self):
- mock = mock_contextmanager_generator()
- with mock:
- pass
- self.assertAfterWithManagerInvariantsNoError(mock)
-
- def testInlineGeneratorBoundSyntax(self):
- with mock_contextmanager_generator() as foo:
- self.assertInWithGeneratorInvariants(foo)
- # FIXME: In the future, we'll try to keep the bound names from leaking
- self.assertAfterWithGeneratorInvariantsNoError(foo)
-
- def testInlineGeneratorBoundToExistingVariable(self):
- foo = None
- with mock_contextmanager_generator() as foo:
- self.assertInWithGeneratorInvariants(foo)
- self.assertAfterWithGeneratorInvariantsNoError(foo)
-
- def testInlineGeneratorBoundToDottedVariable(self):
- with mock_contextmanager_generator() as self.foo:
- self.assertInWithGeneratorInvariants(self.foo)
- self.assertAfterWithGeneratorInvariantsNoError(self.foo)
-
- def testBoundGenerator(self):
- mock = mock_contextmanager_generator()
- with mock as foo:
- self.assertInWithGeneratorInvariants(foo)
- self.assertInWithManagerInvariants(mock)
- self.assertAfterWithGeneratorInvariantsNoError(foo)
- self.assertAfterWithManagerInvariantsNoError(mock)
-
- def testNestedSingleStatements(self):
- mock_a = mock_contextmanager_generator()
- with mock_a as foo:
- mock_b = mock_contextmanager_generator()
- with mock_b as bar:
- self.assertInWithManagerInvariants(mock_a)
- self.assertInWithManagerInvariants(mock_b)
- self.assertInWithGeneratorInvariants(foo)
- self.assertInWithGeneratorInvariants(bar)
- self.assertAfterWithManagerInvariantsNoError(mock_b)
- self.assertAfterWithGeneratorInvariantsNoError(bar)
- self.assertInWithManagerInvariants(mock_a)
- self.assertInWithGeneratorInvariants(foo)
- self.assertAfterWithManagerInvariantsNoError(mock_a)
- self.assertAfterWithGeneratorInvariantsNoError(foo)
-
-
-class NestedNonexceptionalTestCase(unittest.TestCase,
- ContextmanagerAssertionMixin):
- def testSingleArgInlineGeneratorSyntax(self):
- with nested(mock_contextmanager_generator()):
- pass
-
- def testSingleArgUnbound(self):
- mock_contextmanager = mock_contextmanager_generator()
- mock_nested = MockNested(mock_contextmanager)
- with mock_nested:
- self.assertInWithManagerInvariants(mock_contextmanager)
- self.assertInWithManagerInvariants(mock_nested)
- self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)
- self.assertAfterWithManagerInvariantsNoError(mock_nested)
-
- def testSingleArgBoundToNonTuple(self):
- m = mock_contextmanager_generator()
- # This will bind all the arguments to nested() into a single list
- # assigned to foo.
- with nested(m) as foo:
- self.assertInWithManagerInvariants(m)
- self.assertAfterWithManagerInvariantsNoError(m)
-
- def testSingleArgBoundToSingleElementParenthesizedList(self):
- m = mock_contextmanager_generator()
- # This will bind all the arguments to nested() into a single list
- # assigned to foo.
- # FIXME: what should this do: with nested(m) as (foo,):
- with nested(m) as (foo):
- self.assertInWithManagerInvariants(m)
- self.assertAfterWithManagerInvariantsNoError(m)
-
- def testSingleArgBoundToMultipleElementTupleError(self):
- def shouldThrowValueError():
- with nested(mock_contextmanager_generator()) as (foo, bar):
- pass
- self.assertRaises(ValueError, shouldThrowValueError)
-
- def testSingleArgUnbound(self):
- mock_contextmanager = mock_contextmanager_generator()
- mock_nested = MockNested(mock_contextmanager)
- with mock_nested:
- self.assertInWithManagerInvariants(mock_contextmanager)
- self.assertInWithManagerInvariants(mock_nested)
- self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)
- self.assertAfterWithManagerInvariantsNoError(mock_nested)
-
- def testMultipleArgUnbound(self):
- m = mock_contextmanager_generator()
- n = mock_contextmanager_generator()
- o = mock_contextmanager_generator()
- mock_nested = MockNested(m, n, o)
- with mock_nested:
- self.assertInWithManagerInvariants(m)
- self.assertInWithManagerInvariants(n)
- self.assertInWithManagerInvariants(o)
- self.assertInWithManagerInvariants(mock_nested)
- self.assertAfterWithManagerInvariantsNoError(m)
- self.assertAfterWithManagerInvariantsNoError(n)
- self.assertAfterWithManagerInvariantsNoError(o)
- self.assertAfterWithManagerInvariantsNoError(mock_nested)
-
- def testMultipleArgBound(self):
- mock_nested = MockNested(mock_contextmanager_generator(),
- mock_contextmanager_generator(), mock_contextmanager_generator())
- with mock_nested as (m, n, o):
- self.assertInWithGeneratorInvariants(m)
- self.assertInWithGeneratorInvariants(n)
- self.assertInWithGeneratorInvariants(o)
- self.assertInWithManagerInvariants(mock_nested)
- self.assertAfterWithGeneratorInvariantsNoError(m)
- self.assertAfterWithGeneratorInvariantsNoError(n)
- self.assertAfterWithGeneratorInvariantsNoError(o)
- self.assertAfterWithManagerInvariantsNoError(mock_nested)
-
-
-class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
- def testSingleResource(self):
- cm = mock_contextmanager_generator()
- def shouldThrow():
- with cm as self.resource:
- self.assertInWithManagerInvariants(cm)
- self.assertInWithGeneratorInvariants(self.resource)
- self.raiseTestException()
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertAfterWithManagerInvariantsWithError(cm)
- self.assertAfterWithGeneratorInvariantsWithError(self.resource)
-
- def testNestedSingleStatements(self):
- mock_a = mock_contextmanager_generator()
- mock_b = mock_contextmanager_generator()
- def shouldThrow():
- with mock_a as self.foo:
- with mock_b as self.bar:
- self.assertInWithManagerInvariants(mock_a)
- self.assertInWithManagerInvariants(mock_b)
- self.assertInWithGeneratorInvariants(self.foo)
- self.assertInWithGeneratorInvariants(self.bar)
- self.raiseTestException()
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertAfterWithManagerInvariantsWithError(mock_a)
- self.assertAfterWithManagerInvariantsWithError(mock_b)
- self.assertAfterWithGeneratorInvariantsWithError(self.foo)
- self.assertAfterWithGeneratorInvariantsWithError(self.bar)
-
- def testMultipleResourcesInSingleStatement(self):
- cm_a = mock_contextmanager_generator()
- cm_b = mock_contextmanager_generator()
- mock_nested = MockNested(cm_a, cm_b)
- def shouldThrow():
- with mock_nested as (self.resource_a, self.resource_b):
- self.assertInWithManagerInvariants(cm_a)
- self.assertInWithManagerInvariants(cm_b)
- self.assertInWithManagerInvariants(mock_nested)
- self.assertInWithGeneratorInvariants(self.resource_a)
- self.assertInWithGeneratorInvariants(self.resource_b)
- self.raiseTestException()
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertAfterWithManagerInvariantsWithError(cm_a)
- self.assertAfterWithManagerInvariantsWithError(cm_b)
- self.assertAfterWithManagerInvariantsWithError(mock_nested)
- self.assertAfterWithGeneratorInvariantsWithError(self.resource_a)
- self.assertAfterWithGeneratorInvariantsWithError(self.resource_b)
-
- def testNestedExceptionBeforeInnerStatement(self):
- mock_a = mock_contextmanager_generator()
- mock_b = mock_contextmanager_generator()
- self.bar = None
- def shouldThrow():
- with mock_a as self.foo:
- self.assertInWithManagerInvariants(mock_a)
- self.assertInWithGeneratorInvariants(self.foo)
- self.raiseTestException()
- with mock_b as self.bar:
- pass
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertAfterWithManagerInvariantsWithError(mock_a)
- self.assertAfterWithGeneratorInvariantsWithError(self.foo)
-
- # The inner statement stuff should never have been touched
- self.assertEqual(self.bar, None)
- self.assertFalse(mock_b.context_called)
- self.assertFalse(mock_b.enter_called)
- self.assertFalse(mock_b.exit_called)
- self.assertEqual(mock_b.exit_args, None)
-
- def testNestedExceptionAfterInnerStatement(self):
- mock_a = mock_contextmanager_generator()
- mock_b = mock_contextmanager_generator()
- def shouldThrow():
- with mock_a as self.foo:
- with mock_b as self.bar:
- self.assertInWithManagerInvariants(mock_a)
- self.assertInWithManagerInvariants(mock_b)
- self.assertInWithGeneratorInvariants(self.foo)
- self.assertInWithGeneratorInvariants(self.bar)
- self.raiseTestException()
- self.assertRaises(RuntimeError, shouldThrow)
- self.assertAfterWithManagerInvariantsWithError(mock_a)
- self.assertAfterWithManagerInvariantsNoError(mock_b)
- self.assertAfterWithGeneratorInvariantsWithError(self.foo)
- self.assertAfterWithGeneratorInvariantsNoError(self.bar)
-
-
-class NonLocalFlowControlTestCase(unittest.TestCase):
-
- def testWithBreak(self):
- counter = 0
- while True:
- counter += 1
- with mock_contextmanager_generator():
- counter += 10
- break
- counter += 100 # Not reached
- self.assertEqual(counter, 11)
-
- def testWithContinue(self):
- counter = 0
- while True:
- counter += 1
- if counter > 2:
- break
- with mock_contextmanager_generator():
- counter += 10
- continue
- counter += 100 # Not reached
- self.assertEqual(counter, 12)
-
- def testWithReturn(self):
- def foo():
- counter = 0
- while True:
- counter += 1
- with mock_contextmanager_generator():
- counter += 10
- return counter
- counter += 100 # Not reached
- self.assertEqual(foo(), 11)
-
- def testWithYield(self):
- def gen():
- with mock_contextmanager_generator():
- yield 12
- yield 13
- x = list(gen())
- self.assertEqual(x, [12, 13])
-
- def testWithRaise(self):
- counter = 0
- try:
- counter += 1
- with mock_contextmanager_generator():
- counter += 10
- raise RuntimeError
- counter += 100 # Not reached
- except RuntimeError:
- self.assertEqual(counter, 11)
- else:
- self.fail("Didn't raise RuntimeError")
-
-
-class AssignmentTargetTestCase(unittest.TestCase):
-
- def testSingleComplexTarget(self):
- targets = {1: [0, 1, 2]}
- with mock_contextmanager_generator() as targets[1][0]:
- self.assertEqual(targets.keys(), [1])
- self.assertEqual(targets[1][0].__class__, MockResource)
- with mock_contextmanager_generator() as targets.values()[0][1]:
- self.assertEqual(targets.keys(), [1])
- self.assertEqual(targets[1][1].__class__, MockResource)
- with mock_contextmanager_generator() as targets[2]:
- keys = targets.keys()
- keys.sort()
- self.assertEqual(keys, [1, 2])
- class C: pass
- blah = C()
- with mock_contextmanager_generator() as blah.foo:
- self.assertEqual(hasattr(blah, "foo"), True)
-
- def testMultipleComplexTargets(self):
- class C:
- def __context__(self): return self
- def __enter__(self): return 1, 2, 3
- def __exit__(self, *a): pass
- targets = {1: [0, 1, 2]}
- with C() as (targets[1][0], targets[1][1], targets[1][2]):
- self.assertEqual(targets, {1: [1, 2, 3]})
- with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]):
- self.assertEqual(targets, {1: [3, 2, 1]})
- with C() as (targets[1], targets[2], targets[3]):
- self.assertEqual(targets, {1: 1, 2: 2, 3: 3})
- class B: pass
- blah = B()
- with C() as (blah.one, blah.two, blah.three):
- self.assertEqual(blah.one, 1)
- self.assertEqual(blah.two, 2)
- self.assertEqual(blah.three, 3)
-
-
-def test_main():
- run_unittest(FailureTestCase, NonexceptionalTestCase,
- NestedNonexceptionalTestCase, ExceptionalTestCase,
- NonLocalFlowControlTestCase,
- AssignmentTargetTestCase)
-
-
-if __name__ == '__main__':
- test_main()
+#!/usr/bin/env python\r
+\r
+"""Unit tests for the with statement specified in PEP 343."""\r
+\r
+__author__ = "Mike Bland"\r
+__email__ = "mbland at acm dot org"\r
+\r
+import unittest\r
+from test.contextmanager import GeneratorContextManager\r
+from test.nested import nested\r
+from test.test_support import run_unittest\r
+\r
+\r
+class MockContextManager(GeneratorContextManager):\r
+ def __init__(self, gen):\r
+ GeneratorContextManager.__init__(self, gen)\r
+ self.context_called = False\r
+ self.enter_called = False\r
+ self.exit_called = False\r
+ self.exit_args = None\r
+\r
+ def __context__(self):\r
+ self.context_called = True\r
+ return GeneratorContextManager.__context__(self)\r
+\r
+ def __enter__(self):\r
+ self.enter_called = True\r
+ return GeneratorContextManager.__enter__(self)\r
+\r
+ def __exit__(self, type, value, traceback):\r
+ self.exit_called = True\r
+ self.exit_args = (type, value, traceback)\r
+ return GeneratorContextManager.__exit__(self, type, value, traceback)\r
+\r
+\r
+def mock_contextmanager(func):\r
+ def helper(*args, **kwds):\r
+ return MockContextManager(func(*args, **kwds))\r
+ return helper\r
+\r
+\r
+class MockResource(object):\r
+ def __init__(self):\r
+ self.yielded = False\r
+ self.stopped = False\r
+\r
+\r
+@mock_contextmanager\r
+def mock_contextmanager_generator():\r
+ mock = MockResource()\r
+ try:\r
+ mock.yielded = True\r
+ yield mock\r
+ finally:\r
+ mock.stopped = True\r
+\r
+\r
+class MockNested(nested):\r
+ def __init__(self, *contexts):\r
+ nested.__init__(self, *contexts)\r
+ self.context_called = False\r
+ self.enter_called = False\r
+ self.exit_called = False\r
+ self.exit_args = None\r
+\r
+ def __context__(self):\r
+ self.context_called = True\r
+ return nested.__context__(self)\r
+\r
+ def __enter__(self):\r
+ self.enter_called = True\r
+ return nested.__enter__(self)\r
+\r
+ def __exit__(self, *exc_info):\r
+ self.exit_called = True\r
+ self.exit_args = exc_info\r
+ return nested.__exit__(self, *exc_info)\r
+\r
+\r
+class FailureTestCase(unittest.TestCase):\r
+ def testNameError(self):\r
+ def fooNotDeclared():\r
+ with foo: pass\r
+ self.assertRaises(NameError, fooNotDeclared)\r
+\r
+ def testContextAttributeError(self):\r
+ class LacksContext(object):\r
+ def __enter__(self):\r
+ pass\r
+\r
+ def __exit__(self, type, value, traceback):\r
+ pass\r
+\r
+ def fooLacksContext():\r
+ foo = LacksContext()\r
+ with foo: pass\r
+ self.assertRaises(AttributeError, fooLacksContext)\r
+\r
+ def testEnterAttributeError(self):\r
+ class LacksEnter(object):\r
+ def __context__(self):\r
+ pass\r
+\r
+ def __exit__(self, type, value, traceback):\r
+ pass\r
+\r
+ def fooLacksEnter():\r
+ foo = LacksEnter()\r
+ with foo: pass\r
+ self.assertRaises(AttributeError, fooLacksEnter)\r
+\r
+ def testExitAttributeError(self):\r
+ class LacksExit(object):\r
+ def __context__(self):\r
+ pass\r
+\r
+ def __enter__(self):\r
+ pass\r
+\r
+ def fooLacksExit():\r
+ foo = LacksExit()\r
+ with foo: pass\r
+ self.assertRaises(AttributeError, fooLacksExit)\r
+\r
+ def assertRaisesSyntaxError(self, codestr):\r
+ def shouldRaiseSyntaxError(s):\r
+ compile(s, '', 'single')\r
+ self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr)\r
+\r
+ def testAssignmentToNoneError(self):\r
+ self.assertRaisesSyntaxError('with mock as None:\n pass')\r
+ self.assertRaisesSyntaxError(\r
+ 'with mock as (None):\n'\r
+ ' pass')\r
+\r
+ def testAssignmentToEmptyTupleError(self):\r
+ self.assertRaisesSyntaxError(\r
+ 'with mock as ():\n'\r
+ ' pass')\r
+\r
+ def testAssignmentToTupleOnlyContainingNoneError(self):\r
+ self.assertRaisesSyntaxError('with mock as None,:\n pass')\r
+ self.assertRaisesSyntaxError(\r
+ 'with mock as (None,):\n'\r
+ ' pass')\r
+\r
+ def testAssignmentToTupleContainingNoneError(self):\r
+ self.assertRaisesSyntaxError(\r
+ 'with mock as (foo, None, bar):\n'\r
+ ' pass')\r
+\r
+ def testContextThrows(self):\r
+ class ContextThrows(object):\r
+ def __context__(self):\r
+ raise RuntimeError("Context threw")\r
+\r
+ def shouldThrow():\r
+ ct = ContextThrows()\r
+ self.foo = None\r
+ with ct as self.foo:\r
+ pass\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+ self.assertEqual(self.foo, None)\r
+\r
+ def testEnterThrows(self):\r
+ class EnterThrows(object):\r
+ def __context__(self):\r
+ return self\r
+\r
+ def __enter__(self):\r
+ raise RuntimeError("Context threw")\r
+\r
+ def __exit__(self, *args):\r
+ pass\r
+\r
+ def shouldThrow():\r
+ ct = EnterThrows()\r
+ self.foo = None\r
+ with ct as self.foo:\r
+ pass\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+ self.assertEqual(self.foo, None)\r
+\r
+ def testExitThrows(self):\r
+ class ExitThrows(object):\r
+ def __context__(self):\r
+ return self\r
+ def __enter__(self):\r
+ return\r
+ def __exit__(self, *args):\r
+ raise RuntimeError(42)\r
+ def shouldThrow():\r
+ with ExitThrows():\r
+ pass\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+\r
+class ContextmanagerAssertionMixin(object):\r
+ TEST_EXCEPTION = RuntimeError("test exception")\r
+\r
+ def assertInWithManagerInvariants(self, mock_manager):\r
+ self.assertTrue(mock_manager.context_called)\r
+ self.assertTrue(mock_manager.enter_called)\r
+ self.assertFalse(mock_manager.exit_called)\r
+ self.assertEqual(mock_manager.exit_args, None)\r
+\r
+ def assertAfterWithManagerInvariants(self, mock_manager, exit_args):\r
+ self.assertTrue(mock_manager.context_called)\r
+ self.assertTrue(mock_manager.enter_called)\r
+ self.assertTrue(mock_manager.exit_called)\r
+ self.assertEqual(mock_manager.exit_args, exit_args)\r
+\r
+ def assertAfterWithManagerInvariantsNoError(self, mock_manager):\r
+ self.assertAfterWithManagerInvariants(mock_manager,\r
+ (None, None, None))\r
+\r
+ def assertInWithGeneratorInvariants(self, mock_generator):\r
+ self.assertTrue(mock_generator.yielded)\r
+ self.assertFalse(mock_generator.stopped)\r
+\r
+ def assertAfterWithGeneratorInvariantsNoError(self, mock_generator):\r
+ self.assertTrue(mock_generator.yielded)\r
+ self.assertTrue(mock_generator.stopped)\r
+\r
+ def raiseTestException(self):\r
+ raise self.TEST_EXCEPTION\r
+\r
+ def assertAfterWithManagerInvariantsWithError(self, mock_manager):\r
+ self.assertTrue(mock_manager.context_called)\r
+ self.assertTrue(mock_manager.enter_called)\r
+ self.assertTrue(mock_manager.exit_called)\r
+ self.assertEqual(mock_manager.exit_args[0], RuntimeError)\r
+ self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)\r
+\r
+ def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):\r
+ self.assertTrue(mock_generator.yielded)\r
+ self.assertTrue(mock_generator.stopped)\r
+\r
+\r
+class NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):\r
+ def testInlineGeneratorSyntax(self):\r
+ with mock_contextmanager_generator():\r
+ pass\r
+\r
+ def testUnboundGenerator(self):\r
+ mock = mock_contextmanager_generator()\r
+ with mock:\r
+ pass\r
+ self.assertAfterWithManagerInvariantsNoError(mock)\r
+\r
+ def testInlineGeneratorBoundSyntax(self):\r
+ with mock_contextmanager_generator() as foo:\r
+ self.assertInWithGeneratorInvariants(foo)\r
+ # FIXME: In the future, we'll try to keep the bound names from leaking\r
+ self.assertAfterWithGeneratorInvariantsNoError(foo)\r
+\r
+ def testInlineGeneratorBoundToExistingVariable(self):\r
+ foo = None\r
+ with mock_contextmanager_generator() as foo:\r
+ self.assertInWithGeneratorInvariants(foo)\r
+ self.assertAfterWithGeneratorInvariantsNoError(foo)\r
+\r
+ def testInlineGeneratorBoundToDottedVariable(self):\r
+ with mock_contextmanager_generator() as self.foo:\r
+ self.assertInWithGeneratorInvariants(self.foo)\r
+ self.assertAfterWithGeneratorInvariantsNoError(self.foo)\r
+\r
+ def testBoundGenerator(self):\r
+ mock = mock_contextmanager_generator()\r
+ with mock as foo:\r
+ self.assertInWithGeneratorInvariants(foo)\r
+ self.assertInWithManagerInvariants(mock)\r
+ self.assertAfterWithGeneratorInvariantsNoError(foo)\r
+ self.assertAfterWithManagerInvariantsNoError(mock)\r
+\r
+ def testNestedSingleStatements(self):\r
+ mock_a = mock_contextmanager_generator()\r
+ with mock_a as foo:\r
+ mock_b = mock_contextmanager_generator()\r
+ with mock_b as bar:\r
+ self.assertInWithManagerInvariants(mock_a)\r
+ self.assertInWithManagerInvariants(mock_b)\r
+ self.assertInWithGeneratorInvariants(foo)\r
+ self.assertInWithGeneratorInvariants(bar)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_b)\r
+ self.assertAfterWithGeneratorInvariantsNoError(bar)\r
+ self.assertInWithManagerInvariants(mock_a)\r
+ self.assertInWithGeneratorInvariants(foo)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_a)\r
+ self.assertAfterWithGeneratorInvariantsNoError(foo)\r
+\r
+\r
+class NestedNonexceptionalTestCase(unittest.TestCase,\r
+ ContextmanagerAssertionMixin):\r
+ def testSingleArgInlineGeneratorSyntax(self):\r
+ with nested(mock_contextmanager_generator()):\r
+ pass\r
+\r
+ def testSingleArgUnbound(self):\r
+ mock_contextmanager = mock_contextmanager_generator()\r
+ mock_nested = MockNested(mock_contextmanager)\r
+ with mock_nested:\r
+ self.assertInWithManagerInvariants(mock_contextmanager)\r
+ self.assertInWithManagerInvariants(mock_nested)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_nested)\r
+\r
+ def testSingleArgBoundToNonTuple(self):\r
+ m = mock_contextmanager_generator()\r
+ # This will bind all the arguments to nested() into a single list\r
+ # assigned to foo.\r
+ with nested(m) as foo:\r
+ self.assertInWithManagerInvariants(m)\r
+ self.assertAfterWithManagerInvariantsNoError(m)\r
+\r
+ def testSingleArgBoundToSingleElementParenthesizedList(self):\r
+ m = mock_contextmanager_generator()\r
+ # This will bind all the arguments to nested() into a single list\r
+ # assigned to foo.\r
+ # FIXME: what should this do: with nested(m) as (foo,):\r
+ with nested(m) as (foo):\r
+ self.assertInWithManagerInvariants(m)\r
+ self.assertAfterWithManagerInvariantsNoError(m)\r
+\r
+ def testSingleArgBoundToMultipleElementTupleError(self):\r
+ def shouldThrowValueError():\r
+ with nested(mock_contextmanager_generator()) as (foo, bar):\r
+ pass\r
+ self.assertRaises(ValueError, shouldThrowValueError)\r
+\r
+ def testSingleArgUnbound(self):\r
+ mock_contextmanager = mock_contextmanager_generator()\r
+ mock_nested = MockNested(mock_contextmanager)\r
+ with mock_nested:\r
+ self.assertInWithManagerInvariants(mock_contextmanager)\r
+ self.assertInWithManagerInvariants(mock_nested)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_nested)\r
+\r
+ def testMultipleArgUnbound(self):\r
+ m = mock_contextmanager_generator()\r
+ n = mock_contextmanager_generator()\r
+ o = mock_contextmanager_generator()\r
+ mock_nested = MockNested(m, n, o)\r
+ with mock_nested:\r
+ self.assertInWithManagerInvariants(m)\r
+ self.assertInWithManagerInvariants(n)\r
+ self.assertInWithManagerInvariants(o)\r
+ self.assertInWithManagerInvariants(mock_nested)\r
+ self.assertAfterWithManagerInvariantsNoError(m)\r
+ self.assertAfterWithManagerInvariantsNoError(n)\r
+ self.assertAfterWithManagerInvariantsNoError(o)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_nested)\r
+\r
+ def testMultipleArgBound(self):\r
+ mock_nested = MockNested(mock_contextmanager_generator(),\r
+ mock_contextmanager_generator(), mock_contextmanager_generator())\r
+ with mock_nested as (m, n, o):\r
+ self.assertInWithGeneratorInvariants(m)\r
+ self.assertInWithGeneratorInvariants(n)\r
+ self.assertInWithGeneratorInvariants(o)\r
+ self.assertInWithManagerInvariants(mock_nested)\r
+ self.assertAfterWithGeneratorInvariantsNoError(m)\r
+ self.assertAfterWithGeneratorInvariantsNoError(n)\r
+ self.assertAfterWithGeneratorInvariantsNoError(o)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_nested)\r
+\r
+\r
+class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):\r
+ def testSingleResource(self):\r
+ cm = mock_contextmanager_generator()\r
+ def shouldThrow():\r
+ with cm as self.resource:\r
+ self.assertInWithManagerInvariants(cm)\r
+ self.assertInWithGeneratorInvariants(self.resource)\r
+ self.raiseTestException()\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+ self.assertAfterWithManagerInvariantsWithError(cm)\r
+ self.assertAfterWithGeneratorInvariantsWithError(self.resource)\r
+\r
+ def testNestedSingleStatements(self):\r
+ mock_a = mock_contextmanager_generator()\r
+ mock_b = mock_contextmanager_generator()\r
+ def shouldThrow():\r
+ with mock_a as self.foo:\r
+ with mock_b as self.bar:\r
+ self.assertInWithManagerInvariants(mock_a)\r
+ self.assertInWithManagerInvariants(mock_b)\r
+ self.assertInWithGeneratorInvariants(self.foo)\r
+ self.assertInWithGeneratorInvariants(self.bar)\r
+ self.raiseTestException()\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+ self.assertAfterWithManagerInvariantsWithError(mock_a)\r
+ self.assertAfterWithManagerInvariantsWithError(mock_b)\r
+ self.assertAfterWithGeneratorInvariantsWithError(self.foo)\r
+ self.assertAfterWithGeneratorInvariantsWithError(self.bar)\r
+\r
+ def testMultipleResourcesInSingleStatement(self):\r
+ cm_a = mock_contextmanager_generator()\r
+ cm_b = mock_contextmanager_generator()\r
+ mock_nested = MockNested(cm_a, cm_b)\r
+ def shouldThrow():\r
+ with mock_nested as (self.resource_a, self.resource_b):\r
+ self.assertInWithManagerInvariants(cm_a)\r
+ self.assertInWithManagerInvariants(cm_b)\r
+ self.assertInWithManagerInvariants(mock_nested)\r
+ self.assertInWithGeneratorInvariants(self.resource_a)\r
+ self.assertInWithGeneratorInvariants(self.resource_b)\r
+ self.raiseTestException()\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+ self.assertAfterWithManagerInvariantsWithError(cm_a)\r
+ self.assertAfterWithManagerInvariantsWithError(cm_b)\r
+ self.assertAfterWithManagerInvariantsWithError(mock_nested)\r
+ self.assertAfterWithGeneratorInvariantsWithError(self.resource_a)\r
+ self.assertAfterWithGeneratorInvariantsWithError(self.resource_b)\r
+\r
+ def testNestedExceptionBeforeInnerStatement(self):\r
+ mock_a = mock_contextmanager_generator()\r
+ mock_b = mock_contextmanager_generator()\r
+ self.bar = None\r
+ def shouldThrow():\r
+ with mock_a as self.foo:\r
+ self.assertInWithManagerInvariants(mock_a)\r
+ self.assertInWithGeneratorInvariants(self.foo)\r
+ self.raiseTestException()\r
+ with mock_b as self.bar:\r
+ pass\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+ self.assertAfterWithManagerInvariantsWithError(mock_a)\r
+ self.assertAfterWithGeneratorInvariantsWithError(self.foo)\r
+\r
+ # The inner statement stuff should never have been touched\r
+ self.assertEqual(self.bar, None)\r
+ self.assertFalse(mock_b.context_called)\r
+ self.assertFalse(mock_b.enter_called)\r
+ self.assertFalse(mock_b.exit_called)\r
+ self.assertEqual(mock_b.exit_args, None)\r
+\r
+ def testNestedExceptionAfterInnerStatement(self):\r
+ mock_a = mock_contextmanager_generator()\r
+ mock_b = mock_contextmanager_generator()\r
+ def shouldThrow():\r
+ with mock_a as self.foo:\r
+ with mock_b as self.bar:\r
+ self.assertInWithManagerInvariants(mock_a)\r
+ self.assertInWithManagerInvariants(mock_b)\r
+ self.assertInWithGeneratorInvariants(self.foo)\r
+ self.assertInWithGeneratorInvariants(self.bar)\r
+ self.raiseTestException()\r
+ self.assertRaises(RuntimeError, shouldThrow)\r
+ self.assertAfterWithManagerInvariantsWithError(mock_a)\r
+ self.assertAfterWithManagerInvariantsNoError(mock_b)\r
+ self.assertAfterWithGeneratorInvariantsWithError(self.foo)\r
+ self.assertAfterWithGeneratorInvariantsNoError(self.bar)\r
+\r
+\r
+class NonLocalFlowControlTestCase(unittest.TestCase):\r
+\r
+ def testWithBreak(self):\r
+ counter = 0\r
+ while True:\r
+ counter += 1\r
+ with mock_contextmanager_generator():\r
+ counter += 10\r
+ break\r
+ counter += 100 # Not reached\r
+ self.assertEqual(counter, 11)\r
+\r
+ def testWithContinue(self):\r
+ counter = 0\r
+ while True:\r
+ counter += 1\r
+ if counter > 2:\r
+ break\r
+ with mock_contextmanager_generator():\r
+ counter += 10\r
+ continue\r
+ counter += 100 # Not reached\r
+ self.assertEqual(counter, 12)\r
+\r
+ def testWithReturn(self):\r
+ def foo():\r
+ counter = 0\r
+ while True:\r
+ counter += 1\r
+ with mock_contextmanager_generator():\r
+ counter += 10\r
+ return counter\r
+ counter += 100 # Not reached\r
+ self.assertEqual(foo(), 11)\r
+\r
+ def testWithYield(self):\r
+ def gen():\r
+ with mock_contextmanager_generator():\r
+ yield 12\r
+ yield 13\r
+ x = list(gen())\r
+ self.assertEqual(x, [12, 13])\r
+\r
+ def testWithRaise(self):\r
+ counter = 0\r
+ try:\r
+ counter += 1\r
+ with mock_contextmanager_generator():\r
+ counter += 10\r
+ raise RuntimeError\r
+ counter += 100 # Not reached\r
+ except RuntimeError:\r
+ self.assertEqual(counter, 11)\r
+ else:\r
+ self.fail("Didn't raise RuntimeError")\r
+\r
+\r
+class AssignmentTargetTestCase(unittest.TestCase):\r
+\r
+ def testSingleComplexTarget(self):\r
+ targets = {1: [0, 1, 2]}\r
+ with mock_contextmanager_generator() as targets[1][0]:\r
+ self.assertEqual(targets.keys(), [1])\r
+ self.assertEqual(targets[1][0].__class__, MockResource)\r
+ with mock_contextmanager_generator() as targets.values()[0][1]:\r
+ self.assertEqual(targets.keys(), [1])\r
+ self.assertEqual(targets[1][1].__class__, MockResource)\r
+ with mock_contextmanager_generator() as targets[2]:\r
+ keys = targets.keys()\r
+ keys.sort()\r
+ self.assertEqual(keys, [1, 2])\r
+ class C: pass\r
+ blah = C()\r
+ with mock_contextmanager_generator() as blah.foo:\r
+ self.assertEqual(hasattr(blah, "foo"), True)\r
+\r
+ def testMultipleComplexTargets(self):\r
+ class C:\r
+ def __context__(self): return self\r
+ def __enter__(self): return 1, 2, 3\r
+ def __exit__(self, *a): pass\r
+ targets = {1: [0, 1, 2]}\r
+ with C() as (targets[1][0], targets[1][1], targets[1][2]):\r
+ self.assertEqual(targets, {1: [1, 2, 3]})\r
+ with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]):\r
+ self.assertEqual(targets, {1: [3, 2, 1]})\r
+ with C() as (targets[1], targets[2], targets[3]):\r
+ self.assertEqual(targets, {1: 1, 2: 2, 3: 3})\r
+ class B: pass\r
+ blah = B()\r
+ with C() as (blah.one, blah.two, blah.three):\r
+ self.assertEqual(blah.one, 1)\r
+ self.assertEqual(blah.two, 2)\r
+ self.assertEqual(blah.three, 3)\r
+\r
+\r
+def test_main():\r
+ run_unittest(FailureTestCase, NonexceptionalTestCase,\r
+ NestedNonexceptionalTestCase, ExceptionalTestCase,\r
+ NonLocalFlowControlTestCase,\r
+ AssignmentTargetTestCase)\r
+\r
+\r
+if __name__ == '__main__':\r
+ test_main()\r