Skip to content

Commit

Permalink
tests: use test font
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Oct 26, 2024
1 parent c5c2fda commit b7284fb
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 26 deletions.
8 changes: 5 additions & 3 deletions tests/baseline_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2020 Richard Hull and contributors
# Copyright (c) 2017-2024 Richard Hull and contributors
# See LICENSE.rst for details.

from .helpers import test_font


def primitives(device, draw):
padding = 2
Expand All @@ -20,5 +22,5 @@ def primitives(device, draw):
draw.line((x, bottom, x + shape_width, top), fill="yellow")
draw.line((x, top, x + shape_width, bottom), fill="yellow")
x += shape_width + padding
draw.text((x, top), 'Hello', fill="cyan")
draw.text((x, top + 20), 'World!', fill="purple")
draw.text((x, top), 'Hello', font=test_font, fill="cyan")
draw.text((x, top + 20), 'World!', font=test_font, fill="purple")
23 changes: 20 additions & 3 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2023 Richard Hull and contributors
# Copyright (c) 2017-2024 Richard Hull and contributors
# See LICENSE.rst for details.

import sys
import hashlib
from pathlib import Path
from io import StringIO
from contextlib import contextmanager
from PIL import ImageFont


def md5(fname):
with open(fname, 'rb') as fp:
return hashlib.md5(fp.read()).hexdigest()


def get_reference_image(fname):
def get_reference_file(fname):
"""
Get absolute path for ``fname``.
Expand All @@ -25,6 +26,22 @@ def get_reference_image(fname):
return str(Path(__file__).resolve().parent.joinpath('reference', fname))


def get_reference_pillow_font(fname):
"""
Load :py:class:`PIL.ImageFont` type font from provided fname
:param fname: The name of the file that contains the PIL.ImageFont
:type fname: str
:rtype: :py:class:`PIL.ImageFont`
"""
path = get_reference_file(Path('font').joinpath(fname))
return ImageFont.load(path)


# font used in (most) tests
test_font = get_reference_pillow_font('courB08.pil')


def assert_identical(rname, fname):
"""
Files that are compared have the same MD5 hash.
Expand All @@ -34,7 +51,7 @@ def assert_identical(rname, fname):
:param fname: Target file location.
:type fname: str
"""
reference = get_reference_image(rname)
reference = get_reference_file(rname)
md5_ref = md5(reference)
md5_target = md5(fname)
assert md5_ref == md5_target, \
Expand Down
Binary file modified tests/reference/anim.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/reference/font/courB08.pbm
Binary file not shown.
Binary file added tests/reference/font/courB08.pil
Binary file not shown.
8 changes: 3 additions & 5 deletions tests/test_asciiblock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2022 Richard Hull and contributors
# Copyright (c) 2018-2024 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Expand All @@ -10,14 +10,12 @@
import sys
import struct
import hashlib
from pathlib import Path
from unittest.mock import patch

from luma.core.render import canvas

from .baseline_data import primitives
from .helpers import md5, redirect_stdout

from .helpers import md5, redirect_stdout, get_reference_file

def noop():
pass
Expand Down Expand Up @@ -48,7 +46,7 @@ def test_display():
out = f.getvalue().encode('utf-8')

digest = hashlib.md5(out).hexdigest()
fname = Path(__file__).resolve().parent.joinpath('reference', 'asciiblock.txt')
fname = get_reference_file('asciiblock.txt')
assert md5(str(fname)) == digest


Expand Down
8 changes: 4 additions & 4 deletions tests/test_gifanim.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2022 Richard Hull and contributors
# Copyright (c) 2017-2024 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Expand All @@ -18,7 +18,7 @@
import pytest

from .baseline_data import primitives
from .helpers import get_reference_image, assert_identical
from .helpers import get_reference_file, test_font, assert_identical


def test_gifanim_write():
Expand All @@ -30,7 +30,7 @@ def test_gifanim_write():
primitives(device, draw)

with canvas(device) as draw:
draw.text((30, 10), text="Blipvert", fill="white")
draw.text((30, 10), text="Blipvert", font=test_font, fill="white")

with canvas(device) as draw:
primitives(device, draw)
Expand All @@ -48,7 +48,7 @@ def test_gifanim_noimages():


def test_gifanim_max_frames():
reference = get_reference_image('anim.gif')
reference = get_reference_file('anim.gif')
img = Image.open(reference)
with NamedTemporaryFile(suffix='.gif') as temp:
fname = temp.name
Expand Down
22 changes: 11 additions & 11 deletions tests/test_transformer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2022 Richard Hull and contributors
# Copyright (c) 2017-2024 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Expand All @@ -14,11 +14,11 @@
from luma.core.render import canvas
from luma.emulator.render import transformer

from .helpers import get_reference_image
from .helpers import get_reference_file, test_font


def baseline_im():
return pygame.image.load(get_reference_image("capture.png"))
return pygame.image.load(get_reference_file("capture.png"))


def to_pillow_img(surface):
Expand All @@ -31,7 +31,7 @@ def to_pygame_surface(im):


def test_none():
with open(get_reference_image("capture.png"), "rb") as fp:
with open(get_reference_file("capture.png"), "rb") as fp:
ref = Image.open(fp)
surface = baseline_im()
tf = transformer(pygame, 128, 64, 1)
Expand All @@ -41,7 +41,7 @@ def test_none():


def test_scale2x():
with open(get_reference_image("scale2x.png"), "rb") as fp:
with open(get_reference_file("scale2x.png"), "rb") as fp:
ref = Image.open(fp)
surface = baseline_im()
tf = transformer(pygame, 128, 64, 2)
Expand All @@ -51,7 +51,7 @@ def test_scale2x():


def test_smoothscale():
with open(get_reference_image("smoothscale.png"), "rb") as fp:
with open(get_reference_file("smoothscale.png"), "rb") as fp:
ref = Image.open(fp)
surface = baseline_im()
tf = transformer(pygame, 128, 64, 2)
Expand All @@ -61,7 +61,7 @@ def test_smoothscale():


def test_identity():
with open(get_reference_image("identity.png"), "rb") as fp:
with open(get_reference_file("identity.png"), "rb") as fp:
ref = Image.open(fp)
surface = baseline_im()
tf = transformer(pygame, 128, 64, 2)
Expand All @@ -71,13 +71,13 @@ def test_identity():


def test_led_matrix():
with open(get_reference_image("led_matrix.png"), "rb") as fp:
with open(get_reference_file("led_matrix.png"), "rb") as fp:
ref = Image.open(fp)
device = dummy(width=40, height=24)
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white")
draw.text((5, 2), "Hello", fill="white")
draw.text((5, 10), "World", fill="white")
draw.text((5, 2), "Hello", font=test_font, fill="white")
draw.text((5, 10), "World", font=test_font, fill="white")
surface = to_pygame_surface(device.image)
tf = transformer(pygame, device.width, device.height, 16)
im = to_pillow_img(tf.led_matrix(surface))
Expand All @@ -86,7 +86,7 @@ def test_led_matrix():


def test_seven_segment():
with open(get_reference_image("seven_segment.png"), "rb") as fp:
with open(get_reference_file("seven_segment.png"), "rb") as fp:
ref = Image.open(fp)
chars = [
# Alphabet with omissions
Expand Down

0 comments on commit b7284fb

Please sign in to comment.