Skip to content

Commit cd56b8c

Browse files
committed
Implement force_diagnose in test_exercise and check_correct
1 parent fa0577b commit cd56b8c

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

pythonwhat/State.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self,
4949
student_parts=None, solution_parts=None,
5050
highlight = None,
5151
highlighting_disabled = None, messages=None,
52+
force_diagnose = False,
5253
**kwargs):
5354

5455
# Set basic fields from kwargs
@@ -57,6 +58,7 @@ def __init__(self,
5758
self.student_parts = student_parts
5859
self.solution_parts = solution_parts
5960
self.messages = messages if messages else []
61+
self.force_diagnose = force_diagnose
6062

6163
# parse code if didn't happen yet
6264
if not hasattr(self, 'student_tree'):
@@ -194,7 +196,8 @@ def to_child_state(self, student_subtree=None, solution_subtree=None,
194196
highlight = highlight,
195197
highlighting_disabled = highlighting_disabled,
196198
messages = messages,
197-
parent_state = self)
199+
parent_state = self,
200+
force_diagnose=self.force_diagnose)
198201
return(child)
199202

200203
def update(self, **kwargs):

pythonwhat/check_logic.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,21 @@ def check_correct(check, diagnose, state=None):
137137
)
138138
139139
"""
140-
def diagnose_and_check(state=None):
141-
# use multi twice, since diagnose and check may be lists of tests
142-
multi(diagnose, state=state)
140+
feedback = None
141+
try:
143142
multi(check, state=state)
143+
except TestFail as e:
144+
feedback = e.feedback
145+
146+
try:
147+
multi(diagnose, state=state)
148+
except TestFail as e:
149+
if feedback is not None or state.force_diagnose:
150+
feedback = e.feedback
144151

145-
check_or(diagnose_and_check, check, state=state)
152+
if feedback is not None:
153+
rep = Reporter.active_reporter
154+
rep.do_test(Test(feedback))
146155

147156
# utility functions -----------------------------------------------------------
148157

pythonwhat/test_exercise.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def test_exercise(sct,
1414
solution_process,
1515
raw_student_output,
1616
ex_type,
17-
error):
17+
error,
18+
force_diagnose=False):
1819
"""
1920
Point of interaction with the Python backend.
2021
Args:
@@ -42,7 +43,8 @@ def test_exercise(sct,
4243
pre_exercise_code = check_str(pre_exercise_code),
4344
student_process = check_process(student_process),
4445
solution_process = check_process(solution_process),
45-
raw_student_output = check_str(raw_student_output)
46+
raw_student_output = check_str(raw_student_output),
47+
force_diagnose = force_diagnose
4648
)
4749

4850
State.root_state = state

tests/helper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def run(data, run_code = True):
1616
stu_code = data.get("DC_CODE", "")
1717
sol_code = data.get("DC_SOLUTION", "")
1818
sct = data.get("DC_SCT", "")
19+
force_diagnose = data.get("DC_FORCE_DIAGNOSE", False)
1920

2021
class ChDir(object):
2122
"""
@@ -61,6 +62,7 @@ def __exit__(self, *args):
6162
solution_process=sol_process,
6263
raw_student_output = raw_stu_output,
6364
ex_type = "NormalExercise",
65+
force_diagnose = force_diagnose,
6466
error = error)
6567

6668
return res

tests/test_check_logic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ def test_check_correct(sct, passes, msg):
9494
assert output['correct'] == passes
9595
if msg: assert output['message'] == msg
9696

97+
@pytest.mark.parametrize('sct, passes, msg', [
98+
("Ex().check_correct(has_code('a'), has_code('b'))", False, None),
99+
("Ex().check_correct(has_code('a'), has_code('c'))", False, None),
100+
("Ex().check_correct(has_code('b'), has_code('c', not_typed_msg='x'))", False, 'x'),
101+
("Ex().check_correct(has_code('b', not_typed_msg='x'), has_code('a'))", False, 'x')
102+
])
103+
def test_check_correct_force_diagnose(sct, passes, msg):
104+
data = {
105+
'DC_CODE': "'a'",
106+
'DC_SCT': sct,
107+
'DC_FORCE_DIAGNOSE': True
108+
}
109+
output = helper.run(data)
110+
assert output['correct'] == passes
111+
if msg: assert output['message'] == msg
112+
97113
@pytest.mark.parametrize('sct, passes, msg', [
98114
("test_correct(lambda: test_student_typed('a'), lambda: test_student_typed('b'))", True, None),
99115
("test_correct(test_student_typed('a'), test_student_typed('b'))", True, None),

0 commit comments

Comments
 (0)