Skip to content

Commit c42eb70

Browse files
committed
Parametrize test
1 parent 10ccbd7 commit c42eb70

File tree

3 files changed

+60
-62
lines changed

3 files changed

+60
-62
lines changed

Tests/test_file_bmp.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,19 @@
1515
)
1616

1717

18-
def test_sanity(tmp_path: Path) -> None:
19-
def roundtrip(im: Image.Image) -> None:
20-
outfile = tmp_path / "temp.bmp"
21-
22-
im.save(outfile, "BMP")
23-
24-
with Image.open(outfile) as reloaded:
25-
reloaded.load()
26-
assert im.mode == reloaded.mode
27-
assert im.size == reloaded.size
28-
assert reloaded.format == "BMP"
29-
assert reloaded.get_format_mimetype() == "image/bmp"
18+
@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
19+
def test_sanity(mode: str, tmp_path: Path) -> None:
20+
outfile = tmp_path / "temp.bmp"
3021

31-
roundtrip(hopper())
22+
im = hopper(mode)
23+
im.save(outfile, "BMP")
3224

33-
roundtrip(hopper("1"))
34-
roundtrip(hopper("L"))
35-
roundtrip(hopper("P"))
36-
roundtrip(hopper("RGB"))
25+
with Image.open(outfile) as reloaded:
26+
reloaded.load()
27+
assert im.mode == reloaded.mode
28+
assert im.size == reloaded.size
29+
assert reloaded.format == "BMP"
30+
assert reloaded.get_format_mimetype() == "image/bmp"
3731

3832

3933
def test_invalid_file() -> None:

Tests/test_file_sgi.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,26 @@ def test_invalid_file() -> None:
7171
SgiImagePlugin.SgiImageFile(invalid_file)
7272

7373

74-
def test_write(tmp_path: Path) -> None:
75-
def roundtrip(img: Image.Image) -> None:
76-
out = tmp_path / "temp.sgi"
77-
img.save(out, format="sgi")
74+
def roundtrip(img: Image.Image, tmp_path: Path) -> None:
75+
out = tmp_path / "temp.sgi"
76+
img.save(out, format="sgi")
77+
assert_image_equal_tofile(img, out)
78+
79+
out = tmp_path / "fp.sgi"
80+
with open(out, "wb") as fp:
81+
img.save(fp)
7882
assert_image_equal_tofile(img, out)
7983

80-
out = tmp_path / "fp.sgi"
81-
with open(out, "wb") as fp:
82-
img.save(fp)
83-
assert_image_equal_tofile(img, out)
84+
assert not fp.closed
85+
8486

85-
assert not fp.closed
87+
@pytest.mark.parametrize("mode", ("L", "RGB", "RGBA"))
88+
def test_write(mode: str, tmp_path: Path) -> None:
89+
roundtrip(hopper(mode), tmp_path)
8690

87-
for mode in ("L", "RGB", "RGBA"):
88-
roundtrip(hopper(mode))
8991

90-
# Test 1 dimension for an L mode image
91-
roundtrip(Image.new("L", (10, 1)))
92+
def test_write_L_mode_1_dimension(tmp_path: Path) -> None:
93+
roundtrip(Image.new("L", (10, 1)), tmp_path)
9294

9395

9496
def test_write16(tmp_path: Path) -> None:

Tests/test_file_tga.py

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
from glob import glob
5-
from itertools import product
64
from pathlib import Path
75

86
import pytest
@@ -15,14 +13,25 @@
1513
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
1614

1715

18-
_MODES = ("L", "LA", "P", "RGB", "RGBA")
1916
_ORIGINS = ("tl", "bl")
2017

2118
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
2219

2320

24-
@pytest.mark.parametrize("mode", _MODES)
25-
def test_sanity(mode: str, tmp_path: Path) -> None:
21+
@pytest.mark.parametrize(
22+
"size_mode",
23+
(
24+
("1x1", "L"),
25+
("200x32", "L"),
26+
("200x32", "LA"),
27+
("200x32", "P"),
28+
("200x32", "RGB"),
29+
("200x32", "RGBA"),
30+
),
31+
)
32+
@pytest.mark.parametrize("origin", _ORIGINS)
33+
@pytest.mark.parametrize("rle", (True, False))
34+
def test_sanity(size_mode: str, origin: str, rle: str, tmp_path: Path) -> None:
2635
def roundtrip(original_im: Image.Image) -> None:
2736
out = tmp_path / "temp.tga"
2837

@@ -36,33 +45,26 @@ def roundtrip(original_im: Image.Image) -> None:
3645

3746
assert_image_equal(saved_im, original_im)
3847

39-
png_paths = glob(os.path.join(_TGA_DIR_COMMON, f"*x*_{mode.lower()}.png"))
40-
41-
for png_path in png_paths:
42-
with Image.open(png_path) as reference_im:
43-
assert reference_im.mode == mode
44-
45-
path_no_ext = os.path.splitext(png_path)[0]
46-
for origin, rle in product(_ORIGINS, (True, False)):
47-
tga_path = "{}_{}_{}.tga".format(
48-
path_no_ext, origin, "rle" if rle else "raw"
49-
)
50-
51-
with Image.open(tga_path) as original_im:
52-
assert original_im.format == "TGA"
53-
assert original_im.get_format_mimetype() == "image/x-tga"
54-
if rle:
55-
assert original_im.info["compression"] == "tga_rle"
56-
assert (
57-
original_im.info["orientation"]
58-
== _ORIGIN_TO_ORIENTATION[origin]
59-
)
60-
if mode == "P":
61-
assert original_im.getpalette() == reference_im.getpalette()
62-
63-
assert_image_equal(original_im, reference_im)
64-
65-
roundtrip(original_im)
48+
size, mode = size_mode
49+
png_path = os.path.join(_TGA_DIR_COMMON, size + "_" + mode.lower() + ".png")
50+
with Image.open(png_path) as reference_im:
51+
assert reference_im.mode == mode
52+
53+
path_no_ext = os.path.splitext(png_path)[0]
54+
tga_path = "{}_{}_{}.tga".format(path_no_ext, origin, "rle" if rle else "raw")
55+
56+
with Image.open(tga_path) as original_im:
57+
assert original_im.format == "TGA"
58+
assert original_im.get_format_mimetype() == "image/x-tga"
59+
if rle:
60+
assert original_im.info["compression"] == "tga_rle"
61+
assert original_im.info["orientation"] == _ORIGIN_TO_ORIENTATION[origin]
62+
if mode == "P":
63+
assert original_im.getpalette() == reference_im.getpalette()
64+
65+
assert_image_equal(original_im, reference_im)
66+
67+
roundtrip(original_im)
6668

6769

6870
def test_palette_depth_8() -> None:

0 commit comments

Comments
 (0)