|
| 1 | +from os.path import dirname, join |
| 2 | +import subprocess |
| 3 | +import json |
| 4 | +import logger |
| 5 | + |
| 6 | +TESTS = dirname(__file__) |
| 7 | +ROOT = dirname(TESTS) |
| 8 | +BUILD = join(ROOT, "cmake-build-debug") |
| 9 | +EXE = join(BUILD, "lbd") |
| 10 | +TEST_CASES = join(TESTS, "test_cases.json") |
| 11 | + |
| 12 | +TEST_CASE_NAME_KEY = "name" |
| 13 | +TEST_CASE_SOURCE_KEY = "source" |
| 14 | +TEST_CASE_OUTPUT_KEY = "output" |
| 15 | + |
| 16 | + |
| 17 | +def execCommand(command): |
| 18 | + return subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 19 | + |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + with open(TEST_CASES, mode="r", encoding="utf-8") as file: |
| 23 | + testCases = json.load(file) |
| 24 | + passCount, failCount, totalCount = 0, 0, 0 |
| 25 | + for testCase in testCases: |
| 26 | + if TEST_CASE_SOURCE_KEY not in testCase or TEST_CASE_OUTPUT_KEY not in testCase: |
| 27 | + logger.error("Invalid test-case.") |
| 28 | + continue |
| 29 | + testName = testCase[TEST_CASE_NAME_KEY] if TEST_CASE_NAME_KEY in testCase else testCase[TEST_CASE_SOURCE_KEY] |
| 30 | + result = execCommand([EXE, "--file", testCase[TEST_CASE_SOURCE_KEY]]) |
| 31 | + if (exitCode := result.returncode) != 0: |
| 32 | + logger.error(f"Test failed '{testName}' due to non-zero exit-code '{exitCode}'.") |
| 33 | + with open(join(ROOT, testCase[TEST_CASE_OUTPUT_KEY]), mode="r") as file: |
| 34 | + expectedStdout = file.read() |
| 35 | + totalCount += 1 |
| 36 | + if result.stdout == expectedStdout: |
| 37 | + logger.success(f"Test passed '{testName}'.") |
| 38 | + passCount += 1 |
| 39 | + else: |
| 40 | + logger.error(f"Test failed '{testName}' due to output mismatch.") |
| 41 | + failCount += 1 |
| 42 | + logger.info(f"Passed: {passCount}. Failed: {failCount}. Total: {totalCount}.") |
0 commit comments