Use TypedDict

This commit is contained in:
Andrew Murray 2024-01-23 09:55:25 +11:00
parent 2521ec4732
commit 16fd934b00

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import os import os
from pathlib import PosixPath from pathlib import PosixPath
from typing import TypedDict
import pytest import pytest
@ -15,7 +16,14 @@ from .helper import (
fontname = "Tests/fonts/ter-x20b.pcf" fontname = "Tests/fonts/ter-x20b.pcf"
charsets: dict[str, dict[str, int | str]] = {
class Charset(TypedDict):
glyph_count: int
message: str
image1: str
charsets: dict[str, Charset] = {
"iso8859-1": { "iso8859-1": {
"glyph_count": 223, "glyph_count": 223,
"message": "hello, world", "message": "hello, world",
@ -81,14 +89,9 @@ def test_draw(
font = ImageFont.load(tempname) font = ImageFont.load(tempname)
im = Image.new("L", (150, 30), "white") im = Image.new("L", (150, 30), "white")
draw = ImageDraw.Draw(im) draw = ImageDraw.Draw(im)
message = charsets[encoding]["message"].encode(encoding)
message = charsets[encoding]["message"] draw.text((0, 0), message, "black", font=font)
assert isinstance(message, str) assert_image_similar_tofile(im, charsets[encoding]["image1"], 0)
draw.text((0, 0), message.encode(encoding), "black", font=font)
expected_path = charsets[encoding]["image1"]
assert isinstance(expected_path, str)
assert_image_similar_tofile(im, expected_path, 0)
@pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250")) @pytest.mark.parametrize("encoding", ("iso8859-1", "iso8859-2", "cp1250"))
@ -104,10 +107,8 @@ def test_textsize(
assert dy == 20 assert dy == 20
assert dx in (0, 10) assert dx in (0, 10)
assert font.getlength(bytearray([i])) == dx assert font.getlength(bytearray([i])) == dx
message = charsets[encoding]["message"] message = charsets[encoding]["message"].encode(encoding)
assert isinstance(message, str) for i in range(len(message)):
message_bytes = message.encode(encoding) msg = message[: i + 1]
for i in range(len(message_bytes)):
msg = message_bytes[: i + 1]
assert font.getlength(msg) == len(msg) * 10 assert font.getlength(msg) == len(msg) * 10
assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20) assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20)