From f5d866fce0e10251e1164688c5d593fb6c58edb7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 28 Jul 2025 22:10:00 +1000 Subject: [PATCH] Use assert_image_equal and assert_image_equal_tofile --- Tests/test_file_ani.py | 35 ++++++++++++----------------------- Tests/test_file_cur.py | 20 +++++++++++--------- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/Tests/test_file_ani.py b/Tests/test_file_ani.py index bcb9791dc..25e8384cc 100644 --- a/Tests/test_file_ani.py +++ b/Tests/test_file_ani.py @@ -6,18 +6,18 @@ import pytest from PIL import Image +from .helper import assert_image_equal_tofile + def test_aero_busy() -> None: with Image.open("Tests/images/ani/aero_busy.ani") as im: assert im.size == (64, 64) assert im.n_frames == 18 - with Image.open("Tests/images/ani/aero_busy_0.png") as png: - assert png.tobytes() == im.tobytes() + assert_image_equal_tofile(im, "Tests/images/ani/aero_busy_0.png") im.seek(8) - with Image.open("Tests/images/ani/aero_busy_8.png") as png: - assert png.tobytes() == im.tobytes() + assert_image_equal_tofile(im, "Tests/images/ani/aero_busy_8.png") with pytest.raises(EOFError): im.seek(-1) @@ -31,44 +31,33 @@ def test_posy_busy() -> None: assert im.size == (96, 96) assert im.n_frames == 77 - with Image.open("Tests/images/ani/posy_busy_0.png") as png: - assert png.tobytes() == im.tobytes() + assert_image_equal_tofile(im, "Tests/images/ani/posy_busy_0.png") im.seek(24) - with Image.open("Tests/images/ani/posy_busy_24.png") as png: - assert png.tobytes() == im.tobytes() + assert_image_equal_tofile(im, "Tests/images/ani/posy_busy_24.png") with pytest.raises(EOFError): im.seek(77) -def test_stopwtch() -> None: +def test_seq_rate() -> None: with Image.open("Tests/images/ani/stopwtch.ani") as im: assert im.size == (32, 32) assert im.n_frames == 8 - assert im.info["seq"][0] == 0 - assert im.info["seq"][2] == 0 + assert im.info["seq"][:3] == [0, 1, 0] + assert im.info["rate"] == [8, 16, 16] + [8] * 42 - for i, r in enumerate(im.info["rate"]): - if i == 1 or i == 2: - assert r == 16 - else: - assert r == 8 - - with Image.open("Tests/images/ani/stopwtch_0.png") as png: - assert png.tobytes() == im.tobytes() + assert_image_equal_tofile(im, "Tests/images/ani/stopwtch_0.png") im.seek(5) - with Image.open("Tests/images/ani/stopwtch_5.png") as png: - assert png.tobytes() == im.tobytes() + assert_image_equal_tofile(im, "Tests/images/ani/stopwtch_5.png") with pytest.raises(EOFError): im.seek(8) def test_save() -> None: - directory_path = "Tests/images/ani/" filenames = [ "aero_busy_0.png", "aero_busy_8.png", @@ -78,7 +67,7 @@ def test_save() -> None: "stopwtch_5.png", ] - images = [Image.open(directory_path + filename) for filename in filenames] + images = [Image.open("Tests/images/ani/" + filename) for filename in filenames] with BytesIO() as output: images[0].save( diff --git a/Tests/test_file_cur.py b/Tests/test_file_cur.py index 9246b1101..f27de97c2 100644 --- a/Tests/test_file_cur.py +++ b/Tests/test_file_cur.py @@ -6,13 +6,16 @@ import pytest from PIL import CurImagePlugin, Image +from .helper import assert_image_equal -def test_deerstalker() -> None: + +def test_sanity() -> None: with Image.open("Tests/images/cur/deerstalker.cur") as im: assert im.size == (32, 32) assert im.info["hotspots"] == [(0, 0)] assert isinstance(im, CurImagePlugin.CurImageFile) - # Check some pixel colors to ensure image is loaded properly + + # Check pixel colors to ensure image is loaded properly assert im.getpixel((10, 1)) == (0, 0, 0, 0) assert im.getpixel((11, 1)) == (253, 254, 254, 1) assert im.getpixel((16, 16)) == (84, 87, 86, 255) @@ -23,7 +26,8 @@ def test_posy_link() -> None: assert im.size == (128, 128) assert im.info["sizes"] == {(128, 128), (96, 96), (64, 64), (48, 48), (32, 32)} assert im.info["hotspots"] == [(25, 7), (18, 5), (12, 3), (9, 2), (5, 1)] - # check some pixel colors + + # check pixel colors assert im.getpixel((0, 0)) == (0, 0, 0, 0) assert im.getpixel((20, 20)) == (0, 0, 0, 255) assert im.getpixel((40, 40)) == (255, 255, 255, 255) @@ -79,9 +83,8 @@ def test_save_win98_arrow() -> None: hotspots=[(10, 10)], bitmap_format="bmp", ) - with Image.open(output) as reloaded: - assert im.tobytes() == reloaded.tobytes() + assert_image_equal(im, reloaded) with BytesIO() as output: im.save(output, format="CUR") @@ -120,11 +123,10 @@ def test_save_posy_link() -> None: # make sure saved output is readable # and sizes/hotspots are correct with Image.open(output, formats=["CUR"]) as reloaded: - assert (48, 48) == reloaded.size - assert set(sizes[3:]) == reloaded.info["sizes"] + assert reloaded.size == (48, 48) + assert reloaded.info["sizes"] == set(sizes[3:]) - # make sure error is thrown when size and hotspot len's - # don't match + # check error is thrown when size and hotspot length don't match with pytest.raises(ValueError): im.save( output,