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

issue238 fix #295

Merged
merged 1 commit into from
Jan 26, 2025
Merged
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
45 changes: 34 additions & 11 deletions professor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,35 @@ def test_level_high():
"""Little Professor rejects level of 4"""
check50.run("python3 testing.py get_level").stdin("4", prompt=False).reject()


@check50.check(exists)
def test_level_nonint():
"""Little Professor rejects level of \"one\""""
"""Little Professor rejects level of \"one\" """
check50.run("python3 testing.py get_level").stdin("one", prompt=False).reject()


@check50.check(exists)
def test_valid_level():
"""Little Professor accepts valid level"""

# Test all levels 1–3
for level in range(1, 4):
check50.run("python3 testing.py get_level").stdin(str(level), prompt=False).exit(0)
check50.run("python3 testing.py get_level").stdin(
str(level), prompt=False
).exit(0)


@check50.check(test_valid_level)
def test_random():
"""Little Professor generates random numbers correctly"""
level = "1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that some students might use random.randrange for levels 2 and 3 but not for level 1, and check50 would not be able to detect it. However, the likelihood of such a scenario is so low that it may not be worth the effort to check all levels.


# With random.seed(500) in testing.py, we expect these numbers from .randint(0, 9);
# this should catch students if they're using .randrange(0, 9) by accident
output = "[7, 8, 9, 7, 4, 6, 3, 1, 5, 9, 1, 0, 3, 5, 3, 6, 4, 0, 1, 5]"
check50.run("python3 testing.py rand_test").stdin(level, prompt=False).stdout(
regex(output), output, regex=True
)


@check50.check(test_valid_level)
Expand All @@ -43,7 +58,9 @@ def test_range_level_1():

# With random.seed(0) in testing.py, 6 + 6 is expected output from randint and randrange with range of 0–9
output = "6 + 6 ="
check50.run("python3 testing.py main").stdin(level, prompt=False).stdout(regex(output), output, regex=True)
check50.run("python3 testing.py main").stdin(level, prompt=False).stdout(
regex(output), output, regex=True
)


@check50.check(test_valid_level)
Expand All @@ -53,7 +70,9 @@ def test_range_level_2():

# With random.seed(0) in testing.py, 59 + 63 is expected output from randint and randrange with range of 10–99
output = "59 + 63 ="
check50.run("python3 testing.py main").stdin(level, prompt=False).stdout(regex(output), output, regex=True)
check50.run("python3 testing.py main").stdin(level, prompt=False).stdout(
regex(output), output, regex=True
)


@check50.check(test_valid_level)
Expand All @@ -63,7 +82,9 @@ def test_range_level_3():

# With random.seed(0) in testing.py, 964 + 494 is expected output from randint and randrange with range of 100–999
output = "964 + 494 ="
check50.run("python3 testing.py main").stdin(level, prompt=False).stdout(regex(output), output, regex=True)
check50.run("python3 testing.py main").stdin(level, prompt=False).stdout(
regex(output), output, regex=True
)


@check50.check(test_range_level_1)
Expand All @@ -90,7 +111,9 @@ def test_score():
@check50.check(test_generate_problems)
def test_EEE():
"""Little Professor displays EEE when answer is incorrect"""
check50.run("python3 testing.py main").stdin("1", prompt=False).stdin("0", prompt=False).stdout(regex("EEE"), "EEE", regex=True)
check50.run("python3 testing.py main").stdin("1", prompt=False).stdin(
"0", prompt=False
).stdout(regex("EEE"), "EEE", regex=True)


@check50.check(test_generate_problems)
Expand All @@ -104,9 +127,9 @@ def test_show_solution():

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


def score_regex(score):
"""match case-insensitively with only final printing of score"""
return fr'(?i)[^\d+=]*{score}[^\d+=]*$'
return rf"(?i)[^\d+=]*{score}[^\d+=]*$"
6 changes: 5 additions & 1 deletion professor/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
import random
import sys


def main():
argument = sys.argv[1]
if argument == "get_level":
professor.get_level()
elif argument == "main":
random.seed(0)
professor.main()
elif argument == "rand_test":
random.seed(500)
print([professor.generate_integer(1) for _ in range(20)])



main()