Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue272 fix #292

Open
wants to merge 1 commit into
base: 2022/python
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions game/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,78 @@ def exists():
@check50.check(exists)
def test_string_level():
"""game.py rejects non-numeric level"""
check50.run("python3 game.py").stdin("cat", prompt=True).reject()
check50.run("python3 game.py").stdin("cat", prompt=True).stdout(
regex("Level"), "Level:"
).kill()


@check50.check(exists)
def test_integer_level():
"""game.py rejects out-of-range level"""
check50.run("python3 game.py").stdin("0", prompt=True).reject()
check50.run("python3 game.py").stdin("0", prompt=True).stdout(
regex("Level"), "Level:"
).kill()


@check50.check(exists)
def test_valid_level():
"""game.py accepts valid level"""
check50.run("python3 game.py").stdin("10", prompt=True).stdout(regex("Guess"), "Guess:", regex=True).kill()
check50.run("python3 game.py").stdin("10", prompt=True).stdout(
regex("Guess"), "Guess:", regex=True
).kill()


@check50.check(test_valid_level)
def test_string_guess():
"""game.py rejects non-numeric guess"""
check50.run("python3 game.py").stdin("1", prompt=True).stdin("cat", prompt=True).reject()
"""game.py rejects nonnumeric guess"""
check50.run("python3 game.py").stdin("1", prompt=True).stdin(
"cat", prompt=True
).reject()


@check50.check(test_valid_level)
def test_integer_guess():
"""game.py rejects out-of-range guess"""
check50.run("python3 game.py").stdin("1", prompt=True).stdin("0", prompt=True).reject()
def test_nonpositive_guess():
"""game.py rejects nonpositive guess"""
check50.run("python3 game.py").stdin("1", prompt=True).stdin(
"0", prompt=True
).stdout(reject_regex("Guess"), "Guess:").kill()
check50.run("python3 game.py").stdin("50", prompt=True).stdin(
"-50", prompt=True
).stdout(reject_regex("Guess"), "Guess:").kill()


@check50.check(test_valid_level)
def test_too_large():
"""game.py outputs \"Too large!\" when guess is too large"""
output = "Too large!"
check50.run("python3 testing.py").stdin("22", prompt=True).stdin("18", prompt=True).stdout(regex(output), output, regex=True).reject()
check50.run("python3 testing.py").stdin("22", prompt=True).stdin(
"18", prompt=True
).stdout(regex(output), output, regex=True).reject()


@check50.check(test_valid_level)
def test_just_right():
"""game.py outputs \"Just right!\" when guess is correct"""
output = "Just right!"
check50.run("python3 testing.py").stdin("6", prompt=True).stdin("4", prompt=True).stdout(regex(output), output, regex=True).exit()
check50.run("python3 testing.py").stdin("6", prompt=True).stdin(
"4", prompt=True
).stdout(regex(output), output, regex=True).exit()


@check50.check(test_valid_level)
def test_too_small():
"""game.py outputs \"Too small!\" when guess is too small"""
output = "Too small!"
check50.run("python3 testing.py").stdin("5", prompt=True).stdin("2", prompt=True).stdout(regex(output), output, regex=True).reject()
check50.run("python3 testing.py").stdin("5", prompt=True).stdin(
"2", prompt=True
).stdout(regex(output), output, regex=True).reject()


def regex(text):
"""match case-insensitively with any characters on either side"""
return fr'(?i)^.*{escape(text)}.*$'
return rf"(?i)^.*{escape(text)}.*$"


def reject_regex(text):
"""regex to reject if any text was printed before the expected text"""
return rf"(?i)(?<!\n){escape(text)}"
7 changes: 3 additions & 4 deletions game/testing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import random

# Monkey-patch randint, randrange, choice
random.randint = lambda x, y : 4
random.randrange = lambda x, *args, **kwargs : 4
random.choice = lambda x : 4
random.randint = lambda x, y: 4
random.randrange = lambda x, *args, **kwargs: 4
random.choice = lambda x: 4

# Run game via import
import game
Expand All @@ -12,6 +12,5 @@
try:
game.main()
except AttributeError:

# game has no main function
pass