Skip to content
This repository was archived by the owner on Oct 12, 2026. It is now read-only.

Commit 80ac54f

Browse files
committed
test: add tests
1 parent 3e57d31 commit 80ac54f

9 files changed

Lines changed: 116 additions & 0 deletions

File tree

‎examples/out/hello_world.out.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World

‎examples/out/if_then.out.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
True
2+
False

‎examples/out/list.out.txt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
list: []
2+
list: [Hello]
3+
list size: 1.000000
4+
list: [Hello, World]
5+
list[1]: World
6+
list: [Hello, World, Foo]
7+
list: [Hello, World]

‎examples/out/math_demos.out.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
55.000000

‎examples/out/pair.out.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1.000000
2+
2.000000

‎examples/out/shapes.out.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Volume of cylinder is: 785.000000

‎tests/logger.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
3+
4+
def useColor() -> bool:
5+
return sys.stdout.isatty()
6+
7+
8+
def logInternal(ansiColor: str, logLevel: str, message: str):
9+
if useColor():
10+
print(f"[{ansiColor}{logLevel}\033[0m] {message}")
11+
else:
12+
print(f"[{logLevel}] {message}")
13+
14+
15+
def error(message: str):
16+
logInternal("\033[31m", "ERROR", message)
17+
18+
19+
def info(message: str):
20+
logInternal("\033[34m", "INFO", message)
21+
22+
23+
def warning(message: str):
24+
logInternal("\033[33m", "WARNING", message)
25+
26+
27+
def success(message: str):
28+
logInternal("\033[32m", "SUCCESS", message)

‎tests/test_cases.json‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"name": "Hello World",
4+
"source": "examples/hello_world.lbd",
5+
"output": "examples/out/hello_world.out.txt"
6+
},
7+
{
8+
"name": "Conditional branching",
9+
"source": "examples/if_then.lbd",
10+
"output": "examples/out/if_then.out.txt"
11+
},
12+
{
13+
"name": "List",
14+
"source": "examples/list.lbd",
15+
"output": "examples/out/list.out.txt"
16+
},
17+
{
18+
"name": "Fibonacci",
19+
"source": "examples/math_demos.lbd",
20+
"output": "examples/out/math_demos.out.txt"
21+
},
22+
{
23+
"name": "Pair",
24+
"source": "examples/pair.lbd",
25+
"output": "examples/out/pair.out.txt"
26+
},
27+
{
28+
"name": "Shapes",
29+
"source": "examples/shapes.lbd",
30+
"output": "examples/out/shapes.out.txt"
31+
}
32+
]

‎tests/tests.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)