add tests for invalid anchor

This commit is contained in:
nulano 2020-07-23 01:02:37 +02:00
parent e0b53f0014
commit f9624c3205
2 changed files with 47 additions and 0 deletions

View File

@ -864,6 +864,24 @@ class TestImageFont:
with Image.open(target) as expected: with Image.open(target) as expected:
assert_image_similar(im, expected, self.metrics["multiline-anchor"]) assert_image_similar(im, expected, self.metrics["multiline-anchor"])
def test_anchor_invalid(self):
font = self.get_font()
im = Image.new("RGB", (100, 100), "white")
d = ImageDraw.Draw(im)
d.font = font
for anchor in ["", "l", "a", "lax", "sa", "xa", "lx"]:
pytest.raises(ValueError, lambda: font.getmask2("hello", anchor=anchor))
pytest.raises(ValueError, lambda: font.getbbox("hello", anchor=anchor))
pytest.raises(ValueError, lambda: d.text((0, 0), "hello", anchor=anchor))
pytest.raises(
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
)
for anchor in ["lt", "lb"]:
pytest.raises(
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
)
@skip_unless_feature("raqm") @skip_unless_feature("raqm")
class TestImageFont_RaqmLayout(TestImageFont): class TestImageFont_RaqmLayout(TestImageFont):

View File

@ -380,3 +380,32 @@ def test_combine_multiline(anchor, align):
with Image.open(path) as expected: with Image.open(path) as expected:
assert_image_similar(im, expected, 0.015) assert_image_similar(im, expected, 0.015)
def test_anchor_invalid_ttb():
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
im = Image.new("RGB", (100, 100), "white")
d = ImageDraw.Draw(im)
d.font = font
for anchor in ["", "l", "a", "lax", "xa", "la", "ls", "ld", "lx"]:
pytest.raises(
ValueError, lambda: font.getmask2("hello", anchor=anchor, direction="ttb")
)
pytest.raises(
ValueError, lambda: font.getbbox("hello", anchor=anchor, direction="ttb")
)
pytest.raises(
ValueError, lambda: d.text((0, 0), "hello", anchor=anchor, direction="ttb")
)
pytest.raises(
ValueError,
lambda: d.multiline_text(
(0, 0), "foo\nbar", anchor=anchor, direction="ttb"
),
)
# ttb multiline text does not support anchors at all
pytest.raises(
ValueError,
lambda: d.multiline_text((0, 0), "foo\nbar", anchor="mm", direction="ttb"),
)