mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-25 23:43:16 +03:00
Parametrize tests (#8838)
Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
This commit is contained in:
parent
10ccbd7788
commit
1cb6c7c347
|
@ -15,10 +15,11 @@ from .helper import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_sanity(tmp_path: Path) -> None:
|
@pytest.mark.parametrize("mode", ("1", "L", "P", "RGB"))
|
||||||
def roundtrip(im: Image.Image) -> None:
|
def test_sanity(mode: str, tmp_path: Path) -> None:
|
||||||
outfile = tmp_path / "temp.bmp"
|
outfile = tmp_path / "temp.bmp"
|
||||||
|
|
||||||
|
im = hopper(mode)
|
||||||
im.save(outfile, "BMP")
|
im.save(outfile, "BMP")
|
||||||
|
|
||||||
with Image.open(outfile) as reloaded:
|
with Image.open(outfile) as reloaded:
|
||||||
|
@ -28,13 +29,6 @@ def test_sanity(tmp_path: Path) -> None:
|
||||||
assert reloaded.format == "BMP"
|
assert reloaded.format == "BMP"
|
||||||
assert reloaded.get_format_mimetype() == "image/bmp"
|
assert reloaded.get_format_mimetype() == "image/bmp"
|
||||||
|
|
||||||
roundtrip(hopper())
|
|
||||||
|
|
||||||
roundtrip(hopper("1"))
|
|
||||||
roundtrip(hopper("L"))
|
|
||||||
roundtrip(hopper("P"))
|
|
||||||
roundtrip(hopper("RGB"))
|
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_file() -> None:
|
def test_invalid_file() -> None:
|
||||||
with open("Tests/images/flower.jpg", "rb") as fp:
|
with open("Tests/images/flower.jpg", "rb") as fp:
|
||||||
|
|
|
@ -71,8 +71,7 @@ def test_invalid_file() -> None:
|
||||||
SgiImagePlugin.SgiImageFile(invalid_file)
|
SgiImagePlugin.SgiImageFile(invalid_file)
|
||||||
|
|
||||||
|
|
||||||
def test_write(tmp_path: Path) -> None:
|
def roundtrip(img: Image.Image, tmp_path: Path) -> None:
|
||||||
def roundtrip(img: Image.Image) -> None:
|
|
||||||
out = tmp_path / "temp.sgi"
|
out = tmp_path / "temp.sgi"
|
||||||
img.save(out, format="sgi")
|
img.save(out, format="sgi")
|
||||||
assert_image_equal_tofile(img, out)
|
assert_image_equal_tofile(img, out)
|
||||||
|
@ -84,11 +83,14 @@ def test_write(tmp_path: Path) -> None:
|
||||||
|
|
||||||
assert not fp.closed
|
assert not fp.closed
|
||||||
|
|
||||||
for mode in ("L", "RGB", "RGBA"):
|
|
||||||
roundtrip(hopper(mode))
|
|
||||||
|
|
||||||
# Test 1 dimension for an L mode image
|
@pytest.mark.parametrize("mode", ("L", "RGB", "RGBA"))
|
||||||
roundtrip(Image.new("L", (10, 1)))
|
def test_write(mode: str, tmp_path: Path) -> None:
|
||||||
|
roundtrip(hopper(mode), tmp_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_write_L_mode_1_dimension(tmp_path: Path) -> None:
|
||||||
|
roundtrip(Image.new("L", (10, 1)), tmp_path)
|
||||||
|
|
||||||
|
|
||||||
def test_write16(tmp_path: Path) -> None:
|
def test_write16(tmp_path: Path) -> None:
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from glob import glob
|
|
||||||
from itertools import product
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -15,14 +13,27 @@ _TGA_DIR = os.path.join("Tests", "images", "tga")
|
||||||
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
|
_TGA_DIR_COMMON = os.path.join(_TGA_DIR, "common")
|
||||||
|
|
||||||
|
|
||||||
_MODES = ("L", "LA", "P", "RGB", "RGBA")
|
|
||||||
_ORIGINS = ("tl", "bl")
|
_ORIGINS = ("tl", "bl")
|
||||||
|
|
||||||
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
|
_ORIGIN_TO_ORIENTATION = {"tl": 1, "bl": -1}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("mode", _MODES)
|
@pytest.mark.parametrize(
|
||||||
def test_sanity(mode: str, tmp_path: Path) -> None:
|
"size_mode",
|
||||||
|
(
|
||||||
|
("1x1", "L"),
|
||||||
|
("200x32", "L"),
|
||||||
|
("200x32", "LA"),
|
||||||
|
("200x32", "P"),
|
||||||
|
("200x32", "RGB"),
|
||||||
|
("200x32", "RGBA"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
@pytest.mark.parametrize("origin", _ORIGINS)
|
||||||
|
@pytest.mark.parametrize("rle", (True, False))
|
||||||
|
def test_sanity(
|
||||||
|
size_mode: tuple[str, str], origin: str, rle: str, tmp_path: Path
|
||||||
|
) -> None:
|
||||||
def roundtrip(original_im: Image.Image) -> None:
|
def roundtrip(original_im: Image.Image) -> None:
|
||||||
out = tmp_path / "temp.tga"
|
out = tmp_path / "temp.tga"
|
||||||
|
|
||||||
|
@ -36,27 +47,20 @@ def test_sanity(mode: str, tmp_path: Path) -> None:
|
||||||
|
|
||||||
assert_image_equal(saved_im, original_im)
|
assert_image_equal(saved_im, original_im)
|
||||||
|
|
||||||
png_paths = glob(os.path.join(_TGA_DIR_COMMON, f"*x*_{mode.lower()}.png"))
|
size, mode = size_mode
|
||||||
|
png_path = os.path.join(_TGA_DIR_COMMON, size + "_" + mode.lower() + ".png")
|
||||||
for png_path in png_paths:
|
|
||||||
with Image.open(png_path) as reference_im:
|
with Image.open(png_path) as reference_im:
|
||||||
assert reference_im.mode == mode
|
assert reference_im.mode == mode
|
||||||
|
|
||||||
path_no_ext = os.path.splitext(png_path)[0]
|
path_no_ext = os.path.splitext(png_path)[0]
|
||||||
for origin, rle in product(_ORIGINS, (True, False)):
|
tga_path = "{}_{}_{}.tga".format(path_no_ext, origin, "rle" if rle else "raw")
|
||||||
tga_path = "{}_{}_{}.tga".format(
|
|
||||||
path_no_ext, origin, "rle" if rle else "raw"
|
|
||||||
)
|
|
||||||
|
|
||||||
with Image.open(tga_path) as original_im:
|
with Image.open(tga_path) as original_im:
|
||||||
assert original_im.format == "TGA"
|
assert original_im.format == "TGA"
|
||||||
assert original_im.get_format_mimetype() == "image/x-tga"
|
assert original_im.get_format_mimetype() == "image/x-tga"
|
||||||
if rle:
|
if rle:
|
||||||
assert original_im.info["compression"] == "tga_rle"
|
assert original_im.info["compression"] == "tga_rle"
|
||||||
assert (
|
assert original_im.info["orientation"] == _ORIGIN_TO_ORIENTATION[origin]
|
||||||
original_im.info["orientation"]
|
|
||||||
== _ORIGIN_TO_ORIENTATION[origin]
|
|
||||||
)
|
|
||||||
if mode == "P":
|
if mode == "P":
|
||||||
assert original_im.getpalette() == reference_im.getpalette()
|
assert original_im.getpalette() == reference_im.getpalette()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user