Skip to content

Commit 9e50a6a

Browse files
committed
add tests for invalid anchor
1 parent 2d0d528 commit 9e50a6a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

Tests/test_imagefont.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,24 @@ def test_anchor_multiline(self, anchor, align):
865865
with Image.open(target) as expected:
866866
assert_image_similar(im, expected, self.metrics["multiline-anchor"])
867867

868+
def test_anchor_invalid(self):
869+
font = self.get_font()
870+
im = Image.new("RGB", (100, 100), "white")
871+
d = ImageDraw.Draw(im)
872+
d.font = font
873+
874+
for anchor in ["", "l", "a", "lax", "sa", "xa", "lx"]:
875+
pytest.raises(ValueError, lambda: font.getmask2("hello", anchor=anchor))
876+
pytest.raises(ValueError, lambda: font.getbbox("hello", anchor=anchor))
877+
pytest.raises(ValueError, lambda: d.text((0, 0), "hello", anchor=anchor))
878+
pytest.raises(
879+
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
880+
)
881+
for anchor in ["lt", "lb"]:
882+
pytest.raises(
883+
ValueError, lambda: d.multiline_text((0, 0), "foo\nbar", anchor=anchor)
884+
)
885+
868886

869887
@skip_unless_feature("raqm")
870888
class TestImageFont_RaqmLayout(TestImageFont):

Tests/test_imagefontctl.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,32 @@ def test_combine_multiline(anchor, align):
381381

382382
with Image.open(path) as expected:
383383
assert_image_similar(im, expected, 0.015)
384+
385+
386+
def test_anchor_invalid_ttb():
387+
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
388+
im = Image.new("RGB", (100, 100), "white")
389+
d = ImageDraw.Draw(im)
390+
d.font = font
391+
392+
for anchor in ["", "l", "a", "lax", "xa", "la", "ls", "ld", "lx"]:
393+
pytest.raises(
394+
ValueError, lambda: font.getmask2("hello", anchor=anchor, direction="ttb")
395+
)
396+
pytest.raises(
397+
ValueError, lambda: font.getbbox("hello", anchor=anchor, direction="ttb")
398+
)
399+
pytest.raises(
400+
ValueError, lambda: d.text((0, 0), "hello", anchor=anchor, direction="ttb")
401+
)
402+
pytest.raises(
403+
ValueError,
404+
lambda: d.multiline_text(
405+
(0, 0), "foo\nbar", anchor=anchor, direction="ttb"
406+
),
407+
)
408+
# ttb multiline text does not support anchors at all
409+
pytest.raises(
410+
ValueError,
411+
lambda: d.multiline_text((0, 0), "foo\nbar", anchor="mm", direction="ttb"),
412+
)

0 commit comments

Comments
 (0)