step 5
TirSystem/github-action: Sync GitHub mirror metadata / sync-metadata (push) Successful in 5s

Improving the user experience
This commit is contained in:
2026-10-04 12:46:03 +08:00
parent a76df46b79
commit 16cd9d42b9
6 changed files with 222 additions and 30 deletions
+31 -14
View File
@@ -1,4 +1,5 @@
from hangman import main as cli
from hangman.constants import ANSI_BOLD, ANSI_RED, ANSI_RESET, STAGES
from hangman.game import Game
@@ -11,24 +12,40 @@ def run(monkeypatch, capsys, word, inputs):
def test_play_until_won(monkeypatch, capsys):
out = run(monkeypatch, capsys, "hi", ["z", "h", "i"])
assert out.splitlines() == [
"_ _",
"Wrong, 'z' is not in the word. Lives left: 5",
"_ _",
"Right, 'h' is in the word.",
"h _",
"Right, 'i' is in the word.",
"h i",
"You win!",
]
assert "'z' is not in the word. You lose a life." in out
assert "h i" in out
assert out.splitlines()[-1] == "You win!"
def test_repeated_letter_fills_all_blanks(monkeypatch, capsys):
out = run(monkeypatch, capsys, "aa", ["a"])
assert out.splitlines()[-2:] == ["a a", "You win!"]
def test_repeated_and_invalid_input(monkeypatch, capsys):
out = run(monkeypatch, capsys, "hi", ["h", "h", "?", "ab", "i"])
assert "You already guessed 'h'." in out
assert out.count("Please enter a single letter.") == 2
assert out.splitlines()[-1] == "You win!"
def test_play_until_lost(monkeypatch, capsys):
out = run(monkeypatch, capsys, "a", list("bcdefg"))
assert "Lives left: 0" in out
assert STAGES[0] in out
assert out.splitlines()[-1] == "You lose. The word was: a"
def test_gallows_grows_with_wrong_guesses(monkeypatch, capsys):
out = run(monkeypatch, capsys, "a", ["b", "a"])
assert STAGES[6] in out and STAGES[5] in out
def test_title_plain_when_not_a_terminal(capsys):
cli.show_title()
out = capsys.readouterr().out
assert "" not in out
assert "|___/" in out
def test_title_colored_in_a_terminal(monkeypatch, capsys):
monkeypatch.setattr("sys.stdout.isatty", lambda: True, raising=False)
monkeypatch.delenv("NO_COLOR", raising=False)
monkeypatch.setattr(cli.os, "system", lambda _: 0)
cli.show_title()
out = capsys.readouterr().out
assert out.startswith(ANSI_BOLD + ANSI_RED) and ANSI_RESET in out