Skip to content

Commit 5608cac

Browse files
jameselitetahadostifam
authored andcommitted
added testing.
1 parent 7dba954 commit 5608cac

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

test/test_suite.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import sys
2+
from pathlib import Path
3+
import re
4+
import os
5+
import subprocess
6+
import shlex
7+
8+
def build_and_run(file_path, metadata, compiler_path, output_dir):
9+
output_binary = Path(output_dir) / file_path.stem
10+
11+
build_result = subprocess.run([
12+
compiler_path, "build", str(file_path), "-o", str(output_binary),
13+
"--reloc-mode=pic"
14+
], capture_output=True, text=True)
15+
16+
if build_result.returncode != 0:
17+
raise Exception(f"Build error:\n{build_result.stderr}")
18+
19+
args = shlex.split(metadata["args"] or "")
20+
run_cmd = [str(output_binary)] + args
21+
22+
run_result = subprocess.run(run_cmd, input=metadata["stdin"] or "", capture_output=True, text=True)
23+
24+
actual_stdout = run_result.stdout.strip()
25+
expected_stdout = (metadata["stdout"] or "").strip()
26+
27+
if actual_stdout != expected_stdout:
28+
raise Exception(
29+
f"Expected:\n {expected_stdout}\n\nGot:\n {actual_stdout}"
30+
)
31+
32+
def extract_test_metadata(content, file_name):
33+
metadata = {
34+
"stdout": None,
35+
"stdin": None,
36+
"args": None
37+
}
38+
39+
stdout_match = re.search(r"//\s*@stdout:\s*(.*)", content)
40+
if stdout_match:
41+
metadata["stdout"] = stdout_match.group(1)
42+
else:
43+
raise Exception(f"Missing required @stdout directive.")
44+
45+
stdin_match = re.search(r"//\s*@stdin:\s*(.*)", content)
46+
if stdin_match:
47+
metadata["stdin"] = stdin_match.group(1)
48+
49+
args_match = re.search(r"//\s*@args:\s*(.*)", content)
50+
if args_match:
51+
metadata["args"] = args_match.group(1)
52+
53+
return metadata
54+
55+
def main():
56+
try:
57+
if len(sys.argv) < 5 or sys.argv[1] not in ("-d", "--directory"):
58+
raise Exception("Usage: main.py -d <test_dir> [--compiler <compiler_path>] --output <output_dir>")
59+
60+
directory = sys.argv[2]
61+
output_dir = None
62+
compiler_path = "cyrus"
63+
64+
i = 3
65+
while i < len(sys.argv):
66+
if sys.argv[i] == "--compiler":
67+
if i + 1 >= len(sys.argv):
68+
raise Exception("Missing compiler path after --compiler")
69+
compiler_path = sys.argv[i + 1]
70+
i += 2
71+
elif sys.argv[i] == "--output":
72+
if i + 1 >= len(sys.argv):
73+
raise Exception("Missing output directory after --output")
74+
output_dir = sys.argv[i + 1]
75+
i += 2
76+
else:
77+
raise Exception(f"Unknown argument: {sys.argv[i]}")
78+
79+
if not output_dir:
80+
raise Exception("--output is required")
81+
82+
tests_path = Path(directory)
83+
if not tests_path.exists() or not tests_path.is_dir():
84+
raise Exception(f"Provided test directory '{directory}' is invalid.")
85+
86+
output_path = Path(output_dir)
87+
output_path.mkdir(parents=True, exist_ok=True)
88+
89+
passed_tests = []
90+
failed_tests = []
91+
92+
for test_file in tests_path.glob("*.cyr"):
93+
if test_file.is_file():
94+
try:
95+
with open(test_file, "r") as file:
96+
content = file.read()
97+
metadata = extract_test_metadata(content, test_file.name)
98+
build_and_run(test_file, metadata, compiler_path, output_path)
99+
passed_tests.append(test_file.name)
100+
except Exception as e:
101+
failed_tests.append((test_file.name, str(e)))
102+
103+
print("\n=== Test Summary ===")
104+
print(f"Total: {len(passed_tests) + len(failed_tests)}")
105+
print(f"Passed: {len(passed_tests)}")
106+
print(f"Failed: {len(failed_tests)}")
107+
108+
if passed_tests:
109+
print("\nPassed tests:")
110+
for name in passed_tests:
111+
print(f" - {name}")
112+
113+
if failed_tests:
114+
print("\nFailed tests:")
115+
for name, reason in failed_tests:
116+
print(f" - {name}:\n")
117+
print(" " + reason.strip().replace('\n', '\n '))
118+
print()
119+
120+
if failed_tests:
121+
sys.exit(1)
122+
123+
except Exception as err:
124+
print(f"\nFatal error: {err}")
125+
sys.exit(1)
126+
127+
if __name__ == "__main__":
128+
main()

0 commit comments

Comments
 (0)