Skip to content

Commit 4adff39

Browse files
committed
Improved test coverage
1 parent 6fdbf54 commit 4adff39

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

Tests/test_file_gbr.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

3+
from io import BytesIO
4+
35
import pytest
46

5-
from PIL import GbrImagePlugin, Image
7+
from PIL import GbrImagePlugin, Image, _binary
68

79
from .helper import assert_image_equal_tofile
810

@@ -31,8 +33,49 @@ def test_multiple_load_operations() -> None:
3133
assert_image_equal_tofile(im, "Tests/images/gbr.png")
3234

3335

36+
def create_gbr_image(info: dict[str, int] = {}, magic_number=b"") -> BytesIO:
37+
return BytesIO(
38+
b"".join(
39+
_binary.o32be(i)
40+
for i in [
41+
info.get("header_size", 20),
42+
info.get("version", 1),
43+
info.get("width", 1),
44+
info.get("height", 1),
45+
info.get("color_depth", 1),
46+
]
47+
)
48+
+ magic_number
49+
)
50+
51+
3452
def test_invalid_file() -> None:
35-
invalid_file = "Tests/images/flower.jpg"
53+
for f in [
54+
create_gbr_image({"header_size": 0}),
55+
create_gbr_image({"width": 0}),
56+
create_gbr_image({"height": 0}),
57+
]:
58+
with pytest.raises(SyntaxError, match="not a GIMP brush"):
59+
GbrImagePlugin.GbrImageFile(f)
3660

37-
with pytest.raises(SyntaxError):
61+
invalid_file = "Tests/images/flower.jpg"
62+
with pytest.raises(SyntaxError, match="Unsupported GIMP brush version"):
3863
GbrImagePlugin.GbrImageFile(invalid_file)
64+
65+
66+
def test_unsupported_gimp_brush() -> None:
67+
f = create_gbr_image({"color_depth": 2})
68+
with pytest.raises(SyntaxError, match="Unsupported GIMP brush color depth: 2"):
69+
GbrImagePlugin.GbrImageFile(f)
70+
71+
72+
def test_bad_magic_number() -> None:
73+
f = create_gbr_image({"version": 2}, magic_number=b"badm")
74+
with pytest.raises(SyntaxError, match="not a GIMP brush, bad magic number"):
75+
GbrImagePlugin.GbrImageFile(f)
76+
77+
78+
def test_L() -> None:
79+
f = create_gbr_image()
80+
with Image.open(f) as im:
81+
assert im.mode == "L"

0 commit comments

Comments
 (0)