Skip to content

Commit

Permalink
Xfail tests when audio devices are missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
HexDecimal committed May 28, 2023
1 parent 4990592 commit 66a0bd9
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tests/test_sdl_audio.py
Expand Up @@ -13,11 +13,20 @@
# ruff: noqa: D103


needs_audio_device = pytest.mark.xfail(
not list(tcod.sdl.audio.get_devices()), reason="This test requires an audio device"
)
needs_audio_capture = pytest.mark.xfail(
not list(tcod.sdl.audio.get_capture_devices()), reason="This test requires an audio capture device"
)


def test_devices() -> None:
list(tcod.sdl.audio.get_devices())
list(tcod.sdl.audio.get_capture_devices())


@needs_audio_device
def test_audio_device() -> None:
with tcod.sdl.audio.open(frequency=44100, format=np.float32, channels=2, paused=True) as device:
assert not device.stopped
Expand Down Expand Up @@ -47,14 +56,17 @@ def test_audio_device() -> None:
mixer.stop()


@needs_audio_capture
def test_audio_capture() -> None:
with tcod.sdl.audio.open(capture=True) as device:
assert not device.stopped
assert isinstance(device.dequeue_audio(), np.ndarray)


@needs_audio_device
def test_audio_device_repr() -> None:
with tcod.sdl.audio.open(format=np.uint16, paused=True, callback=True) as device:
assert not device.stopped
assert "silence=" in repr(device)
assert "callback=" in repr(device)
assert "stopped=" in repr(device)
Expand Down Expand Up @@ -83,6 +95,7 @@ def test_convert_float64() -> None:
)


@needs_audio_device
def test_audio_callback() -> None:
class CheckCalled:
was_called: bool = False
Expand All @@ -95,13 +108,15 @@ def __call__(self, device: tcod.sdl.audio.AudioDevice, stream: NDArray[Any]) ->

check_called = CheckCalled()
with tcod.sdl.audio.open(callback=check_called, paused=False) as device:
assert not device.stopped
device.callback = device.callback
while not check_called.was_called:
time.sleep(0.001)


@pytest.mark.skipif(sys.version_info < (3, 8), reason="Needs sys.unraisablehook support")
@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning")
@needs_audio_device
def test_audio_callback_unraisable() -> None:
"""Test unraisable error in audio callback.
Expand All @@ -116,6 +131,7 @@ def __call__(self, device: tcod.sdl.audio.AudioDevice, stream: NDArray[Any]) ->
raise Exception("Test unraisable error") # noqa

check_called = CheckCalled()
with tcod.sdl.audio.open(callback=check_called, paused=False):
with tcod.sdl.audio.open(callback=check_called, paused=False) as device:
assert not device.stopped
while not check_called.was_called:
time.sleep(0.001)

0 comments on commit 66a0bd9

Please sign in to comment.