]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-138772: Add tests for Turtle.dot() signature (GH-138773) (#140992)
authorJan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com>
Wed, 5 Nov 2025 15:24:46 +0000 (16:24 +0100)
committerGitHub <noreply@github.com>
Wed, 5 Nov 2025 15:24:46 +0000 (17:24 +0200)
Co-authored-by: Jan-Eric Nitschke <47750513+JanEricNitschke@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Lib/test/test_turtle.py

index 14121a590a50267ca42ca3ec57e21c6a15a6334a..a572ff4b9e91ccc4762ae45bcd48389dd7ebd3d9 100644 (file)
@@ -1,5 +1,6 @@
 import pickle
 import unittest
+import unittest.mock
 from test import support
 from test.support import import_helper
 from test.support import os_helper
@@ -50,6 +51,34 @@ visible = False
 """
 
 
+def patch_screen():
+    """Patch turtle._Screen for testing without a display.
+
+    We must patch the _Screen class itself instead of the _Screen
+    instance because instantiating it requires a display.
+    """
+    # Create a mock screen that delegates color validation to the real TurtleScreen methods
+    mock_screen = unittest.mock.MagicMock()
+    mock_screen.__class__ = turtle._Screen
+    mock_screen.mode.return_value = "standard"
+    mock_screen._colormode = 1.0
+
+    def mock_iscolorstring(color):
+        valid_colors = {'red', 'green', 'blue', 'black', 'white', 'yellow',
+                        'orange', 'purple', 'pink', 'brown', 'gray', 'grey',
+                        'cyan', 'magenta'}
+
+        return color in valid_colors or (isinstance(color, str) and color.startswith('#'))
+
+    mock_screen._iscolorstring = mock_iscolorstring
+    mock_screen._colorstr = turtle._Screen._colorstr.__get__(mock_screen)
+
+    return unittest.mock.patch(
+        "turtle._Screen.__new__",
+        return_value=mock_screen
+    )
+
+
 class TurtleConfigTest(unittest.TestCase):
 
     def get_cfg_file(self, cfg_str):
@@ -461,6 +490,38 @@ class TestTPen(unittest.TestCase):
             self.assertTrue(tpen.isdown())
 
 
+class TestTurtle(unittest.TestCase):
+    def setUp(self):
+        with patch_screen():
+            self.turtle = turtle.Turtle()
+
+        # Reset the Screen singleton to avoid reference leaks
+        self.addCleanup(setattr, turtle.Turtle, '_screen', None)
+
+    def test_dot_signature(self):
+        self.turtle.dot()
+        self.turtle.dot(10)
+        self.turtle.dot(size=10)
+        self.turtle.dot((0, 0, 0))
+        self.turtle.dot(size=(0, 0, 0))
+        self.turtle.dot("blue")
+        self.turtle.dot("")
+        self.turtle.dot(size="blue")
+        self.turtle.dot(20, "blue")
+        self.turtle.dot(20, "blue")
+        self.turtle.dot(20, (0, 0, 0))
+        self.turtle.dot(20, 0, 0, 0)
+        with self.assertRaises(TypeError):
+            self.turtle.dot(color="blue")
+        self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, "_not_a_color_")
+        self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, (0, 0, 0, 0))
+        self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, 0, 0, 0, 0)
+        self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, (-1, 0, 0))
+        self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, -1, 0, 0)
+        self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, (0, 257, 0))
+        self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, 0, 257, 0)
+
+
 class TestModuleLevel(unittest.TestCase):
     def test_all_signatures(self):
         import inspect