|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import io |
| 5 | +import re |
| 6 | +import sys |
| 7 | +import json |
| 8 | +import filecmp |
| 9 | +import subprocess |
| 10 | + |
| 11 | +from collections import namedtuple as nt |
| 12 | + |
| 13 | + |
| 14 | +class bcolors: |
| 15 | + OKBLUE = '\033[94m' |
| 16 | + OKGREEN = '\033[92m' |
| 17 | + WARNING = '\033[93m' |
| 18 | + FAIL = '\033[91m' |
| 19 | + ENDC = '\033[0m' |
| 20 | + |
| 21 | + |
| 22 | +def print_green(text): |
| 23 | + print(f"{bcolors.OKGREEN}{text}{bcolors.ENDC}") |
| 24 | + |
| 25 | + |
| 26 | +def print_red(text): |
| 27 | + print(f"{bcolors.FAIL}{text}{bcolors.ENDC}") |
| 28 | + |
| 29 | + |
| 30 | +Test = nt("Test", "process output expected name") |
| 31 | + |
| 32 | + |
| 33 | +def check_result(test): |
| 34 | + out = test.output.decode("ascii").strip() |
| 35 | + if test.expected == out: |
| 36 | + print_green(f"Test: {test.name} is [OK]") |
| 37 | + else: |
| 38 | + print_red(f"Test: {test.name} is [FAIL]") |
| 39 | + print_green(test.expected) |
| 40 | + print_red(out) |
| 41 | + print("-" * 10) |
| 42 | + |
| 43 | + |
| 44 | +def run_test(program_path, test_path): |
| 45 | + with open(test_path, "r") as test: |
| 46 | + test_case = json.load(test) |
| 47 | + p = subprocess.Popen(["python3", program_path + "/" + program_path.split("/")[-1] + ".py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 48 | + outp, errs = p.communicate(str(test_case["input"]).encode("ascii")) |
| 49 | + if errs: |
| 50 | + print_red("[FAIL]") |
| 51 | + print(errs) |
| 52 | + return |
| 53 | + check_result(Test(p, outp, test_case["output"], test_path.split("/")[-1])) |
| 54 | + |
| 55 | + |
| 56 | +def main(argv): |
| 57 | + """ |
| 58 | + Assuming each program is in it's own folder containing "test" folder inside |
| 59 | + Each test is a json file with "input" and "output" string keys |
| 60 | + """ |
| 61 | + cwd = os.getcwd() |
| 62 | + program_path = os.path.join(cwd, argv[1]) |
| 63 | + for test_path in os.listdir(os.path.join(program_path, "test")): |
| 64 | + if not re.match(test_path, "_res$") and not re.match(test_path, "_test_res$"): |
| 65 | + full_path = os.path.join(program_path, "test", test_path) |
| 66 | + run_test(program_path, full_path) |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + if len(sys.argv) < 2: |
| 71 | + print("Please select the program") |
| 72 | + exit(-1) |
| 73 | + main(sys.argv) |
0 commit comments