Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """Unit tests for the with statement specified in PEP 343.""" |
| 4 | |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 5 | from __future__ import with_statement |
| 6 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 7 | __author__ = "Mike Bland" |
| 8 | __email__ = "mbland at acm dot org" |
| 9 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 10 | import sys |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 11 | import unittest |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 12 | from collections import deque |
| 13 | from contextlib import GeneratorContextManager, contextmanager |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 14 | from test.test_support import run_unittest |
| 15 | |
| 16 | |
| 17 | class MockContextManager(GeneratorContextManager): |
| 18 | def __init__(self, gen): |
| 19 | GeneratorContextManager.__init__(self, gen) |
| 20 | self.context_called = False |
| 21 | self.enter_called = False |
| 22 | self.exit_called = False |
| 23 | self.exit_args = None |
| 24 | |
| 25 | def __context__(self): |
| 26 | self.context_called = True |
| 27 | return GeneratorContextManager.__context__(self) |
| 28 | |
| 29 | def __enter__(self): |
| 30 | self.enter_called = True |
| 31 | return GeneratorContextManager.__enter__(self) |
| 32 | |
| 33 | def __exit__(self, type, value, traceback): |
| 34 | self.exit_called = True |
| 35 | self.exit_args = (type, value, traceback) |
| 36 | return GeneratorContextManager.__exit__(self, type, value, traceback) |
| 37 | |
| 38 | |
| 39 | def mock_contextmanager(func): |
| 40 | def helper(*args, **kwds): |
| 41 | return MockContextManager(func(*args, **kwds)) |
| 42 | return helper |
| 43 | |
| 44 | |
| 45 | class MockResource(object): |
| 46 | def __init__(self): |
| 47 | self.yielded = False |
| 48 | self.stopped = False |
| 49 | |
| 50 | |
| 51 | @mock_contextmanager |
| 52 | def mock_contextmanager_generator(): |
| 53 | mock = MockResource() |
| 54 | try: |
| 55 | mock.yielded = True |
| 56 | yield mock |
| 57 | finally: |
| 58 | mock.stopped = True |
| 59 | |
| 60 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 61 | class Nested(object): |
| 62 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 63 | def __init__(self, *contexts): |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 64 | self.contexts = contexts |
| 65 | self.entered = None |
| 66 | |
| 67 | def __context__(self): |
| 68 | return self |
| 69 | |
| 70 | def __enter__(self): |
| 71 | if self.entered is not None: |
| 72 | raise RuntimeError("Context is not reentrant") |
| 73 | self.entered = deque() |
| 74 | vars = [] |
| 75 | try: |
| 76 | for context in self.contexts: |
| 77 | mgr = context.__context__() |
| 78 | vars.append(mgr.__enter__()) |
| 79 | self.entered.appendleft(mgr) |
| 80 | except: |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 81 | if not self.__exit__(*sys.exc_info()): |
| 82 | raise |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 83 | return vars |
| 84 | |
| 85 | def __exit__(self, *exc_info): |
| 86 | # Behave like nested with statements |
| 87 | # first in, last out |
| 88 | # New exceptions override old ones |
| 89 | ex = exc_info |
| 90 | for mgr in self.entered: |
| 91 | try: |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 92 | if mgr.__exit__(*ex): |
| 93 | ex = (None, None, None) |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 94 | except: |
| 95 | ex = sys.exc_info() |
| 96 | self.entered = None |
| 97 | if ex is not exc_info: |
| 98 | raise ex[0], ex[1], ex[2] |
| 99 | |
| 100 | |
| 101 | class MockNested(Nested): |
| 102 | def __init__(self, *contexts): |
| 103 | Nested.__init__(self, *contexts) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 104 | self.context_called = False |
| 105 | self.enter_called = False |
| 106 | self.exit_called = False |
| 107 | self.exit_args = None |
| 108 | |
| 109 | def __context__(self): |
| 110 | self.context_called = True |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 111 | return Nested.__context__(self) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 112 | |
| 113 | def __enter__(self): |
| 114 | self.enter_called = True |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 115 | return Nested.__enter__(self) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 116 | |
| 117 | def __exit__(self, *exc_info): |
| 118 | self.exit_called = True |
| 119 | self.exit_args = exc_info |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 120 | return Nested.__exit__(self, *exc_info) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | class FailureTestCase(unittest.TestCase): |
| 124 | def testNameError(self): |
| 125 | def fooNotDeclared(): |
| 126 | with foo: pass |
| 127 | self.assertRaises(NameError, fooNotDeclared) |
| 128 | |
| 129 | def testContextAttributeError(self): |
| 130 | class LacksContext(object): |
| 131 | def __enter__(self): |
| 132 | pass |
| 133 | |
| 134 | def __exit__(self, type, value, traceback): |
| 135 | pass |
| 136 | |
| 137 | def fooLacksContext(): |
| 138 | foo = LacksContext() |
| 139 | with foo: pass |
| 140 | self.assertRaises(AttributeError, fooLacksContext) |
| 141 | |
| 142 | def testEnterAttributeError(self): |
| 143 | class LacksEnter(object): |
| 144 | def __context__(self): |
| 145 | pass |
| 146 | |
| 147 | def __exit__(self, type, value, traceback): |
| 148 | pass |
| 149 | |
| 150 | def fooLacksEnter(): |
| 151 | foo = LacksEnter() |
| 152 | with foo: pass |
| 153 | self.assertRaises(AttributeError, fooLacksEnter) |
| 154 | |
| 155 | def testExitAttributeError(self): |
| 156 | class LacksExit(object): |
| 157 | def __context__(self): |
| 158 | pass |
| 159 | |
| 160 | def __enter__(self): |
| 161 | pass |
| 162 | |
| 163 | def fooLacksExit(): |
| 164 | foo = LacksExit() |
| 165 | with foo: pass |
| 166 | self.assertRaises(AttributeError, fooLacksExit) |
| 167 | |
| 168 | def assertRaisesSyntaxError(self, codestr): |
| 169 | def shouldRaiseSyntaxError(s): |
| 170 | compile(s, '', 'single') |
| 171 | self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr) |
| 172 | |
| 173 | def testAssignmentToNoneError(self): |
| 174 | self.assertRaisesSyntaxError('with mock as None:\n pass') |
| 175 | self.assertRaisesSyntaxError( |
| 176 | 'with mock as (None):\n' |
| 177 | ' pass') |
| 178 | |
| 179 | def testAssignmentToEmptyTupleError(self): |
| 180 | self.assertRaisesSyntaxError( |
| 181 | 'with mock as ():\n' |
| 182 | ' pass') |
| 183 | |
| 184 | def testAssignmentToTupleOnlyContainingNoneError(self): |
| 185 | self.assertRaisesSyntaxError('with mock as None,:\n pass') |
| 186 | self.assertRaisesSyntaxError( |
| 187 | 'with mock as (None,):\n' |
| 188 | ' pass') |
| 189 | |
| 190 | def testAssignmentToTupleContainingNoneError(self): |
| 191 | self.assertRaisesSyntaxError( |
| 192 | 'with mock as (foo, None, bar):\n' |
| 193 | ' pass') |
| 194 | |
| 195 | def testContextThrows(self): |
| 196 | class ContextThrows(object): |
| 197 | def __context__(self): |
| 198 | raise RuntimeError("Context threw") |
| 199 | |
| 200 | def shouldThrow(): |
| 201 | ct = ContextThrows() |
| 202 | self.foo = None |
| 203 | with ct as self.foo: |
| 204 | pass |
| 205 | self.assertRaises(RuntimeError, shouldThrow) |
| 206 | self.assertEqual(self.foo, None) |
| 207 | |
| 208 | def testEnterThrows(self): |
| 209 | class EnterThrows(object): |
| 210 | def __context__(self): |
| 211 | return self |
| 212 | |
| 213 | def __enter__(self): |
| 214 | raise RuntimeError("Context threw") |
| 215 | |
| 216 | def __exit__(self, *args): |
| 217 | pass |
| 218 | |
| 219 | def shouldThrow(): |
| 220 | ct = EnterThrows() |
| 221 | self.foo = None |
| 222 | with ct as self.foo: |
| 223 | pass |
| 224 | self.assertRaises(RuntimeError, shouldThrow) |
| 225 | self.assertEqual(self.foo, None) |
| 226 | |
| 227 | def testExitThrows(self): |
| 228 | class ExitThrows(object): |
| 229 | def __context__(self): |
| 230 | return self |
| 231 | def __enter__(self): |
| 232 | return |
| 233 | def __exit__(self, *args): |
| 234 | raise RuntimeError(42) |
| 235 | def shouldThrow(): |
| 236 | with ExitThrows(): |
| 237 | pass |
| 238 | self.assertRaises(RuntimeError, shouldThrow) |
| 239 | |
| 240 | class ContextmanagerAssertionMixin(object): |
| 241 | TEST_EXCEPTION = RuntimeError("test exception") |
| 242 | |
| 243 | def assertInWithManagerInvariants(self, mock_manager): |
| 244 | self.assertTrue(mock_manager.context_called) |
| 245 | self.assertTrue(mock_manager.enter_called) |
| 246 | self.assertFalse(mock_manager.exit_called) |
| 247 | self.assertEqual(mock_manager.exit_args, None) |
| 248 | |
| 249 | def assertAfterWithManagerInvariants(self, mock_manager, exit_args): |
| 250 | self.assertTrue(mock_manager.context_called) |
| 251 | self.assertTrue(mock_manager.enter_called) |
| 252 | self.assertTrue(mock_manager.exit_called) |
| 253 | self.assertEqual(mock_manager.exit_args, exit_args) |
| 254 | |
| 255 | def assertAfterWithManagerInvariantsNoError(self, mock_manager): |
| 256 | self.assertAfterWithManagerInvariants(mock_manager, |
| 257 | (None, None, None)) |
| 258 | |
| 259 | def assertInWithGeneratorInvariants(self, mock_generator): |
| 260 | self.assertTrue(mock_generator.yielded) |
| 261 | self.assertFalse(mock_generator.stopped) |
| 262 | |
| 263 | def assertAfterWithGeneratorInvariantsNoError(self, mock_generator): |
| 264 | self.assertTrue(mock_generator.yielded) |
| 265 | self.assertTrue(mock_generator.stopped) |
| 266 | |
| 267 | def raiseTestException(self): |
| 268 | raise self.TEST_EXCEPTION |
| 269 | |
| 270 | def assertAfterWithManagerInvariantsWithError(self, mock_manager): |
| 271 | self.assertTrue(mock_manager.context_called) |
| 272 | self.assertTrue(mock_manager.enter_called) |
| 273 | self.assertTrue(mock_manager.exit_called) |
| 274 | self.assertEqual(mock_manager.exit_args[0], RuntimeError) |
| 275 | self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION) |
| 276 | |
| 277 | def assertAfterWithGeneratorInvariantsWithError(self, mock_generator): |
| 278 | self.assertTrue(mock_generator.yielded) |
| 279 | self.assertTrue(mock_generator.stopped) |
| 280 | |
| 281 | |
| 282 | class NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): |
| 283 | def testInlineGeneratorSyntax(self): |
| 284 | with mock_contextmanager_generator(): |
| 285 | pass |
| 286 | |
| 287 | def testUnboundGenerator(self): |
| 288 | mock = mock_contextmanager_generator() |
| 289 | with mock: |
| 290 | pass |
| 291 | self.assertAfterWithManagerInvariantsNoError(mock) |
| 292 | |
| 293 | def testInlineGeneratorBoundSyntax(self): |
| 294 | with mock_contextmanager_generator() as foo: |
| 295 | self.assertInWithGeneratorInvariants(foo) |
| 296 | # FIXME: In the future, we'll try to keep the bound names from leaking |
| 297 | self.assertAfterWithGeneratorInvariantsNoError(foo) |
| 298 | |
| 299 | def testInlineGeneratorBoundToExistingVariable(self): |
| 300 | foo = None |
| 301 | with mock_contextmanager_generator() as foo: |
| 302 | self.assertInWithGeneratorInvariants(foo) |
| 303 | self.assertAfterWithGeneratorInvariantsNoError(foo) |
| 304 | |
| 305 | def testInlineGeneratorBoundToDottedVariable(self): |
| 306 | with mock_contextmanager_generator() as self.foo: |
| 307 | self.assertInWithGeneratorInvariants(self.foo) |
| 308 | self.assertAfterWithGeneratorInvariantsNoError(self.foo) |
| 309 | |
| 310 | def testBoundGenerator(self): |
| 311 | mock = mock_contextmanager_generator() |
| 312 | with mock as foo: |
| 313 | self.assertInWithGeneratorInvariants(foo) |
| 314 | self.assertInWithManagerInvariants(mock) |
| 315 | self.assertAfterWithGeneratorInvariantsNoError(foo) |
| 316 | self.assertAfterWithManagerInvariantsNoError(mock) |
| 317 | |
| 318 | def testNestedSingleStatements(self): |
| 319 | mock_a = mock_contextmanager_generator() |
| 320 | with mock_a as foo: |
| 321 | mock_b = mock_contextmanager_generator() |
| 322 | with mock_b as bar: |
| 323 | self.assertInWithManagerInvariants(mock_a) |
| 324 | self.assertInWithManagerInvariants(mock_b) |
| 325 | self.assertInWithGeneratorInvariants(foo) |
| 326 | self.assertInWithGeneratorInvariants(bar) |
| 327 | self.assertAfterWithManagerInvariantsNoError(mock_b) |
| 328 | self.assertAfterWithGeneratorInvariantsNoError(bar) |
| 329 | self.assertInWithManagerInvariants(mock_a) |
| 330 | self.assertInWithGeneratorInvariants(foo) |
| 331 | self.assertAfterWithManagerInvariantsNoError(mock_a) |
| 332 | self.assertAfterWithGeneratorInvariantsNoError(foo) |
| 333 | |
| 334 | |
| 335 | class NestedNonexceptionalTestCase(unittest.TestCase, |
| 336 | ContextmanagerAssertionMixin): |
| 337 | def testSingleArgInlineGeneratorSyntax(self): |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 338 | with Nested(mock_contextmanager_generator()): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 339 | pass |
| 340 | |
| 341 | def testSingleArgUnbound(self): |
| 342 | mock_contextmanager = mock_contextmanager_generator() |
| 343 | mock_nested = MockNested(mock_contextmanager) |
| 344 | with mock_nested: |
| 345 | self.assertInWithManagerInvariants(mock_contextmanager) |
| 346 | self.assertInWithManagerInvariants(mock_nested) |
| 347 | self.assertAfterWithManagerInvariantsNoError(mock_contextmanager) |
| 348 | self.assertAfterWithManagerInvariantsNoError(mock_nested) |
| 349 | |
| 350 | def testSingleArgBoundToNonTuple(self): |
| 351 | m = mock_contextmanager_generator() |
| 352 | # This will bind all the arguments to nested() into a single list |
| 353 | # assigned to foo. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 354 | with Nested(m) as foo: |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 355 | self.assertInWithManagerInvariants(m) |
| 356 | self.assertAfterWithManagerInvariantsNoError(m) |
| 357 | |
| 358 | def testSingleArgBoundToSingleElementParenthesizedList(self): |
| 359 | m = mock_contextmanager_generator() |
| 360 | # This will bind all the arguments to nested() into a single list |
| 361 | # assigned to foo. |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 362 | with Nested(m) as (foo): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 363 | self.assertInWithManagerInvariants(m) |
| 364 | self.assertAfterWithManagerInvariantsNoError(m) |
| 365 | |
| 366 | def testSingleArgBoundToMultipleElementTupleError(self): |
| 367 | def shouldThrowValueError(): |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 368 | with Nested(mock_contextmanager_generator()) as (foo, bar): |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 369 | pass |
| 370 | self.assertRaises(ValueError, shouldThrowValueError) |
| 371 | |
| 372 | def testSingleArgUnbound(self): |
| 373 | mock_contextmanager = mock_contextmanager_generator() |
| 374 | mock_nested = MockNested(mock_contextmanager) |
| 375 | with mock_nested: |
| 376 | self.assertInWithManagerInvariants(mock_contextmanager) |
| 377 | self.assertInWithManagerInvariants(mock_nested) |
| 378 | self.assertAfterWithManagerInvariantsNoError(mock_contextmanager) |
| 379 | self.assertAfterWithManagerInvariantsNoError(mock_nested) |
| 380 | |
| 381 | def testMultipleArgUnbound(self): |
| 382 | m = mock_contextmanager_generator() |
| 383 | n = mock_contextmanager_generator() |
| 384 | o = mock_contextmanager_generator() |
| 385 | mock_nested = MockNested(m, n, o) |
| 386 | with mock_nested: |
| 387 | self.assertInWithManagerInvariants(m) |
| 388 | self.assertInWithManagerInvariants(n) |
| 389 | self.assertInWithManagerInvariants(o) |
| 390 | self.assertInWithManagerInvariants(mock_nested) |
| 391 | self.assertAfterWithManagerInvariantsNoError(m) |
| 392 | self.assertAfterWithManagerInvariantsNoError(n) |
| 393 | self.assertAfterWithManagerInvariantsNoError(o) |
| 394 | self.assertAfterWithManagerInvariantsNoError(mock_nested) |
| 395 | |
| 396 | def testMultipleArgBound(self): |
| 397 | mock_nested = MockNested(mock_contextmanager_generator(), |
| 398 | mock_contextmanager_generator(), mock_contextmanager_generator()) |
| 399 | with mock_nested as (m, n, o): |
| 400 | self.assertInWithGeneratorInvariants(m) |
| 401 | self.assertInWithGeneratorInvariants(n) |
| 402 | self.assertInWithGeneratorInvariants(o) |
| 403 | self.assertInWithManagerInvariants(mock_nested) |
| 404 | self.assertAfterWithGeneratorInvariantsNoError(m) |
| 405 | self.assertAfterWithGeneratorInvariantsNoError(n) |
| 406 | self.assertAfterWithGeneratorInvariantsNoError(o) |
| 407 | self.assertAfterWithManagerInvariantsNoError(mock_nested) |
| 408 | |
| 409 | |
| 410 | class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): |
| 411 | def testSingleResource(self): |
| 412 | cm = mock_contextmanager_generator() |
| 413 | def shouldThrow(): |
| 414 | with cm as self.resource: |
| 415 | self.assertInWithManagerInvariants(cm) |
| 416 | self.assertInWithGeneratorInvariants(self.resource) |
| 417 | self.raiseTestException() |
| 418 | self.assertRaises(RuntimeError, shouldThrow) |
| 419 | self.assertAfterWithManagerInvariantsWithError(cm) |
| 420 | self.assertAfterWithGeneratorInvariantsWithError(self.resource) |
| 421 | |
| 422 | def testNestedSingleStatements(self): |
| 423 | mock_a = mock_contextmanager_generator() |
| 424 | mock_b = mock_contextmanager_generator() |
| 425 | def shouldThrow(): |
| 426 | with mock_a as self.foo: |
| 427 | with mock_b as self.bar: |
| 428 | self.assertInWithManagerInvariants(mock_a) |
| 429 | self.assertInWithManagerInvariants(mock_b) |
| 430 | self.assertInWithGeneratorInvariants(self.foo) |
| 431 | self.assertInWithGeneratorInvariants(self.bar) |
| 432 | self.raiseTestException() |
| 433 | self.assertRaises(RuntimeError, shouldThrow) |
| 434 | self.assertAfterWithManagerInvariantsWithError(mock_a) |
| 435 | self.assertAfterWithManagerInvariantsWithError(mock_b) |
| 436 | self.assertAfterWithGeneratorInvariantsWithError(self.foo) |
| 437 | self.assertAfterWithGeneratorInvariantsWithError(self.bar) |
| 438 | |
| 439 | def testMultipleResourcesInSingleStatement(self): |
| 440 | cm_a = mock_contextmanager_generator() |
| 441 | cm_b = mock_contextmanager_generator() |
| 442 | mock_nested = MockNested(cm_a, cm_b) |
| 443 | def shouldThrow(): |
| 444 | with mock_nested as (self.resource_a, self.resource_b): |
| 445 | self.assertInWithManagerInvariants(cm_a) |
| 446 | self.assertInWithManagerInvariants(cm_b) |
| 447 | self.assertInWithManagerInvariants(mock_nested) |
| 448 | self.assertInWithGeneratorInvariants(self.resource_a) |
| 449 | self.assertInWithGeneratorInvariants(self.resource_b) |
| 450 | self.raiseTestException() |
| 451 | self.assertRaises(RuntimeError, shouldThrow) |
| 452 | self.assertAfterWithManagerInvariantsWithError(cm_a) |
| 453 | self.assertAfterWithManagerInvariantsWithError(cm_b) |
| 454 | self.assertAfterWithManagerInvariantsWithError(mock_nested) |
| 455 | self.assertAfterWithGeneratorInvariantsWithError(self.resource_a) |
| 456 | self.assertAfterWithGeneratorInvariantsWithError(self.resource_b) |
| 457 | |
| 458 | def testNestedExceptionBeforeInnerStatement(self): |
| 459 | mock_a = mock_contextmanager_generator() |
| 460 | mock_b = mock_contextmanager_generator() |
| 461 | self.bar = None |
| 462 | def shouldThrow(): |
| 463 | with mock_a as self.foo: |
| 464 | self.assertInWithManagerInvariants(mock_a) |
| 465 | self.assertInWithGeneratorInvariants(self.foo) |
| 466 | self.raiseTestException() |
| 467 | with mock_b as self.bar: |
| 468 | pass |
| 469 | self.assertRaises(RuntimeError, shouldThrow) |
| 470 | self.assertAfterWithManagerInvariantsWithError(mock_a) |
| 471 | self.assertAfterWithGeneratorInvariantsWithError(self.foo) |
| 472 | |
| 473 | # The inner statement stuff should never have been touched |
| 474 | self.assertEqual(self.bar, None) |
| 475 | self.assertFalse(mock_b.context_called) |
| 476 | self.assertFalse(mock_b.enter_called) |
| 477 | self.assertFalse(mock_b.exit_called) |
| 478 | self.assertEqual(mock_b.exit_args, None) |
| 479 | |
| 480 | def testNestedExceptionAfterInnerStatement(self): |
| 481 | mock_a = mock_contextmanager_generator() |
| 482 | mock_b = mock_contextmanager_generator() |
| 483 | def shouldThrow(): |
| 484 | with mock_a as self.foo: |
| 485 | with mock_b as self.bar: |
| 486 | self.assertInWithManagerInvariants(mock_a) |
| 487 | self.assertInWithManagerInvariants(mock_b) |
| 488 | self.assertInWithGeneratorInvariants(self.foo) |
| 489 | self.assertInWithGeneratorInvariants(self.bar) |
| 490 | self.raiseTestException() |
| 491 | self.assertRaises(RuntimeError, shouldThrow) |
| 492 | self.assertAfterWithManagerInvariantsWithError(mock_a) |
| 493 | self.assertAfterWithManagerInvariantsNoError(mock_b) |
| 494 | self.assertAfterWithGeneratorInvariantsWithError(self.foo) |
| 495 | self.assertAfterWithGeneratorInvariantsNoError(self.bar) |
| 496 | |
| 497 | |
| 498 | class NonLocalFlowControlTestCase(unittest.TestCase): |
| 499 | |
| 500 | def testWithBreak(self): |
| 501 | counter = 0 |
| 502 | while True: |
| 503 | counter += 1 |
| 504 | with mock_contextmanager_generator(): |
| 505 | counter += 10 |
| 506 | break |
| 507 | counter += 100 # Not reached |
| 508 | self.assertEqual(counter, 11) |
| 509 | |
| 510 | def testWithContinue(self): |
| 511 | counter = 0 |
| 512 | while True: |
| 513 | counter += 1 |
| 514 | if counter > 2: |
| 515 | break |
| 516 | with mock_contextmanager_generator(): |
| 517 | counter += 10 |
| 518 | continue |
| 519 | counter += 100 # Not reached |
| 520 | self.assertEqual(counter, 12) |
| 521 | |
| 522 | def testWithReturn(self): |
| 523 | def foo(): |
| 524 | counter = 0 |
| 525 | while True: |
| 526 | counter += 1 |
| 527 | with mock_contextmanager_generator(): |
| 528 | counter += 10 |
| 529 | return counter |
| 530 | counter += 100 # Not reached |
| 531 | self.assertEqual(foo(), 11) |
| 532 | |
| 533 | def testWithYield(self): |
| 534 | def gen(): |
| 535 | with mock_contextmanager_generator(): |
| 536 | yield 12 |
| 537 | yield 13 |
| 538 | x = list(gen()) |
| 539 | self.assertEqual(x, [12, 13]) |
| 540 | |
| 541 | def testWithRaise(self): |
| 542 | counter = 0 |
| 543 | try: |
| 544 | counter += 1 |
| 545 | with mock_contextmanager_generator(): |
| 546 | counter += 10 |
| 547 | raise RuntimeError |
| 548 | counter += 100 # Not reached |
| 549 | except RuntimeError: |
| 550 | self.assertEqual(counter, 11) |
| 551 | else: |
| 552 | self.fail("Didn't raise RuntimeError") |
| 553 | |
| 554 | |
| 555 | class AssignmentTargetTestCase(unittest.TestCase): |
| 556 | |
| 557 | def testSingleComplexTarget(self): |
| 558 | targets = {1: [0, 1, 2]} |
| 559 | with mock_contextmanager_generator() as targets[1][0]: |
| 560 | self.assertEqual(targets.keys(), [1]) |
| 561 | self.assertEqual(targets[1][0].__class__, MockResource) |
| 562 | with mock_contextmanager_generator() as targets.values()[0][1]: |
| 563 | self.assertEqual(targets.keys(), [1]) |
| 564 | self.assertEqual(targets[1][1].__class__, MockResource) |
| 565 | with mock_contextmanager_generator() as targets[2]: |
| 566 | keys = targets.keys() |
| 567 | keys.sort() |
| 568 | self.assertEqual(keys, [1, 2]) |
| 569 | class C: pass |
| 570 | blah = C() |
| 571 | with mock_contextmanager_generator() as blah.foo: |
| 572 | self.assertEqual(hasattr(blah, "foo"), True) |
| 573 | |
| 574 | def testMultipleComplexTargets(self): |
| 575 | class C: |
| 576 | def __context__(self): return self |
| 577 | def __enter__(self): return 1, 2, 3 |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 578 | def __exit__(self, t, v, tb): pass |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 579 | targets = {1: [0, 1, 2]} |
| 580 | with C() as (targets[1][0], targets[1][1], targets[1][2]): |
| 581 | self.assertEqual(targets, {1: [1, 2, 3]}) |
| 582 | with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]): |
| 583 | self.assertEqual(targets, {1: [3, 2, 1]}) |
| 584 | with C() as (targets[1], targets[2], targets[3]): |
| 585 | self.assertEqual(targets, {1: 1, 2: 2, 3: 3}) |
| 586 | class B: pass |
| 587 | blah = B() |
| 588 | with C() as (blah.one, blah.two, blah.three): |
| 589 | self.assertEqual(blah.one, 1) |
| 590 | self.assertEqual(blah.two, 2) |
| 591 | self.assertEqual(blah.three, 3) |
| 592 | |
| 593 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 594 | class ExitSwallowsExceptionTestCase(unittest.TestCase): |
| 595 | |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 596 | def testExitTrueSwallowsException(self): |
| 597 | class AfricanSwallow: |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 598 | def __context__(self): return self |
| 599 | def __enter__(self): pass |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 600 | def __exit__(self, t, v, tb): return True |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 601 | try: |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 602 | with AfricanSwallow(): |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 603 | 1/0 |
| 604 | except ZeroDivisionError: |
| 605 | self.fail("ZeroDivisionError should have been swallowed") |
| 606 | |
Guido van Rossum | f669436 | 2006-03-10 02:28:35 +0000 | [diff] [blame] | 607 | def testExitFalseDoesntSwallowException(self): |
| 608 | class EuropeanSwallow: |
| 609 | def __context__(self): return self |
| 610 | def __enter__(self): pass |
| 611 | def __exit__(self, t, v, tb): return False |
| 612 | try: |
| 613 | with EuropeanSwallow(): |
| 614 | 1/0 |
| 615 | except ZeroDivisionError: |
| 616 | pass |
| 617 | else: |
| 618 | self.fail("ZeroDivisionError should have been raised") |
| 619 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 620 | |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 621 | def test_main(): |
| 622 | run_unittest(FailureTestCase, NonexceptionalTestCase, |
| 623 | NestedNonexceptionalTestCase, ExceptionalTestCase, |
| 624 | NonLocalFlowControlTestCase, |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 625 | AssignmentTargetTestCase, |
| 626 | ExitSwallowsExceptionTestCase) |
Tim Peters | 400cbc3 | 2006-02-28 18:44:41 +0000 | [diff] [blame] | 627 | |
| 628 | |
| 629 | if __name__ == '__main__': |
| 630 | test_main() |