Skip to content

Commit

Permalink
Simplify using state.report
Browse files Browse the repository at this point in the history
  • Loading branch information
hermansje committed May 10, 2019
1 parent a9e42fc commit 78746cf
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 22 deletions.
11 changes: 6 additions & 5 deletions protowhat/State.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def parse(self, text, test=True):
result = self.ast_dispatcher.parse(text)
except self.ast_dispatcher.ParseError as e:
if test:
self.report(Feedback(e.message))
self.report(e.message)
else:
raise InstructorError(
"Something went wrong when parsing PEC or solution code: %s"
Expand Down Expand Up @@ -125,10 +125,11 @@ def get_ast_path(self):
except StopIteration:
return self.ast_dispatcher.describe(self.student_ast, "{node_name}")

def report(self, feedback: Feedback):
if feedback.highlight is None and self is not getattr(self, "root_state", None):
feedback.highlight = self.student_ast
test = Fail(feedback)
def report(self, feedback: str):
test_feedback = Feedback(feedback, self)
if test_feedback.highlight is None and self is not getattr(self, "root_state", None):
test_feedback.highlight = self.student_ast
test = Fail(test_feedback)

return self.do_test(test)

Expand Down
6 changes: 3 additions & 3 deletions protowhat/checks/check_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def check_file(

p = Path(path)
if not p.exists():
state.report(Feedback(missing_msg.format(path))) # test file exists
state.report(missing_msg.format(path)) # test file exists
if p.is_dir():
state.report(Feedback(is_dir_msg.format(path))) # test its not a dir
state.report(is_dir_msg.format(path)) # test its not a dir

code = p.read_text()

Expand All @@ -41,7 +41,7 @@ def check_file(
def has_dir(state, path, incorrect_msg="Did you create a directory `{}`?"):
"""Test whether a directory exists."""
if not Path(path).is_dir():
state.report(Feedback(incorrect_msg.format(path)))
state.report(incorrect_msg.format(path))

return state

Expand Down
12 changes: 6 additions & 6 deletions protowhat/checks/check_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def check_node(
)
if _msg is None:
_msg = MSG_CHECK_FALLBACK
state.report(Feedback(_msg))
state.report(_msg)

return state.to_child(student_ast=stu_stmt, solution_ast=sol_stmt)

Expand Down Expand Up @@ -149,11 +149,11 @@ def select(node_name, node):
try:
stu_attr = select(name, state.student_ast)
except:
state.report(Feedback(_msg))
state.report(_msg)

# fail if attribute exists, but is none only for student
if stu_attr is None and sol_attr is not None:
state.report(Feedback(_msg))
state.report(_msg)

return state.to_child(student_ast=stu_attr, solution_ast=sol_attr)

Expand Down Expand Up @@ -227,7 +227,7 @@ def get_text(ast, code):
res = text in stu_text if fixed else re.search(text, stu_text)

if not res:
state.report(Feedback(_msg))
state.report(_msg)

return state

Expand Down Expand Up @@ -304,14 +304,14 @@ def get_str(ast, code, sql):
else "Something is missing.",
)
if (exact and (sol_rep != stu_rep)) or (not exact and (sol_rep not in stu_rep)):
state.report(Feedback(_msg))
state.report(_msg)

return state


def has_parsed_ast(state):
asts = [state.student_ast, state.solution_ast]
if any(isinstance(c, state.ast_dispatcher.ParseError) for c in asts):
state.report(Feedback("AST did not parse"))
state.report("AST did not parse")

return state
10 changes: 5 additions & 5 deletions protowhat/checks/check_logic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from protowhat.Feedback import Feedback
from protowhat.Test import TestFail
from protowhat.Test import TestFail, Fail
from functools import partial

from protowhat.utils import legacy_signature
Expand Down Expand Up @@ -68,7 +68,7 @@ def check_not(state, *tests, msg):
except TestFail:
# it fails, as expected, off to next one
continue
return state.report(Feedback(msg))
return state.report(msg)

# return original state, so can be chained
return state
Expand Down Expand Up @@ -111,7 +111,7 @@ def check_or(state, *tests):
if success:
return state # todo: add test

state.report(first_feedback)
state.do_test(Fail(first_feedback))


def check_correct(state, check, diagnose):
Expand Down Expand Up @@ -146,7 +146,7 @@ def check_correct(state, check, diagnose):
feedback = e.feedback

if feedback is not None:
state.report(feedback)
state.do_test(Fail(feedback))

return state # todo: add test

Expand Down Expand Up @@ -180,6 +180,6 @@ def fail(state, msg="fail"):
For example, failing a test will highlight the code as if the previous test/check had failed.
"""
_msg = state.build_message(msg)
state.report(Feedback(_msg, state))
state.report(_msg)

return state
2 changes: 1 addition & 1 deletion protowhat/checks/check_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def has_chosen(state, correct, msgs):
exec(state.student_code, globals(), ctxt)
sel_indx = ctxt["selected_option"]
if sel_indx != correct:
state.report(Feedback(msgs[sel_indx - 1]))
state.report(msgs[sel_indx - 1])
else:
state.reporter.success_msg = msgs[correct - 1]

Expand Down
2 changes: 1 addition & 1 deletion protowhat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _debug(state, msg="", on_error=False):

if not on_error:
# latest highlight added automatically
state.report(Feedback(feedback))
state.report(feedback)
else:
# debug on next failure
state.debug = True
Expand Down
2 changes: 1 addition & 1 deletion tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def child_state(state):


def fail(state):
state.report(Feedback("Fail"))
state.report("Fail")


def dummy_checks():
Expand Down

0 comments on commit 78746cf

Please sign in to comment.