MCPcopy Index your code
hub / github.com/python/cpython / patch_screen

Function patch_screen

Lib/test/test_turtle.py:57–82  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

55
56
57def patch_screen():
58 """Patch turtle._Screen for testing without a display.
59
60 We must patch the _Screen class itself instead of the _Screen
61 instance because instantiating it requires a display.
62 """
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
79 return unittest.mock.patch(
80 "turtle._Screen.__new__",
81 return_value=mock_screen
82 )
83
84
85class TurtleConfigTest(unittest.TestCase):

Callers 1

setUpMethod · 0.85

Calls 1

__get__Method · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…