From 15b5d1724462c270f8c469e4443ffb04980f5c28 Mon Sep 17 00:00:00 2001 From: Reminisque Date: Sun, 21 Jun 2020 22:48:49 -0700 Subject: [PATCH 1/5] Add unit test for mouse.set_cursor --- test/mouse_test.py | 62 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/test/mouse_test.py b/test/mouse_test.py index e5c5c9e302..1a092c83a8 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -61,9 +61,67 @@ def todo_test_get_cursor(self): """Ensures get_cursor works correctly.""" self.fail() - def todo_test_set_cursor(self): + def test_set_cursor(self): """Ensures set_cursor works correctly.""" - self.fail() + size = (8, 8) + hotspot = (0, 0) + xormask = (0, 126, 64, 64, 32, 16, 0, 0) + andmask = (254, 255, 254, 112, 56, 28, 12, 0) + expected_cursor = (size, hotspot, xormask, andmask) + + + try: + # Error should be raised when the display is uninitialized + with self.assertRaises(pygame.error): + pygame.display.quit() + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + + pygame.display.init() + + # TypeError raised when PyArg_ParseTuple fails to parse parameters + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) + + # TypeError raised when either mask is not a sequence + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) + + # TypeError raised when element of mask is not an integer + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) + + # ValueError raised when width not divisible by 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) + + # ValueError raised when length of either mask != width * height / 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) + + # Working as intended + self.assertEqual( + pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None + ) + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) + pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)) + self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) + + # SDLError should be raised when mouse cursor is NULL + except pygame.error: + with self.assertRaises(pygame.error): + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + def test_get_focused(self): """Ensures get_focused returns the correct type.""" From 0d4cfc8d7ccef8c522d55e2f6c076a3737906d72 Mon Sep 17 00:00:00 2001 From: Reminisque Date: Sun, 21 Jun 2020 22:53:56 -0700 Subject: [PATCH 2/5] Fix unnecessarily putting tests inside try --- test/mouse_test.py | 78 ++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/test/mouse_test.py b/test/mouse_test.py index 1a092c83a8..e8758c2919 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -69,46 +69,45 @@ def test_set_cursor(self): andmask = (254, 255, 254, 112, 56, 28, 12, 0) expected_cursor = (size, hotspot, xormask, andmask) + # Error should be raised when the display is uninitialized + with self.assertRaises(pygame.error): + pygame.display.quit() + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + pygame.display.init() + + # TypeError raised when PyArg_ParseTuple fails to parse parameters + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) + + # TypeError raised when either mask is not a sequence + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) + + # TypeError raised when element of mask is not an integer + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) + + # ValueError raised when width not divisible by 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) + + # ValueError raised when length of either mask != width * height / 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) + + # Working as intended try: - # Error should be raised when the display is uninitialized - with self.assertRaises(pygame.error): - pygame.display.quit() - pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - - pygame.display.init() - - # TypeError raised when PyArg_ParseTuple fails to parse parameters - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) - - # TypeError raised when either mask is not a sequence - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) - - # TypeError raised when element of mask is not an integer - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) - - # ValueError raised when width not divisible by 8 - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) - - # ValueError raised when length of either mask != width * height / 8 - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) - - # Working as intended self.assertEqual( pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None ) @@ -116,13 +115,12 @@ def test_set_cursor(self): self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)) self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) - + # SDLError should be raised when mouse cursor is NULL except pygame.error: with self.assertRaises(pygame.error): pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - def test_get_focused(self): """Ensures get_focused returns the correct type.""" focused = pygame.mouse.get_focused() From 953fd837e0eaa7ac812e513586c6c5fa4602ea6f Mon Sep 17 00:00:00 2001 From: Reminisque Date: Sun, 28 Jun 2020 16:48:43 -0700 Subject: [PATCH 3/5] Add branch to test for SDL2 --- test/mouse_test.py | 104 ++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/test/mouse_test.py b/test/mouse_test.py index e8758c2919..14880e4c33 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -69,57 +69,65 @@ def test_set_cursor(self): andmask = (254, 255, 254, 112, 56, 28, 12, 0) expected_cursor = (size, hotspot, xormask, andmask) - # Error should be raised when the display is uninitialized - with self.assertRaises(pygame.error): - pygame.display.quit() - pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + # Unable to use mouse.get_cursor in SDL2, so check that mouse.set_cursor + # doesn't raise a error when called with data expected to work + if not SDL1: + try: + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + except pygame.error: + self.fail() + else: + # Error should be raised when the display is uninitialized + with self.assertRaises(pygame.error): + pygame.display.quit() + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - pygame.display.init() - - # TypeError raised when PyArg_ParseTuple fails to parse parameters - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) - - # TypeError raised when either mask is not a sequence - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) - - # TypeError raised when element of mask is not an integer - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) - - # ValueError raised when width not divisible by 8 - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) - - # ValueError raised when length of either mask != width * height / 8 - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) - - # Working as intended - try: - self.assertEqual( - pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None - ) - pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) - pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)) - self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) + pygame.display.init() - # SDLError should be raised when mouse cursor is NULL - except pygame.error: - with self.assertRaises(pygame.error): + # TypeError raised when PyArg_ParseTuple fails to parse parameters + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) + + # TypeError raised when either mask is not a sequence + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) + + # TypeError raised when element of mask is not an integer + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) + + # ValueError raised when width not divisible by 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) + + # ValueError raised when length of either mask != width * height / 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) + + # Working as intended + try: + self.assertEqual( + pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None + ) pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) + pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)) + self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) + + # SDLError should be raised when mouse cursor is NULL + except pygame.error: + with self.assertRaises(pygame.error): + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) def test_get_focused(self): """Ensures get_focused returns the correct type.""" From bebc145e21c0e58147fe5c424b9d0305995661a9 Mon Sep 17 00:00:00 2001 From: Reminisque Date: Fri, 3 Jul 2020 17:58:01 -0700 Subject: [PATCH 4/5] Split test_set_cursor into different SDL versions --- test/mouse_test.py | 158 +++++++++++++++++++++++++++++---------------- 1 file changed, 104 insertions(+), 54 deletions(-) diff --git a/test/mouse_test.py b/test/mouse_test.py index 14880e4c33..6b682e481d 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -61,7 +61,8 @@ def todo_test_get_cursor(self): """Ensures get_cursor works correctly.""" self.fail() - def test_set_cursor(self): + @unittest.skipIf(not SDL1, "mouse.get_cursor only available in SDL1") + def test_set_cursor_sdl1(self): """Ensures set_cursor works correctly.""" size = (8, 8) hotspot = (0, 0) @@ -69,65 +70,114 @@ def test_set_cursor(self): andmask = (254, 255, 254, 112, 56, 28, 12, 0) expected_cursor = (size, hotspot, xormask, andmask) - # Unable to use mouse.get_cursor in SDL2, so check that mouse.set_cursor - # doesn't raise a error when called with data expected to work - if not SDL1: - try: - pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - except pygame.error: - self.fail() - else: - # Error should be raised when the display is uninitialized + # Error should be raised when the display is uninitialized + with self.assertRaises(pygame.error): + pygame.display.quit() + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + + pygame.display.init() + + # TypeError raised when PyArg_ParseTuple fails to parse parameters + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) + + # TypeError raised when either mask is not a sequence + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) + + # TypeError raised when element of mask is not an integer + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) + + # ValueError raised when width not divisible by 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) + + # ValueError raised when length of either mask != width * height / 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) + + # Working as intended + try: + self.assertEqual( + pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None) + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) + self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) + pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)) + self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) + + # SDLError should be raised when mouse cursor is NULL + except pygame.error: with self.assertRaises(pygame.error): - pygame.display.quit() pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - pygame.display.init() - - # TypeError raised when PyArg_ParseTuple fails to parse parameters - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) + + @unittest.skipIf(SDL1 or os.environ.get("SDL_VIDEODRIVER", "") == "dummy", + "Fails on SDL2 with dummy video driver, unable to use mouse.get_cursor") + def test_set_cursor_sdl2(self): + """Ensures set_cursor works correctly.""" + size = (8, 8) + hotspot = (0, 0) + xormask = (0, 126, 64, 64, 32, 16, 0, 0) + andmask = (254, 255, 254, 112, 56, 28, 12, 0) + expected_cursor = (size, hotspot, xormask, andmask) - # TypeError raised when either mask is not a sequence - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) + # Error should be raised when the display is uninitialized + with self.assertRaises(pygame.error): + pygame.display.quit() + pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - # TypeError raised when element of mask is not an integer - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) - with self.assertRaises(TypeError): - pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) - - # ValueError raised when width not divisible by 8 - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) - - # ValueError raised when length of either mask != width * height / 8 - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) - with self.assertRaises(ValueError): - pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) - - # Working as intended - try: - self.assertEqual( - pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None - ) + pygame.display.init() + + # TypeError raised when PyArg_ParseTuple fails to parse parameters + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(("w", "h"), hotspot, xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("0", "0"), xormask, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, ("x", "y", "z"), xormask, andmask) + + # TypeError raised when either mask is not a sequence + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, 12345678, andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, 12345678) + + # TypeError raised when element of mask is not an integer + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, "00000000", andmask) + with self.assertRaises(TypeError): + pygame.mouse.set_cursor(size, hotspot, xormask, (2, [0], 4, 0, 0, 8, 0, 1)) + + # ValueError raised when width not divisible by 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((3, 8), hotspot, xormask, andmask) + + # ValueError raised when length of either mask != width * height / 8 + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, (128, 64, 32), andmask) + with self.assertRaises(ValueError): + pygame.mouse.set_cursor((16, 2), hotspot, xormask, (192, 96, 48, 0, 1)) + + # Working as intended, no checks for same value as mouse.get_cursor in SDL2 + try: + self.assertEqual( + pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None) + + # SDLError should be raised when mouse cursor is NULL + except pygame.error: + with self.assertRaises(pygame.error): pygame.mouse.set_cursor(size, hotspot, xormask, andmask) - self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) - pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)) - self.assertEqual(pygame.mouse.get_cursor(), expected_cursor) - - # SDLError should be raised when mouse cursor is NULL - except pygame.error: - with self.assertRaises(pygame.error): - pygame.mouse.set_cursor(size, hotspot, xormask, andmask) def test_get_focused(self): """Ensures get_focused returns the correct type.""" From b9a65a100c7c7ccd97dd9269a6aca57f8a8feec4 Mon Sep 17 00:00:00 2001 From: Reminisque Date: Sat, 4 Jul 2020 17:17:18 -0700 Subject: [PATCH 5/5] Remove unused var and added seq. check in SDL2 --- test/mouse_test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/mouse_test.py b/test/mouse_test.py index 6b682e481d..f5a782a84f 100644 --- a/test/mouse_test.py +++ b/test/mouse_test.py @@ -130,7 +130,6 @@ def test_set_cursor_sdl2(self): hotspot = (0, 0) xormask = (0, 126, 64, 64, 32, 16, 0, 0) andmask = (254, 255, 254, 112, 56, 28, 12, 0) - expected_cursor = (size, hotspot, xormask, andmask) # Error should be raised when the display is uninitialized with self.assertRaises(pygame.error): @@ -172,7 +171,9 @@ def test_set_cursor_sdl2(self): # Working as intended, no checks for same value as mouse.get_cursor in SDL2 try: self.assertEqual( - pygame.mouse.set_cursor((16, 1), hotspot, (8, 0), (0, 192)), None) + pygame.mouse.set_cursor(size, hotspot, xormask, andmask), None) + self.assertEqual( + pygame.mouse.set_cursor(size, hotspot, list(xormask), list(andmask)), None) # SDLError should be raised when mouse cursor is NULL except pygame.error: