Add the Pen protocol, window helpers and a recording fake turtle

The drawing functions take a Pen, the part of turtle.Turtle they use, so a test can
pass a fake that records every call. window.py is the only module that imports
turtle, and it does so inside the functions so that importing the package needs no Tk.

Task: MIL-002#1
Task: MIL-002#2
This commit is contained in:
2026-10-07 23:58:50 +08:00
parent d38f114cc2
commit e2249de46e
4 changed files with 271 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
import sys
import pytest
from turtle_challenges.window import (
TURTLE_UNAVAILABLE_MESSAGE,
TurtleUnavailableError,
create_pen,
create_window,
)
@pytest.fixture
def without_turtle(monkeypatch: pytest.MonkeyPatch) -> None:
"""Make `import turtle` fail, as on a Python without Tk."""
monkeypatch.setitem(sys.modules, "turtle", None)
def test_create_window_reports_a_missing_turtle(without_turtle: None) -> None:
with pytest.raises(TurtleUnavailableError) as error_info:
create_window()
assert str(error_info.value) == TURTLE_UNAVAILABLE_MESSAGE
assert isinstance(error_info.value.__cause__, ImportError)
def test_create_pen_reports_a_missing_turtle(without_turtle: None) -> None:
with pytest.raises(TurtleUnavailableError):
create_pen()
def test_the_missing_turtle_message_names_the_debian_package() -> None:
assert "python3-tk" in TURTLE_UNAVAILABLE_MESSAGE