From cc22efda7a296c3e6ca9b40a4a4eb4d1af1741ac Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 28 Mar 2026 23:54:09 +1100 Subject: [PATCH] Parametrize tests --- Tests/test_imagefile.py | 178 +++++++++++----------------------------- 1 file changed, 47 insertions(+), 131 deletions(-) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 2dcebc4b1..5f4ed2eb0 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -295,6 +295,26 @@ class TestPyDecoder(CodecsTest): with pytest.raises(ValueError): MockPyDecoder.last.set_as_raw(b"\x00") + @pytest.mark.parametrize( + "extents", + ( + (-10, yoff, xoff + xsize, yoff + ysize), + (xoff, -10, xoff + xsize, yoff + ysize), + (xoff, yoff, -10, yoff + ysize), + (xoff, yoff, xoff + xsize, -10), + (xoff, yoff, xoff + xsize + 100, yoff + ysize), + (xoff, yoff, xoff + xsize, yoff + ysize + 100), + ), + ) + def test_extents(self, extents: tuple[int, int, int, int]) -> None: + buf = BytesIO(b"\x00" * 255) + + im = MockImageFile(buf) + im.tile = [ImageFile._Tile("MOCK", extents, 32, None)] + + with pytest.raises(ValueError): + im.load() + def test_extents_none(self) -> None: buf = BytesIO(b"\x00" * 255) @@ -308,53 +328,6 @@ class TestPyDecoder(CodecsTest): assert MockPyDecoder.last.state.xsize == 200 assert MockPyDecoder.last.state.ysize == 200 - def test_negative_offset(self) -> None: - buf = BytesIO(b"\x00" * 255) - - im = MockImageFile(buf) - im.tile = [ImageFile._Tile("MOCK", (-10, yoff, xsize, ysize), 32, None)] - - with pytest.raises(ValueError): - im.load() - - im.tile = [ImageFile._Tile("MOCK", (xoff, -10, xsize, ysize), 32, None)] - with pytest.raises(ValueError): - im.load() - - def test_negative_size(self) -> None: - buf = BytesIO(b"\x00" * 255) - - im = MockImageFile(buf) - im.tile = [ImageFile._Tile("MOCK", (xoff, yoff, -10, yoff + ysize), 32, None)] - - with pytest.raises(ValueError): - im.load() - - im.tile = [ImageFile._Tile("MOCK", (xoff, yoff, xoff + xsize, -10), 32, None)] - with pytest.raises(ValueError): - im.load() - - def test_oversize(self) -> None: - buf = BytesIO(b"\x00" * 255) - - im = MockImageFile(buf) - im.tile = [ - ImageFile._Tile( - "MOCK", (xoff, yoff, xoff + xsize + 100, yoff + ysize), 32, None - ) - ] - - with pytest.raises(ValueError): - im.load() - - im.tile = [ - ImageFile._Tile( - "MOCK", (xoff, yoff, xoff + xsize, yoff + ysize + 100), 32, None - ) - ] - with pytest.raises(ValueError): - im.load() - def test_decode(self) -> None: decoder = ImageFile.PyDecoder("") with pytest.raises(NotImplementedError): @@ -384,6 +357,33 @@ class TestPyEncoder(CodecsTest): assert MockPyEncoder.last.state.xsize == xsize assert MockPyEncoder.last.state.ysize == ysize + @pytest.mark.parametrize( + "extents", + ( + (-10, yoff, xoff + xsize, yoff + ysize), + (xoff, -10, xoff + xsize, yoff + ysize), + (xoff, yoff, -10, yoff + ysize), + (xoff, yoff, xoff + xsize, -10), + (xoff, yoff, xoff + xsize + 100, yoff + ysize), + (xoff, yoff, xoff + xsize, yoff + ysize + 100), + ), + ) + def test_extents(self, extents: tuple[int, int, int, int]) -> None: + buf = BytesIO(b"\x00" * 255) + + im = MockImageFile(buf) + + fp = BytesIO() + MockPyEncoder.last = None + with pytest.raises(ValueError): + ImageFile._save(im, fp, [ImageFile._Tile("MOCK", extents, 0, "RGB")]) + last: MockPyEncoder | None = MockPyEncoder.last + assert last + assert last.cleanup_called + + with pytest.raises(ValueError): + ImageFile._save(im, fp, [ImageFile._Tile("MOCK", extents, 0, "RGB")]) + def test_extents_none(self) -> None: buf = BytesIO(b"\x00" * 255) @@ -399,90 +399,6 @@ class TestPyEncoder(CodecsTest): assert MockPyEncoder.last.state.xsize == 200 assert MockPyEncoder.last.state.ysize == 200 - def test_negative_offset(self) -> None: - buf = BytesIO(b"\x00" * 255) - - im = MockImageFile(buf) - - fp = BytesIO() - MockPyEncoder.last = None - with pytest.raises(ValueError): - ImageFile._save( - im, - fp, - [ - ImageFile._Tile( - "MOCK", (-10, yoff, xoff + xsize, yoff + ysize), 0, "RGB" - ) - ], - ) - last: MockPyEncoder | None = MockPyEncoder.last - assert last - assert last.cleanup_called - - with pytest.raises(ValueError): - ImageFile._save( - im, - fp, - [ - ImageFile._Tile( - "MOCK", (xoff, -10, xoff + xsize, yoff + ysize), 0, "RGB" - ) - ], - ) - - def test_negative_size(self) -> None: - buf = BytesIO(b"\x00" * 255) - - im = MockImageFile(buf) - - fp = BytesIO() - MockPyEncoder.last = None - with pytest.raises(ValueError): - ImageFile._save( - im, - fp, - [ImageFile._Tile("MOCK", (xoff, yoff, -10, yoff + ysize), 0, "RGB")], - ) - last: MockPyEncoder | None = MockPyEncoder.last - assert last - assert last.cleanup_called - - with pytest.raises(ValueError): - ImageFile._save( - im, - fp, - [ImageFile._Tile("MOCK", (xoff, yoff, xoff + xsize, -10), 0, "RGB")], - ) - - def test_oversize(self) -> None: - buf = BytesIO(b"\x00" * 255) - - im = MockImageFile(buf) - - fp = BytesIO() - with pytest.raises(ValueError): - ImageFile._save( - im, - fp, - [ - ImageFile._Tile( - "MOCK", (xoff, yoff, xoff + xsize + 100, yoff + ysize), 0, "RGB" - ) - ], - ) - - with pytest.raises(ValueError): - ImageFile._save( - im, - fp, - [ - ImageFile._Tile( - "MOCK", (xoff, yoff, xoff + xsize, yoff + ysize + 100), 0, "RGB" - ) - ], - ) - def test_encode(self) -> None: encoder = ImageFile.PyEncoder("") with pytest.raises(NotImplementedError):