Skip to content

Commit

Permalink
add tests for invalid anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
nulano committed Sep 1, 2020
1 parent 2d0d528 commit 9e50a6a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,24 @@ def test_anchor_multiline(self, anchor, align):
with Image.open(target) as expected:
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")
class TestImageFont_RaqmLayout(TestImageFont):
Expand Down
29 changes: 29 additions & 0 deletions Tests/test_imagefontctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,32 @@ def test_combine_multiline(anchor, align):

with Image.open(path) as expected:
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"),
)

0 comments on commit 9e50a6a

Please sign in to comment.