""" debug_lib A bunch of helpful debuging functions Cal.W 2019 """ class auto_input: """Given a list automagicly enter a value.""" def __init__(self, input_list): #Just setup some internal stuff self.input_index = 0 self.input_list = input_list def __call__(self, input_text): #TLDR: Get the next value from the array and return that # If there was an error then ask the user for input try: out = self.input_list[self.input_index] except: out = None self.input_index += 1 if out is None: out = __builtins__['input']('#Input#: '+input_text) else: print(str(input_text)+str(out)) return str(out) def stringCompairTests(test_function, tests, doPrint=True, logFile=None): """Run a bunch of tests on a function and check the outputs via comparing them to wanted outputs as strings. Prams: test_function: function - This needs to be the function to test tests: list - The list of tests to run. Each test is a list in the form: [(arg1, arg2, ...), expected_output] Note: The number of arguments in the tuple *must* equal the number required for the function doPrint: bool - Print the test results - Default: True logFile: str - The path to write the output as a log if it is NoneType(None) then it won't make a logfile - Default: None Returns: list - A list containing each test result as a tuple in the form: (returned_result, Bool(test_succeeded), test) Example: def doThing(arg1, arg2): return arg1 + arg2 tests = [ [(1, 2), 3], [("Hello", "World"), "HelloWorld"], [(1, 3), 2] ] test_results = stringCompairTests(doThing, tests) >>>test_results => [ (3, True, [(1, 2), 3]), ("HelloWorld", True, [("Hello", "World"), "HelloWorld"]), (4, False, [(1, 3), 2]) ] """ test_results = [] logString = "" for i, test in enumerate(tests): result = test_function(**test[0]) if type(test[0]) is dict else test_function(*test[0]) test_results.append((result, str(test[1]) == str(result), test)) logStringL = "-"*50 logStringL += "\n"+ "Test: " + str(i) + (" - Success" if result else " - Failure") logStringL += "\n"+ "Given Arguments: \"" + '", "'.join(test[0]) + '"' logStringL += "\n"+ "Expected Output: " + str(test[1]) logStringL += "\n"+ "Returned Output: " + str(result) logStringL += "\n"+ "Returned Values " + ("don't" if not result else "") + "match." logString += '\n' + logStringL if doPrint: print(logStringL) successfull_tests = sum([1 for x in test_results if x[1]]) logStringL = "\n" + str(successfull_tests) + " test" + ("s" if len(test_results)-successfull_tests == 1 else '') logStringL += " succeeded with " + str(len(test_results)-successfull_tests) + " test" logStringL += ("s" if len(test_results)-successfull_tests == 1 else "") + " failing." logString += '\n' + logStringL if doPrint: print(logStringL,"\n") if logFile not in (None, ""): if type(logFile) is bool and logFile: logFile = __file__.rsplit('\\', 1)[1] if logString[0] == "\n": logString = logString[1:] with open(str(logFile)+".log", 'w') as writeFile: writeFile.write(logString) writeFile.close() if doPrint: print("The logfile:", '\''+str(logFile)+".log'", "has been written to.", "\n") return test_results