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

Checking if the player has won
This commit is contained in:
2026-10-04 12:40:15 +08:00
parent 15d5f7134d
commit ce87f23314
6 changed files with 66 additions and 24 deletions
+18 -13
View File
@@ -1,23 +1,28 @@
from hangman import main as cli
from hangman.game import Game
def run(monkeypatch, capsys, word, letter):
monkeypatch.setattr(cli, "choose_word", lambda: word)
monkeypatch.setattr("builtins.input", lambda _="": letter)
cli.main()
def run(monkeypatch, capsys, word, inputs):
it = iter(inputs)
monkeypatch.setattr("builtins.input", lambda _="": next(it))
cli.play(Game(word))
return capsys.readouterr().out
def test_right_guess_fills_blanks(monkeypatch, capsys):
out = run(monkeypatch, capsys, "camel", "a")
def test_play_until_won(monkeypatch, capsys):
out = run(monkeypatch, capsys, "hi", ["z", "h", "i"])
assert out.splitlines() == [
"_ _ _ _ _",
"Right, 'a' is in the word.",
"_ a _ _ _",
"_ _",
"Wrong, 'z' is not in the word.",
"_ _",
"Right, 'h' is in the word.",
"h _",
"Right, 'i' is in the word.",
"h i",
"You win!",
]
def test_wrong_guess_keeps_blanks(monkeypatch, capsys):
out = run(monkeypatch, capsys, "camel", "z")
assert "Wrong" in out
assert out.splitlines()[-1] == "_ _ _ _ _"
def test_repeated_letter_fills_all_blanks(monkeypatch, capsys):
out = run(monkeypatch, capsys, "aa", ["a"])
assert out.splitlines()[-2:] == ["a a", "You win!"]