Skip to content

Commit c7457c1

Browse files
committed
Add a test for tuples that have negative coordinates.
Specifically, a negative width or height. Particularly, this seems like an easy case for a user to hit if somebody forgets that the PIL tuple style uses top, left, RIGHT, BOTTOM and not top, left, WIDTH, HEIGHT. Ask how I know. Test that we do handle that gracefully.
1 parent 2b50e70 commit c7457c1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/tests/test_implementation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,31 @@ def test_grab_with_tuple(mss_impl: Callable[..., MSSBase]) -> None:
251251
assert im.rgb == im2.rgb
252252

253253

254+
def test_grab_with_invalid_tuple(mss_impl: Callable[..., MSSBase]) -> None:
255+
with mss_impl() as sct:
256+
# Remember that rect tuples are PIL-style: (left, top, right, bottom)
257+
258+
# Negative top
259+
negative_box = (100, -100, 500, 500)
260+
with pytest.raises(ScreenShotError):
261+
sct.grab(negative_box)
262+
263+
# Negative left
264+
negative_box = (-100, 100, 500, 500)
265+
with pytest.raises(ScreenShotError):
266+
sct.grab(negative_box)
267+
268+
# Negative width (but right > 0)
269+
negative_box = (100, 100, 50, 500)
270+
with pytest.raises(ScreenShotError):
271+
sct.grab(negative_box)
272+
273+
# Negative height (but bottom > 0)
274+
negative_box = (100, 100, 500, 50)
275+
with pytest.raises(ScreenShotError):
276+
sct.grab(negative_box)
277+
278+
254279
def test_grab_with_tuple_percents(mss_impl: Callable[..., MSSBase]) -> None:
255280
with mss_impl() as sct:
256281
monitor = sct.monitors[1]

0 commit comments

Comments
 (0)