Skip to content

Commit 2462807

Browse files
gh-138772: Add tests for Turtle.dot() signature (GH-138773)
1 parent e9c538d commit 2462807

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

Lib/test/test_turtle.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,25 @@ def patch_screen():
6060
We must patch the _Screen class itself instead of the _Screen
6161
instance because instantiating it requires a display.
6262
"""
63+
# Create a mock screen that delegates color validation to the real TurtleScreen methods
64+
mock_screen = unittest.mock.MagicMock()
65+
mock_screen.__class__ = turtle._Screen
66+
mock_screen.mode.return_value = "standard"
67+
mock_screen._colormode = 1.0
68+
69+
def mock_iscolorstring(color):
70+
valid_colors = {'red', 'green', 'blue', 'black', 'white', 'yellow',
71+
'orange', 'purple', 'pink', 'brown', 'gray', 'grey',
72+
'cyan', 'magenta'}
73+
74+
return color in valid_colors or (isinstance(color, str) and color.startswith('#'))
75+
76+
mock_screen._iscolorstring = mock_iscolorstring
77+
mock_screen._colorstr = turtle._Screen._colorstr.__get__(mock_screen)
78+
6379
return unittest.mock.patch(
6480
"turtle._Screen.__new__",
65-
**{
66-
"return_value.__class__": turtle._Screen,
67-
"return_value.mode.return_value": "standard",
68-
},
81+
return_value=mock_screen
6982
)
7083

7184

@@ -635,6 +648,28 @@ def test_poly_context_when_creating_poly(self):
635648
self.assertTrue(self.turtle._creatingPoly)
636649
self.assertFalse(self.turtle._creatingPoly)
637650

651+
def test_dot_signature(self):
652+
self.turtle.dot()
653+
self.turtle.dot(10)
654+
self.turtle.dot(size=10)
655+
self.turtle.dot((0, 0, 0))
656+
self.turtle.dot(size=(0, 0, 0))
657+
self.turtle.dot("blue")
658+
self.turtle.dot("")
659+
self.turtle.dot(size="blue")
660+
self.turtle.dot(20, "blue")
661+
self.turtle.dot(20, "blue")
662+
self.turtle.dot(20, (0, 0, 0))
663+
self.turtle.dot(20, 0, 0, 0)
664+
with self.assertRaises(TypeError):
665+
self.turtle.dot(color="blue")
666+
self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, "_not_a_color_")
667+
self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, (0, 0, 0, 0))
668+
self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, 0, 0, 0, 0)
669+
self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, (-1, 0, 0))
670+
self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, -1, 0, 0)
671+
self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, (0, 257, 0))
672+
self.assertRaises(turtle.TurtleGraphicsError, self.turtle.dot, 0, 0, 257, 0)
638673

639674
class TestModuleLevel(unittest.TestCase):
640675
def test_all_signatures(self):

0 commit comments

Comments
 (0)