18
18
# Module: TestFramework
19
19
# Author: Dilshan Angampitiya
20
# Steven Bird (revisions)
22
23
# Brief description of the Module# define custom exceptions
23
24
# use exceptions for all errors found in testing
25
26
import sys, StringIO, copy
27
29
# student error or author error
28
30
# errors in student code get handled internally
157
159
exec "__f__ = %s" %function in included_code
159
raise TestCreationError("Invalid function %s" %function)
161
raise TestCreationError("Invalid function %s" % function)
161
163
f = included_code['__f__']
163
165
if not callable(f):
164
raise TestCreationError("Invalid function %s" %function)
166
raise TestCreationError("Invalid function %s" % function)
170
172
def validate_functions(self, included_code):
171
173
"""Ensure all functions used by the test cases exist and are callable.
172
Also covert their string representations to function objects.
174
Also convert their string representations to function objects.
173
175
This can only be done once all the include code has been specified.
175
177
(test_type, function) = self._stdout_test
215
217
"""Compare solution output and attempt output using the
216
218
specified comparison function.
218
# converts unicode to string
219
if type(solution_output) == unicode:
220
solution_output = str(solution_output)
222
if type(attempt_output) == unicode:
223
attempt_output = str(attempt_output)
220
solution_output = str(solution_output)
221
attempt_output = str(attempt_output)
225
223
if test_type == 'norm':
226
224
return f(solution_output) == f(attempt_output)
231
229
"""Compare solution code and attempt code using the
232
230
specified comparison function.
235
raise TestCreationError("Invalid function %s" %f)
232
if type(f) in types.StringTypes: # kludge
236
234
if test_type == 'norm':
237
235
return f(solution) == f(attempt)
251
249
# check function return value (None for scripts)
252
250
(test_type, f) = self._result_test
253
if not self._check_output(solution_data['result'], attempt_data['result'], test_type, f):
251
if not self._check_output(solution_data['result'], attempt_data['result'], test_type, f):
254
252
return 'Unexpected function return value'
257
255
(test_type, f) = self._stdout_test
258
if not self._check_output(solution_data['stdout'], attempt_data['stdout'], test_type, f):
256
if not self._check_output(solution_data['stdout'], attempt_data['stdout'], test_type, f):
259
257
return 'Unexpected output'
262
260
(test_type, f) = self._stderr_test
263
if not self._check_output(solution_data['stderr'], attempt_data['stderr'], test_type, f):
261
if not self._check_output(solution_data['stderr'], attempt_data['stderr'], test_type, f):
264
262
return 'Unexpected error output'
267
265
(test_type, f) = self._exception_test
268
if not self._check_output(solution_data['exception'], attempt_data['exception'], test_type, f):
266
if not self._check_output(solution_data['exception'], attempt_data['exception'], test_type, f):
269
267
return 'Unexpected exception'
271
269
solution_files = solution_data['modified_files']
338
336
def add_variable(self, variable, value):
339
337
""" Add the given varibale-value pair to the initial global environment
340
338
for this test case.
341
Throw and exception if thevalue cannot be paresed.
339
Throw and exception if the value cannot be paresed.