From cdcdef5521fea69cb33185fe8b142a99f9819c8e Mon Sep 17 00:00:00 2001 From: Rooster <161361481+mr-peppermint@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:20:43 +0530 Subject: [PATCH] Add Number Guessing Game boundary checks --- .../Number-Guessing-Game.py | 7 ++ tests/test_number_guessing_game_boundaries.py | 106 ++++++++++++++++++ work/R26-T052/check.py | 18 +++ work/R26-T052/evidence.md | 27 +++++ 4 files changed, 158 insertions(+) create mode 100644 tests/test_number_guessing_game_boundaries.py create mode 100644 work/R26-T052/check.py create mode 100644 work/R26-T052/evidence.md diff --git a/games/Number-Guessing-Game/Number-Guessing-Game.py b/games/Number-Guessing-Game/Number-Guessing-Game.py index dd032d14..858323af 100644 --- a/games/Number-Guessing-Game/Number-Guessing-Game.py +++ b/games/Number-Guessing-Game/Number-Guessing-Game.py @@ -31,6 +31,13 @@ def main() -> None: + """Run the game within its supported input boundaries. + + Difficulty choices are 1, 2, and 3 (with 2 as the default), and guesses + must be integers from MIN_NUMBER through MAX_NUMBER, inclusive. A guess + outside that range is rejected without using an attempt or entering the + guess history. + """ print("šŸŽÆ Welcome to the Number Guessing Game!\n") while True: diff --git a/tests/test_number_guessing_game_boundaries.py b/tests/test_number_guessing_game_boundaries.py new file mode 100644 index 00000000..5e9d815b --- /dev/null +++ b/tests/test_number_guessing_game_boundaries.py @@ -0,0 +1,106 @@ +"""Boundary checks for the Number Guessing Game's interactive main(). + +The game reads choices and guesses through imported helpers and chooses a +secret number randomly, so the tests replace those external inputs with +controlled values and capture the printed output. +""" + +import importlib.util +from pathlib import Path +from unittest.mock import patch + +import pytest + + +PROJECT_ROOT = Path(__file__).resolve().parents[1] +MODULE_PATH = ( + PROJECT_ROOT + / "games" + / "Number-Guessing-Game" + / "Number-Guessing-Game.py" +) + + +@pytest.fixture +def game(): + spec = importlib.util.spec_from_file_location("number_guessing_game", MODULE_PATH) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def run_game(game, difficulty, guesses, secret): + """Run one round with deterministic choices, guesses, and secret number.""" + with ( + patch.object(game, "get_choice", side_effect=[difficulty, "n"]), + patch.object(game, "get_int", side_effect=guesses) as get_int, + patch.object(game.random, "randint", return_value=secret) as randint, + ): + game.main() + randint.assert_called_once_with(game.MIN_NUMBER, game.MAX_NUMBER) + return get_int + + +@pytest.mark.parametrize( + ("difficulty", "attempt_limit"), + [("1", 15), ("2", 10), ("3", 5)], +) +def test_difficulty_choices_use_the_documented_attempt_limits( + game, capsys, difficulty, attempt_limit +): + # Source precondition: DIFFICULTY_ATTEMPTS at Number-Guessing-Game.py:21-25. + run_game(game, difficulty, [50], 50) + + output = capsys.readouterr().out + assert f"You have {attempt_limit} attempts." in output + assert f"Attempts: 1/{attempt_limit}" in output + + +def test_smallest_supported_guess_is_accepted(game, capsys): + # Source precondition: MIN_NUMBER is 1 at Number-Guessing-Game.py:17. + run_game(game, "1", [1], 1) + + output = capsys.readouterr().out + assert "You have 15 attempts." in output + assert "Attempts: 1/15" in output + assert "Guesses: 1" in output + + +def test_ordinary_guess_is_accepted(game, capsys): + # Source preconditions: difficulty 2 allows 10 tries (lines 21-25), and + # guesses from 1 through 100 pass main()'s range check (lines 76-78). + run_game(game, "2", [50], 50) + + output = capsys.readouterr().out + assert "You have 10 attempts." in output + assert "Attempts: 1/10" in output + assert "Guesses: 50" in output + + +def test_largest_supported_guess_is_accepted(game, capsys): + # Source precondition: MAX_NUMBER is 100 at Number-Guessing-Game.py:18. + run_game(game, "3", [100], 100) + + output = capsys.readouterr().out + assert "You have 5 attempts." in output + assert "Attempts: 1/5" in output + assert "Guesses: 100" in output + + +def test_guesses_outside_the_range_do_not_use_attempts_or_enter_history( + game, capsys +): + # Source preconditions: main() checks the inclusive range at lines 76-78, + # then increments attempts and appends to guess_history at lines 80-81. + get_int = run_game(game, "1", [0, 101, 100], 100) + + output = capsys.readouterr().out + assert output.count("Enter a number between 1 and 100.") == 2 + assert [call.kwargs["prompt"] for call in get_int.call_args_list] == [ + "\nšŸ‘‰ Guess (15 left): ", + "\nšŸ‘‰ Guess (15 left): ", + "\nšŸ‘‰ Guess (15 left): ", + ] + assert "Attempts: 1/15" in output + assert "Guesses: 100" in output + assert "0, 101" not in output diff --git a/work/R26-T052/check.py b/work/R26-T052/check.py new file mode 100644 index 00000000..c48e43ab --- /dev/null +++ b/work/R26-T052/check.py @@ -0,0 +1,18 @@ +"""Run only the focused Number Guessing Game boundary tests.""" + +from pathlib import Path +import subprocess +import sys + + +PROJECT_ROOT = Path(__file__).resolve().parents[2] +TEST_FILE = PROJECT_ROOT / "tests" / "test_number_guessing_game_boundaries.py" + + +if __name__ == "__main__": + result = subprocess.run( + [sys.executable, "-m", "pytest", "-q", str(TEST_FILE)], + cwd=PROJECT_ROOT, + check=False, + ) + raise SystemExit(result.returncode) diff --git a/work/R26-T052/evidence.md b/work/R26-T052/evidence.md new file mode 100644 index 00000000..469e564e --- /dev/null +++ b/work/R26-T052/evidence.md @@ -0,0 +1,27 @@ +# R26-T052 check evidence + +## Command and environment + +- Command: `/private/tmp/task1-venv/bin/python work/R26-T052/check.py` +- Python: 3.11.17 +- pytest: 9.2.0 +- Result: 7 passed, 0 failed + +## Cases and expected results + +| Input case | Expected result | Observed result | +|---|---|---| +| Difficulty `1`, secret `50`, guess `50` | Easy difficulty permits 15 attempts; the correct guess wins on attempt 1. | Printed 15 attempts and `Attempts: 1/15`. | +| Difficulty `2`, secret `50`, guess `50` | Medium difficulty permits 10 attempts; the correct guess wins on attempt 1. | Printed 10 attempts and `Attempts: 1/10`. | +| Difficulty `3`, secret `50`, guess `50` | Hard difficulty permits 5 attempts; the correct guess wins on attempt 1. | Printed 5 attempts and `Attempts: 1/5`. | +| Difficulty `1`, secret `1`, guess `1` | The inclusive lower bound is accepted and wins on attempt 1. | Printed `Attempts: 1/15` and guess history `1`. | +| Difficulty `2`, secret `50`, guess `50` | An ordinary in-range guess is accepted and wins on attempt 1. | Printed `Attempts: 1/10` and guess history `50`. | +| Difficulty `3`, secret `100`, guess `100` | The inclusive upper bound is accepted and wins on attempt 1. | Printed `Attempts: 1/5` and guess history `100`. | +| Difficulty `1`, secret `100`, guesses `0`, `101`, `100` | `0` and `101` are rejected without spending attempts or entering history; `100` then wins on attempt 1. | Printed the range warning twice, all three prompts showed 15 remaining, and history contained only `100`. | + +## Source contract anchors + +- `MIN_NUMBER` and `MAX_NUMBER`: `games/Number-Guessing-Game/Number-Guessing-Game.py:17-18`. +- Difficulty-to-attempt mapping: `games/Number-Guessing-Game/Number-Guessing-Game.py:21-25`. +- Inclusive guess check: `games/Number-Guessing-Game/Number-Guessing-Game.py:76-78`. +- Attempt increment and history append after validation: `games/Number-Guessing-Game/Number-Guessing-Game.py:80-81`.