import os
import subprocess

COMPILER = "./build/bin/compiler"
TEST_DIR = "./test/test_case/functional"

pass_cnt = 0
fail_cnt = 0

print("===== SysY Batch Test Start =====")

for file in os.listdir(TEST_DIR):
    if not file.endswith(".sy"):
        continue

    path = os.path.join(TEST_DIR, file)
    print(f"[TEST] {file} ... ", end="")

    result = subprocess.run(
        [COMPILER, "--emit-parse-tree", path],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.PIPE
    )

    if result.returncode == 0:
        print("PASS")
        pass_cnt += 1
    else:
        print("FAIL")
        fail_cnt += 1
        print("---- Error ----")
        print(result.stderr.decode())
        print("---------------")

print("===============================")
print(f"Total: {pass_cnt + fail_cnt}")
print(f"PASS : {pass_cnt}")
print(f"FAIL : {fail_cnt}")
print("===============================")