226
226
return f(solution_output, attempt_output)
228
def _check_code(self, solution, attempt, test_type, f):
228
def _check_code(self, solution, attempt, test_type, f, include_space):
229
229
"""Compare solution code and attempt code using the
230
230
specified comparison function.
232
232
if type(f) in types.StringTypes: # kludge
233
f = eval(str(f), include_space)
234
234
if test_type == 'norm':
235
235
return f(solution) == f(attempt)
237
237
return f(solution, attempt)
239
def run(self, solution_data, attempt_data):
239
def run(self, solution_data, attempt_data, include_space):
240
240
"""Run the tests to compare the solution and attempt data
241
241
Returns the empty string if the test passes, or else an error message.
244
244
# check source code itself
245
245
(test_type, f) = self._code_test
246
if not self._check_code(solution_data['code'], attempt_data['code'], test_type, f):
246
if not self._check_code(solution_data['code'], attempt_data['code'], test_type, f, include_space):
247
247
return 'Unexpected code'
249
249
# check function return value (None for scripts)
374
374
""" Get the name of the test case """
375
375
return self._name
377
def run(self, solution, attempt_code, stop_on_fail=True):
377
def run(self, solution, attempt_code, include_space, stop_on_fail=True):
378
378
""" Run the solution and the attempt with the inputs specified for this test case.
379
379
Then pass the outputs to each test part and collate the results.
416
416
# generate results
417
417
for test_part in self._parts:
419
result = test_part.run(solution_data, attempt_data)
419
result = test_part.run(solution_data, attempt_data, include_space)
421
421
raise TestError(sys.exc_info())
554
554
test_case_results = []
556
556
for test in self._tests:
557
result_dict = test.run(self._solution, attempt_code)
557
result_dict = test.run(self._solution, attempt_code, self._include_space)
558
558
if 'exception' in result_dict and result_dict['exception']['critical']:
559
559
# critical error occured, running more cases is useless
560
560
# FunctionNotFound, Syntax, Indentation