From f2754943c38aca6bfe438769114339b23521e7eb Mon Sep 17 00:00:00 2001 From: ahmedsamyh Date: Sat, 16 Nov 2024 14:41:26 +0500 Subject: [PATCH] Implement testing program in python (test.py) --- test.py | 55 ++++++++++++++++++++++++++ tests/01-unterminated-string.momo.test | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..a8a78d1 --- /dev/null +++ b/test.py @@ -0,0 +1,55 @@ +import os +import sys +import subprocess + +TEST_DIR = "./tests/" +TEST_FILE_SUFFIX = ".test" + +def error(msg: str): + print(f"ERROR: {msg}", file=sys.stderr) + + +def cmd(args: [str], echo=False) -> subprocess.CompletedProcess: + if echo: + print(f"CMD: \"", end='') + for i in range(len(args)): + print(f"{args[i]}", end='') + if i < len(args)-1: + print(" ", end='') + + print("\"") + return subprocess.run(args, capture_output=True, text=True) + + +def test_source_file(filename: str): + if not os.path.exists(filename): + error(f"File {filename} doesn't exist!") + exit(1) + + # TODO: option to record/update test file + testfilename: str = filename + TEST_FILE_SUFFIX + if not os.path.exists(testfilename): + error("Test file {testfilename} doesn't exist!") + exit(1) + + p = cmd(["python", "./main.py", filename]) + + output = p.stdout + + f = open(testfilename, 'r') + expected_output = f.read() + f.close() + + if output != expected_output: + print("Failed!") + print(f"Expected: `{expected_output}`") + print(f"Got: `{output}`") + else: + print("Success!") + +def main(): + test_source_file("./tests/01-unterminated-string.momo") + + +if __name__ == '__main__': + main() diff --git a/tests/01-unterminated-string.momo.test b/tests/01-unterminated-string.momo.test index 4259fc6..88b52ed 100644 --- a/tests/01-unterminated-string.momo.test +++ b/tests/01-unterminated-string.momo.test @@ -1 +1 @@ -'[ERROR] tests/01-unterminated-string.momo:1:21: Unterminated string!' +'[ERROR] ./tests/01-unterminated-string.momo:1:21: Unterminated string!' -- 2.39.5