|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +from io import BytesIO |
| 4 | + |
3 | 5 | import pytest
|
4 | 6 |
|
5 |
| -from PIL import GbrImagePlugin, Image |
| 7 | +from PIL import GbrImagePlugin, Image, _binary |
6 | 8 |
|
7 | 9 | from .helper import assert_image_equal_tofile
|
8 | 10 |
|
@@ -31,8 +33,49 @@ def test_multiple_load_operations() -> None:
|
31 | 33 | assert_image_equal_tofile(im, "Tests/images/gbr.png")
|
32 | 34 |
|
33 | 35 |
|
| 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 | + |
34 | 52 | 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) |
36 | 60 |
|
37 |
| - with pytest.raises(SyntaxError): |
| 61 | + invalid_file = "Tests/images/flower.jpg" |
| 62 | + with pytest.raises(SyntaxError, match="Unsupported GIMP brush version"): |
38 | 63 | 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