Skip to content
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
7 changes: 6 additions & 1 deletion pythonwhat/check_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,13 @@ def has_equal_ast(incorrect_msg="FMT: Your code does not seem to match the solut
"""
rep = Reporter.active_reporter

parse_tree = lambda n: ast.dump(n.body[0] if isinstance(n, ast.Module) and len(n.body) == 1 else n)
def parse_tree(n):
# get contents of module.body if only 1 element
crnt = n.body[0] if isinstance(n, ast.Module) and len(n.body) == 1 else n

# remove Expr if it exists
return ast.dump(crnt.value if isinstance(crnt, ast.Expr) else crnt)

stu_rep = parse_tree(state.student_tree)
sol_rep = parse_tree(state.solution_tree if not code else ast.parse(code))

Expand Down
9 changes: 9 additions & 0 deletions tests/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ def test_exact_false_fail(self):
self.data["DC_SCT"] = "Ex().has_equal_ast(exact=False)"
self.failing_submission()

def test_part_of_method_pass(self):
self.data["DC_SCT"] = """Ex().has_equal_ast(code = 'dict(a = "a")', exact=False)"""
sct_payload = helper.run(self.data)
self.assertTrue(sct_payload['correct'])

def test_part_of_method_fail(self):
self.data["DC_SCT"] = """Ex().has_equal_ast(code = 'dict(a = "a")', exact=False)"""
self.failing_submission()

class TestOverride(unittest.TestCase):
"""
This class is used to test overriding w/ correct and incorrect code. Tests are
Expand Down