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.
()
| 55 | |
| 56 | |
| 57 | def 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 | |
| 85 | class TurtleConfigTest(unittest.TestCase): |