Skip to content

Patch jar check #337

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

Merged
merged 4 commits into from
Jul 7, 2025
Merged
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
68 changes: 68 additions & 0 deletions jar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import check50
import re
import ast


@check50.check()
Expand Down Expand Up @@ -77,3 +78,70 @@ def test_number_functions():
raise check50.Failure(
"test_jar.py does not contain at least four valid functions"
)


@check50.check(test_student_file_passes)
def test_named_functions():
"""test_jar.py defines test_init, test_str, test_deposit, and test_withdraw"""
result = check50.run("pytest test_jar.py --collect-only -q").stdout()
collected_tests = []
for line in result.strip().split('\n'):
if '::test_' in line:
test_name = line.split('::')[1].strip()
collected_tests.append(test_name)

required_funcs = ["test_init", "test_str", "test_deposit", "test_withdraw"]
missing_funcs = []
for func in required_funcs:
if func not in collected_tests:
missing_funcs.append(func)

if missing_funcs:
raise check50.Failure(
f"Function(s) not collected by pytest in test_jar.py: {', '.join(missing_funcs)}")


@check50.check(test_student_file_passes)
def test_valid_testing():
"""test_jar.py contains implemented functions"""
with open("test_jar.py") as f:
contents = f.read()

try:
tree = ast.parse(contents)
except SyntaxError:
raise check50.Failure("test_jar.py contains syntax errors")

required_funcs = ["test_init", "test_str", "test_deposit", "test_withdraw"]
implemented_funcs = []

for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name in required_funcs:
has_test_content = False

# Walk through all nodes in the function to find test-related code
for child in ast.walk(node):
# Check for assert statements
if isinstance(child, ast.Assert):
has_test_content = True
break
# Check for pytest.raises or with statements (for context managers)
elif isinstance(child, ast.With):
has_test_content = True
break
# Check for calls to pytest functions or assert methods
elif isinstance(child, ast.Call):
if isinstance(child.func, ast.Attribute):
# Check for pytest.* or self.assert* calls
if (hasattr(child.func.value, 'id') and child.func.value.id == 'pytest') or \
child.func.attr.startswith('assert'):
has_test_content = True
break

if has_test_content:
implemented_funcs.append(node.name)

missing_or_empty = set(required_funcs) - set(implemented_funcs)
if missing_or_empty:
raise check50.Failure(
f"Missing or empty function(s) in test_jar.py: {', '.join(sorted(missing_or_empty))}")