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 Jul 22, 2020
1 parent e0b53f0 commit a72d414
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,20 @@ 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)
)


@skip_unless_feature("raqm")
class TestImageFont_RaqmLayout(TestImageFont):
Expand Down
24 changes: 24 additions & 0 deletions Tests/test_imagefontctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,27 @@ 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"
),
)

0 comments on commit a72d414

Please sign in to comment.