# Last updated April 12, 2013 """ THIS IS NOT THE VERSION STUDENTS SHOULD USE! IT IS FOR TESTING THAT STUDENTS COVERED SUFFICIENT CASES IT SHOULD BE SAVED IN THE SAME LOCATION AS THE TEST_TESTS SCRIPT """ import sys, os backup_stdin = sys.stdin backup_stdout = sys.stdout class InputError(Exception): """ List given to set_input was too short. """ def __init__(self): self.val = "no elements remaining in input_list" def __str__(self): return self.val class redirect_input: """ Keyboard input is redirected from this class whenever set_input is called. """ def __init__(self, inp): self.lst = inp def readline(self): if self.lst: return self.lst.pop(0) else: raise InputError() class redirect_output: """ Screen output is redirected to this class whenever set_screen is called. """ def __init__(self): self.screen = "" def __str__(self): return self.screen def __nonzero__(self): return bool(self.screen) def write(self, string): self.screen += string def reset(self): self.screen = "" # Same constants as regular check in case students (incorrectly) reference them expected_screen = "" actual_screen = redirect_output() test_output = redirect_output() input_list = [] file_list = [] dir_list = [] # Needs to be defined to avoid NameError, but has no effect on test_tests def set_screen(string): pass # Doesn't add newline characters to each element of input_list, nor does it # have any effect on sys.stdout or sys.stdin def set_input(lst): global input_list if type(lst) != list: raise TypeError("set_input must consume a list") for i in lst: if type(i) != str: raise TypeError("all elements of input_list must be strings") input_list = lst # Needs to be defined to avoid NameError, but has no effect on test_tests def set_file(resulting_file, expected_file): pass # Needs to be defined to avoid NameError, but has no effect on test_tests def set_file_exact(resulting_file, expected_file): pass # Wrapper function, just like in normal check def expect(label, function_call, expected_value): run_test(label, function_call, expected_value, None) # Wrapper function, just like in normal check def within(label, function_call, expected_value, acceptable_tolerance): run_test(label, function_call, expected_value, acceptable_tolerance) # Simply writes def run_test(label, result, expected, tolerance): global input_list if input_list: result.append(input_list) input_list = [] f = open('/u/cs116/marking/test_tests/cases.txt', 'a') if type(result) == list: # if type != list then this is testing a state variable, not a function call # so this case can be ignored by test_tests f.write(str(result+[expected])+'\n') f.close() # Needs to be defined to avoid NameError, but has no effect on test_tests def approx(result, expected, tolerance): pass # Needs to be defined to avoid NameError, but has no effect on test_tests def compare_files(label, new_files, fname1, fname2, exact): pass