]> www.git.momoyon.org Git - lang.git/commitdiff
Implement testing program in python (test.py)
authorahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 09:41:26 +0000 (14:41 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 09:41:26 +0000 (14:41 +0500)
test.py [new file with mode: 0644]
tests/01-unterminated-string.momo.test

diff --git a/test.py b/test.py
new file mode 100644 (file)
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()
index 4259fc65e6007304325c927711126faa00619963..88b52ed3b4e85c63f0a7d06c1798084d04e419a9 100644 (file)
@@ -1 +1 @@
-'[ERROR] tests/01-unterminated-string.momo:1:21: Unterminated string!'
+'[ERROR] ./tests/01-unterminated-string.momo:1:21: Unterminated string!'