Pillow/Tests/test_imageshow.py
Hugo van Kemenade 4a4b90c365
Autotype tests (#7756)
* autotyping: --none-return
* autotyping: --scalar-return
* autotyping: --int-param
* autotyping: --float-param
* autotyping: --str-param
* autotyping: --annotate-named-param tmp_path:pathlib.Path
2024-01-31 20:12:58 +11:00

94 lines
2.1 KiB
Python

from __future__ import annotations
import pytest
from PIL import Image, ImageShow
from .helper import hopper, is_win32, on_ci
def test_sanity() -> None:
dir(Image)
dir(ImageShow)
def test_register() -> None:
# Test registering a viewer that is not a class
ImageShow.register("not a class")
# Restore original state
ImageShow._viewers.pop()
@pytest.mark.parametrize(
"order",
[-1, 0],
)
def test_viewer_show(order) -> None:
class TestViewer(ImageShow.Viewer):
def show_image(self, image, **options) -> bool:
self.methodCalled = True
return True
viewer = TestViewer()
ImageShow.register(viewer, order)
for mode in ("1", "I;16", "LA", "RGB", "RGBA"):
viewer.methodCalled = False
with hopper(mode) as im:
assert ImageShow.show(im)
assert viewer.methodCalled
# Restore original state
ImageShow._viewers.pop(0)
@pytest.mark.skipif(
not on_ci() or is_win32(),
reason="Only run on CIs; hangs on Windows CIs",
)
@pytest.mark.parametrize("mode", ("1", "I;16", "LA", "RGB", "RGBA"))
def test_show(mode) -> None:
im = hopper(mode)
assert ImageShow.show(im)
def test_show_without_viewers() -> None:
viewers = ImageShow._viewers
ImageShow._viewers = []
with hopper() as im:
assert not ImageShow.show(im)
ImageShow._viewers = viewers
def test_viewer() -> None:
viewer = ImageShow.Viewer()
assert viewer.get_format(None) is None
with pytest.raises(NotImplementedError):
viewer.get_command(None)
@pytest.mark.parametrize("viewer", ImageShow._viewers)
def test_viewers(viewer) -> None:
try:
viewer.get_command("test.jpg")
except NotImplementedError:
pass
def test_ipythonviewer() -> None:
pytest.importorskip("IPython", reason="IPython not installed")
for viewer in ImageShow._viewers:
if isinstance(viewer, ImageShow.IPythonViewer):
test_viewer = viewer
break
else:
pytest.fail()
im = hopper()
assert test_viewer.show(im) == 1