Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_contextlib.TestContextDecorator Class Reference

List of all members.

Public Member Functions

def test_contextdecorator
def test_contextdecorator_with_exception
def test_decorator
def test_decorator_with_exception
def test_decorating_method
def test_typo_enter
def test_typo_exit
def test_contextdecorator_as_mixin
def test_contextmanager_as_decorator

Public Attributes

 a
 b
 c
 started
 exc

Detailed Description

Definition at line 220 of file test_contextlib.py.


Member Function Documentation

Definition at line 222 of file test_contextlib.py.

00222 
00223     def test_contextdecorator(self):
00224         context = mycontext()
00225         with context as result:
00226             self.assertIs(result, context)
00227             self.assertTrue(context.started)
00228 
00229         self.assertEqual(context.exc, (None, None, None))
00230 

Here is the call graph for this function:

Definition at line 328 of file test_contextlib.py.

00328 
00329     def test_contextdecorator_as_mixin(self):
00330         class somecontext(object):
00331             started = False
00332             exc = None
00333 
00334             def __enter__(self):
00335                 self.started = True
00336                 return self
00337 
00338             def __exit__(self, *exc):
00339                 self.exc = exc
00340 
00341         class mycontext(somecontext, ContextDecorator):
00342             pass
00343 
00344         context = mycontext()
00345         @context
00346         def test():
00347             self.assertIsNone(context.exc)
00348             self.assertTrue(context.started)
00349         test()
00350         self.assertEqual(context.exc, (None, None, None))
00351 

Here is the call graph for this function:

Definition at line 231 of file test_contextlib.py.

00231 
00232     def test_contextdecorator_with_exception(self):
00233         context = mycontext()
00234 
00235         with self.assertRaisesRegex(NameError, 'foo'):
00236             with context:
00237                 raise NameError('foo')
00238         self.assertIsNotNone(context.exc)
00239         self.assertIs(context.exc[0], NameError)
00240 
00241         context = mycontext()
00242         context.catch = True
00243         with context:
00244             raise NameError('foo')
00245         self.assertIsNotNone(context.exc)
00246         self.assertIs(context.exc[0], NameError)
00247 

Here is the call graph for this function:

Definition at line 352 of file test_contextlib.py.

00352 
00353     def test_contextmanager_as_decorator(self):
00354         @contextmanager
00355         def woohoo(y):
00356             state.append(y)
00357             yield
00358             state.append(999)
00359 
00360         state = []
00361         @woohoo(1)
00362         def test(x):
00363             self.assertEqual(state, [1])
00364             state.append(x)
00365         test('something')
00366         self.assertEqual(state, [1, 'something', 999])
00367 
00368         # Issue #11647: Ensure the decorated function is 'reusable'
00369         state = []
00370         test('something else')
00371         self.assertEqual(state, [1, 'something else', 999])
00372 
00373 
# This is needed to make the test actually run under regrtest.py!

Here is the call graph for this function:

Definition at line 274 of file test_contextlib.py.

00274 
00275     def test_decorating_method(self):
00276         context = mycontext()
00277 
00278         class Test(object):
00279 
00280             @context
00281             def method(self, a, b, c=None):
00282                 self.a = a
00283                 self.b = b
00284                 self.c = c
00285 
00286         # these tests are for argument passing when used as a decorator
00287         test = Test()
00288         test.method(1, 2)
00289         self.assertEqual(test.a, 1)
00290         self.assertEqual(test.b, 2)
00291         self.assertEqual(test.c, None)
00292 
00293         test = Test()
00294         test.method('a', 'b', 'c')
00295         self.assertEqual(test.a, 'a')
00296         self.assertEqual(test.b, 'b')
00297         self.assertEqual(test.c, 'c')
00298 
00299         test = Test()
00300         test.method(a=1, b=2)
00301         self.assertEqual(test.a, 1)
00302         self.assertEqual(test.b, 2)
00303 

Definition at line 248 of file test_contextlib.py.

00248 
00249     def test_decorator(self):
00250         context = mycontext()
00251 
00252         @context
00253         def test():
00254             self.assertIsNone(context.exc)
00255             self.assertTrue(context.started)
00256         test()
00257         self.assertEqual(context.exc, (None, None, None))
00258 

Here is the call graph for this function:

Definition at line 259 of file test_contextlib.py.

00259 
00260     def test_decorator_with_exception(self):
00261         context = mycontext()
00262 
00263         @context
00264         def test():
00265             self.assertIsNone(context.exc)
00266             self.assertTrue(context.started)
00267             raise NameError('foo')
00268 
00269         with self.assertRaisesRegex(NameError, 'foo'):
00270             test()
00271         self.assertIsNotNone(context.exc)
00272         self.assertIs(context.exc[0], NameError)
00273 

Here is the call graph for this function:

Definition at line 304 of file test_contextlib.py.

00304 
00305     def test_typo_enter(self):
00306         class mycontext(ContextDecorator):
00307             def __unter__(self):
00308                 pass
00309             def __exit__(self, *exc):
00310                 pass
00311 
00312         with self.assertRaises(AttributeError):
00313             with mycontext():
00314                 pass
00315 

Definition at line 316 of file test_contextlib.py.

00316 
00317     def test_typo_exit(self):
00318         class mycontext(ContextDecorator):
00319             def __enter__(self):
00320                 pass
00321             def __uxit__(self, *exc):
00322                 pass
00323 
00324         with self.assertRaises(AttributeError):
00325             with mycontext():
00326                 pass
00327 

Here is the call graph for this function:


Member Data Documentation

Definition at line 281 of file test_contextlib.py.

Definition at line 282 of file test_contextlib.py.

Definition at line 283 of file test_contextlib.py.

Definition at line 338 of file test_contextlib.py.

Definition at line 334 of file test_contextlib.py.


The documentation for this class was generated from the following file: