helpers.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2017-2020 Richard Hull and contributors
  3. # See LICENSE.rst for details.
  4. from pathlib import Path
  5. from unittest.mock import Mock
  6. import pytest
  7. from PIL import ImageChops
  8. import luma.core.error
  9. serial = Mock(unsafe=True)
  10. def setup_function(function):
  11. """
  12. Called after a test finished.
  13. """
  14. serial.reset_mock()
  15. serial.command.side_effect = None
  16. def assert_invalid_dimensions(deviceType, serial_interface, width, height):
  17. """
  18. Assert an invalid resolution raises a
  19. :py:class:`luma.core.error.DeviceDisplayModeError`.
  20. """
  21. with pytest.raises(luma.core.error.DeviceDisplayModeError) as ex:
  22. deviceType(serial_interface, width=width, height=height)
  23. assert f"Unsupported display mode: {width} x {height}" in str(ex.value)
  24. def get_reference_file(fname):
  25. """
  26. Get absolute path for ``fname``.
  27. :param fname: Filename.
  28. :type fname: str or pathlib.Path
  29. :rtype: str
  30. """
  31. return str(Path(__file__).resolve().parent.joinpath('reference', fname))
  32. def get_reference_image(fname):
  33. """
  34. :param fname: Filename.
  35. :type fname: str or pathlib.Path
  36. """
  37. return get_reference_file(Path('images').joinpath(fname))
  38. def assert_identical_image(reference, target):
  39. bbox = ImageChops.difference(reference, target).getbbox()
  40. assert bbox is None