Skip to content

Commit cd345a6

Browse files
authored
Merge pull request #199 from datacamp/cleanup-reporter
cleanup reporter
2 parents 79061ee + 8b40dd7 commit cd345a6

26 files changed

Lines changed: 17 additions & 101 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ before_install:
88
- "git clone https://$GH_TOKEN@github.com/datacamp/pythonbackend.git && pip install ./pythonbackend && rm -rf pythonbackend"
99
install: pip install -r requirements.txt
1010

11-
script: pytest tests -s -m "not dep_matplotlib"
11+
script: pytest
1212
after_script: "export PYTHONWHAT_DEBUG_FEEDBACK='True' && pytest tests -s -m 'feedback'"

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
testpaths = tests/
3+
addopts =-s -m "not dep_matplotlib"

pythonwhat/Reporter.py

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,8 @@ def __init__(self):
2020
self.feedback = Feedback("Oh no, your solution is incorrect! Please, try again.")
2121
self.success_msg = "Great work!"
2222
self.errors_allowed = False
23-
self.tags = {}
2423
self.failure_msg = ""
2524
self.fallback_ast = None
26-
self.test_stack = []
27-
self.test_mode = None
28-
29-
def set_success_msg(self, success_msg):
30-
self.success_msg = success_msg
31-
32-
def allow_errors(self):
33-
self.errors_allowed = True
34-
35-
def reject_errors(self):
36-
self.errors_allowed = False
37-
38-
def fail(self, failure_msg):
39-
self.failed_test = True
40-
self.feedback = Feedback(failure_msg)
4125

4226
def do_test(self, testobj, prepend_on_fail="", fallback_ast=None):
4327
"""Do test.
@@ -46,16 +30,9 @@ def do_test(self, testobj, prepend_on_fail="", fallback_ast=None):
4630
the state of the reporter changes and the feedback is kept.
4731
"""
4832

49-
if self.test_mode is 'or':
50-
return self.test_stack.append([testobj, prepend_on_fail, fallback_ast])
51-
5233
if prepend_on_fail: self.failure_msg = prepend_on_fail
5334
if fallback_ast: self.fallback_ast = fallback_ast
5435

55-
if self.failed_test:
56-
self.feedback.message = self.failure_msg + self.feedback.message
57-
raise TestFail
58-
return
5936
if isinstance(testobj, Test):
6037
testobj.test()
6138
result = testobj.result
@@ -74,72 +51,35 @@ def do_test(self, testobj, prepend_on_fail="", fallback_ast=None):
7451
#self.failure_msg_stack.pop()
7552
return result
7653

77-
def do_tests(self, testobjs):
78-
"""Do multiple tests.
79-
80-
Execute an array of tests.
81-
"""
82-
for testobj in testobjs:
83-
if self.failed_test:
84-
break
85-
86-
self.do_test(testobj)
87-
88-
def start_or_test(self):
89-
self.test_mode = 'or'
90-
self.test_stack = []
91-
92-
def end_or_test(self):
93-
self.test_mode = None
94-
first_message = None
95-
success = False
96-
for sct_args in self.test_stack:
97-
try:
98-
self.do_test(*sct_args)
99-
success = True
100-
except TestFail as e:
101-
if not first_message: first_message = self.feedback.message
102-
self.failed_test = False
103-
104-
if success: return
105-
106-
self.failed_test = True
107-
self.feedback.message = first_message
108-
raise TestFail
109-
110-
def set_tag(self, key, value):
111-
self.tags[key] = value
112-
113-
11454
def build_payload(self, error):
11555
if (error and not self.failed_test and not self.errors_allowed):
11656
feedback_msg = "Your code contains an error: `%s`" % str(error[1])
117-
return({
57+
return {
11858
"correct": False,
119-
"message": Reporter.to_html(feedback_msg),
120-
"tags": {"fun": "runtime_error"}})
59+
"message": Reporter.to_html(feedback_msg)
60+
}
12161

12262
if self.failed_test:
12363
if not self.feedback.line_info:
124-
return({
64+
return {
12565
"correct": False,
126-
"message": Reporter.to_html(self.feedback.message),
127-
"tags": self.tags})
66+
"message": Reporter.to_html(self.feedback.message)
67+
}
12868
else:
12969
# Hack to make it work with campus app implementation
13070
if self.feedback.line_info["column_start"] is None:
13171
col_start = None
13272
else:
13373
col_start = self.feedback.line_info["column_start"] + 1
13474

135-
return({
75+
return {
13676
"correct": False,
13777
"message": Reporter.to_html(self.feedback.message),
13878
"line_start": self.feedback.line_info["line_start"],
13979
"column_start": col_start,
14080
"line_end": self.feedback.line_info["line_end"],
141-
"column_end": self.feedback.line_info["column_end"],
142-
"tags": self.tags})
81+
"column_end": self.feedback.line_info["column_end"]
82+
}
14383

14484

14585
else:

pythonwhat/State.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,12 @@ def parse_ext(x):
232232
utils_ast.mark_text_ranges(res, x + '\n')
233233

234234
except IndentationError as e:
235-
rep.set_tag("fun", "indentation_error")
236235
e.filename = "script.py"
237236
# no line info for now
238237
rep.feedback = Feedback("Your code could not be parsed due to an error in the indentation:<br>`%s.`" % str(e))
239238
rep.failed_test = True
240239

241240
except SyntaxError as e:
242-
rep.set_tag("fun", "syntax_error")
243241
e.filename = "script.py"
244242
# no line info for now
245243
rep.feedback = Feedback("Your code can not be executed due to a syntax error:<br>`%s.`" % str(e))
@@ -248,7 +246,6 @@ def parse_ext(x):
248246
# Can happen, can't catch this earlier because we can't differentiate between
249247
# TypeError in parsing or TypeError within code (at runtime).
250248
except:
251-
rep.set_tag("fun", "other_error")
252249
rep.feedback.message = "Something went wrong while parsing your code."
253250
rep.failed_test = True
254251

pythonwhat/test_exercise.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def test_exercise(sct,
6565
sct_cntxt.update(spec_2_context)
6666

6767
try:
68+
# if statement detects, for example, if State hit a parsing error w/student code
6869
if not rep.failed_test:
6970
exec(sct, sct_cntxt) # Spec v2 tests run immediately
7071
for test in tree.crnt_node: # Spec v1 tests run after
@@ -81,11 +82,11 @@ def success_msg(message):
8182
message (str): A string containing the feedback message.
8283
"""
8384
rep = Reporter.active_reporter
84-
rep.set_success_msg(message)
85+
rep.success_msg = message
8586

8687

8788
def allow_errors():
8889
rep = Reporter.active_reporter
89-
rep.allow_errors()
90+
rep.errors_allowed = True
9091

9192
cntxt['success_msg'] = success_msg

pythonwhat/test_funcs/test_comp.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def test_list_comp(index=1,
2727
"""
2828

2929
rep = Reporter.active_reporter
30-
rep.set_tag("fun", "test_list_comp")
3130

3231
test_comp("list comprehension", 'list_comps', **(locals()))
3332

@@ -44,7 +43,6 @@ def test_generator_exp(index=1,
4443
"""Test generator expressions
4544
"""
4645
rep = Reporter.active_reporter
47-
rep.set_tag("fun", "test_generator_exp")
4846

4947
test_comp("generator expression", 'generator_exps', **(locals()))
5048

@@ -63,7 +61,6 @@ def test_dict_comp(index=1,
6361
"""Test dict comprehension.
6462
"""
6563
rep = Reporter.active_reporter
66-
rep.set_tag("fun", "test_dict_comp")
6764

6865
test_comp("dictionary comprehension", 'dict_comps', **(locals()))
6966

pythonwhat/test_funcs/test_correct.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def test_correct(check, diagnose, state=None):
99
1010
"""
1111
rep = Reporter.active_reporter
12-
rep.set_tag("fun", "test_correct")
1312

1413
def diagnose_and_check(state=None):
1514
# use multi twice, since diagnose and check may be lists of tests

pythonwhat/test_funcs/test_data_frame.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def test_data_frame(name,
2020
"""
2121

2222
rep = Reporter.active_reporter
23-
rep.set_tag("fun", "test_data_frame")
2423

2524
child = check_object(name, undefined_msg or MSG_UNDEFINED, expand_msg="", state=state, typestr="pandas DataFrame")
2625
is_instance(pd.DataFrame, not_data_frame_msg or MSG_NOT_INSTANCE, state=child) # test instance

pythonwhat/test_funcs/test_dictionary.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def test_dictionary(name,
1818
"""
1919

2020
rep = Reporter.active_reporter
21-
rep.set_tag("fun", "test_dictionary")
2221

2322
child = check_object(name, undefined_msg or MSG_UNDEFINED, expand_msg = "", state=state, typestr="dictionary")
2423
is_instance(dict, not_dictionary_msg or MSG_NOT_INSTANCE, state=child) # test instance

pythonwhat/test_funcs/test_for_loop.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def test_for_loop(index=1,
6262
:code:`test_exression_output()` will pass on the body code.
6363
"""
6464
rep = Reporter.active_reporter
65-
rep.set_tag("fun", "test_for_loop")
6665

6766
state = check_node('for_loops', index-1, "`for` loops", MSG_MISSING, MSG_PREPEND, state=state)
6867

0 commit comments

Comments
 (0)