Skip to content

Commit

Permalink
Add test coverage for the contextlib API (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra committed May 5, 2024
1 parent 07e353b commit 0f2fa02
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions betty/tests/test_contextlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from __future__ import annotations

from contextlib import suppress
from types import TracebackType

from betty.contextlib import SynchronizedContextManager


class DummyAsynchronousContextManager:
def __init__(self):
self.entered = False
self.exited = False
self.exc_type: type[BaseException] | None = None
self.exc_val: BaseException | None = None
self.exc_tb: TracebackType | None = None

async def __aenter__(self):
self.entered = True

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
self.exited = True
self.exc_type = exc_type
self.exc_val = exc_val
self.exc_tb = exc_tb


class TestSynchronizedContextManager:
async def test(self) -> None:
asynchronous_context_manager = DummyAsynchronousContextManager()
sut = SynchronizedContextManager(asynchronous_context_manager)
with sut:
pass
assert asynchronous_context_manager.entered
assert asynchronous_context_manager.exited

async def test_with_error(self) -> None:
asynchronous_context_manager = DummyAsynchronousContextManager()
sut = SynchronizedContextManager(asynchronous_context_manager)
error = RuntimeError()
with suppress(RuntimeError):
with sut:
raise error
assert asynchronous_context_manager.entered
assert asynchronous_context_manager.exited
assert asynchronous_context_manager.exc_type is RuntimeError
assert asynchronous_context_manager.exc_val is error
assert asynchronous_context_manager.exc_tb is error.__traceback__

0 comments on commit 0f2fa02

Please sign in to comment.