Skip to content

Commit

Permalink
apply _test_case decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Dec 25, 2024
1 parent 2c8c174 commit 61f0545
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions Lib/test/test_contextlib_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,7 @@ def wrapper(*args, **kwargs):
return wrapper


class TestAbstractAsyncContextManager(unittest.IsolatedAsyncioTestCase):

def test_async_test_self_test(self):
class _async_yield:
def __init__(self, v):
self.v = v

def __await__(self):
return (yield self.v)

@_async_test
async def do_not_stop_coro():
while True:
await _async_yield(None)

with self.assertRaisesRegex(AssertionError, "coroutine did not stop"):
do_not_stop_coro()
class TestAbstractAsyncContextManager(unittest.TestCase):

@_async_test
async def test_enter(self):
Expand All @@ -60,6 +44,7 @@ async def __aexit__(self, *args):
async with manager as context:
self.assertIs(manager, context)

@_async_test
async def test_slots(self):
class DefaultAsyncContextManager(AbstractAsyncContextManager):
__slots__ = ()
Expand All @@ -71,6 +56,7 @@ async def __aexit__(self, *args):
manager = DefaultAsyncContextManager()
manager.var = 42

@_async_test
async def test_async_gen_propagates_generator_exit(self):
# A regression test for https://bugs.python.org/issue33786.

Expand Down Expand Up @@ -121,8 +107,9 @@ class NoneAexit(ManagerFromScratch):
self.assertFalse(issubclass(NoneAexit, AbstractAsyncContextManager))


class AsyncContextManagerTestCase(unittest.IsolatedAsyncioTestCase):
class AsyncContextManagerTestCase(unittest.TestCase):

@_async_test
async def test_contextmanager_plain(self):
state = []
@asynccontextmanager
Expand All @@ -136,6 +123,7 @@ async def woohoo():
state.append(x)
self.assertEqual(state, [1, 42, 999])

@_async_test
async def test_contextmanager_finally(self):
state = []
@asynccontextmanager
Expand All @@ -153,6 +141,7 @@ async def woohoo():
raise ZeroDivisionError()
self.assertEqual(state, [1, 42, 999])

@_async_test
async def test_contextmanager_traceback(self):
@asynccontextmanager
async def f():
Expand Down Expand Up @@ -208,6 +197,7 @@ class StopAsyncIterationSubclass(StopAsyncIteration):
self.assertEqual(frames[0].name, 'test_contextmanager_traceback')
self.assertEqual(frames[0].line, 'raise stop_exc')

@_async_test
async def test_contextmanager_no_reraise(self):
@asynccontextmanager
async def whee():
Expand All @@ -217,6 +207,7 @@ async def whee():
# Calling __aexit__ should not result in an exception
self.assertFalse(await ctx.__aexit__(TypeError, TypeError("foo"), None))

@_async_test
async def test_contextmanager_trap_yield_after_throw(self):
@asynccontextmanager
async def whoo():
Expand All @@ -232,6 +223,7 @@ async def whoo():
# The "gen" attribute is an implementation detail.
self.assertFalse(ctx.gen.ag_suspended)

@_async_test
async def test_contextmanager_trap_no_yield(self):
@asynccontextmanager
async def whoo():
Expand All @@ -241,6 +233,7 @@ async def whoo():
with self.assertRaises(RuntimeError):
await ctx.__aenter__()

@_async_test
async def test_contextmanager_trap_second_yield(self):
@asynccontextmanager
async def whoo():
Expand All @@ -254,6 +247,7 @@ async def whoo():
# The "gen" attribute is an implementation detail.
self.assertFalse(ctx.gen.ag_suspended)

@_async_test
async def test_contextmanager_non_normalised(self):
@asynccontextmanager
async def whoo():
Expand All @@ -267,6 +261,7 @@ async def whoo():
with self.assertRaises(SyntaxError):
await ctx.__aexit__(RuntimeError, None, None)

@_async_test
async def test_contextmanager_except(self):
state = []
@asynccontextmanager
Expand All @@ -284,6 +279,7 @@ async def woohoo():
raise ZeroDivisionError(999)
self.assertEqual(state, [1, 42, 999])

@_async_test
async def test_contextmanager_except_stopiter(self):
@asynccontextmanager
async def woohoo():
Expand All @@ -310,6 +306,7 @@ class StopAsyncIterationSubclass(StopAsyncIteration):
else:
self.fail(f'{stop_exc} was suppressed')

@_async_test
async def test_contextmanager_wrap_runtimeerror(self):
@asynccontextmanager
async def woohoo():
Expand Down Expand Up @@ -354,12 +351,14 @@ def test_contextmanager_doc_attrib(self):
self.assertEqual(baz.__doc__, "Whee!")

@support.requires_docstrings
@_async_test
async def test_instance_docstring_given_cm_docstring(self):
baz = self._create_contextmanager_attribs()(None)
self.assertEqual(baz.__doc__, "Whee!")
async with baz:
pass # suppress warning

@_async_test
async def test_keywords(self):
# Ensure no keyword arguments are inhibited
@asynccontextmanager
Expand All @@ -368,6 +367,7 @@ async def woohoo(self, func, args, kwds):
async with woohoo(self=11, func=22, args=33, kwds=44) as target:
self.assertEqual(target, (11, 22, 33, 44))

@_async_test
async def test_recursive(self):
depth = 0
ncols = 0
Expand All @@ -394,6 +394,7 @@ async def recursive():
self.assertEqual(ncols, 10)
self.assertEqual(depth, 0)

@_async_test
async def test_decorator(self):
entered = False

Expand All @@ -412,6 +413,7 @@ async def test():
await test()
self.assertFalse(entered)

@_async_test
async def test_decorator_with_exception(self):
entered = False

Expand All @@ -434,6 +436,7 @@ async def test():
await test()
self.assertFalse(entered)

@_async_test
async def test_decorating_method(self):

@asynccontextmanager
Expand Down Expand Up @@ -468,14 +471,15 @@ async def method(self, a, b, c=None):
self.assertEqual(test.b, 2)


class AclosingTestCase(unittest.IsolatedAsyncioTestCase):
class AclosingTestCase(unittest.TestCase):

@support.requires_docstrings
def test_instance_docs(self):
cm_docstring = aclosing.__doc__
obj = aclosing(None)
self.assertEqual(obj.__doc__, cm_docstring)

@_async_test
async def test_aclosing(self):
state = []
class C:
Expand All @@ -487,6 +491,7 @@ async def aclose(self):
self.assertEqual(x, y)
self.assertEqual(state, [1])

@_async_test
async def test_aclosing_error(self):
state = []
class C:
Expand All @@ -500,6 +505,7 @@ async def aclose(self):
1 / 0
self.assertEqual(state, [1])

@_async_test
async def test_aclosing_bpo41229(self):
state = []

Expand All @@ -525,7 +531,7 @@ async def agenfunc():
self.assertEqual(state, [1])


class TestAsyncExitStack(TestBaseExitStack, unittest.IsolatedAsyncioTestCase):
class TestAsyncExitStack(TestBaseExitStack, unittest.TestCase):
class SyncAsyncExitStack(AsyncExitStack):

def close(self):
Expand Down Expand Up @@ -589,6 +595,7 @@ async def _exit(*args, **kwds):
stack.push_async_callback(callback=_exit, arg=3)
self.assertEqual(result, [])

@_async_test
async def test_async_push(self):
exc_raised = ZeroDivisionError
async def _expect_exc(exc_type, exc, exc_tb):
Expand Down Expand Up @@ -624,6 +631,7 @@ async def __aexit__(self, *exc_details):
self.assertIs(stack._exit_callbacks[-1][1], _expect_exc)
1/0

@_async_test
async def test_enter_async_context(self):
class TestCM(object):
async def __aenter__(self):
Expand All @@ -645,6 +653,7 @@ async def _exit():

self.assertEqual(result, [1, 2, 3, 4])

@_async_test
async def test_enter_async_context_errors(self):
class LacksEnterAndExit:
pass
Expand All @@ -664,6 +673,7 @@ async def __aenter__(self):
await stack.enter_async_context(LacksExit())
self.assertFalse(stack._exit_callbacks)

@_async_test
async def test_async_exit_exception_chaining(self):
# Ensure exception chaining matches the reference behaviour
async def raise_exc(exc):
Expand Down Expand Up @@ -695,6 +705,7 @@ async def suppress_exc(*exc_details):
self.assertIsInstance(inner_exc, ValueError)
self.assertIsInstance(inner_exc.__context__, ZeroDivisionError)

@_async_test
async def test_async_exit_exception_explicit_none_context(self):
# Ensure AsyncExitStack chaining matches actual nested `with` statements
# regarding explicit __context__ = None.
Expand Down Expand Up @@ -729,6 +740,7 @@ async def my_cm_with_exit_stack():
else:
self.fail("Expected IndexError, but no exception was raised")

@_async_test
async def test_instance_bypass_async(self):
class Example(object): pass
cm = Example()
Expand All @@ -741,7 +753,8 @@ class Example(object): pass
self.assertIs(stack._exit_callbacks[-1][1], cm)


class TestAsyncNullcontext(unittest.IsolatedAsyncioTestCase):
class TestAsyncNullcontext(unittest.TestCase):
@_async_test
async def test_async_nullcontext(self):
class C:
pass
Expand Down

0 comments on commit 61f0545

Please sign in to comment.