gemstone/tests/ast/test_ast.py

152 lines
2.5 KiB
Python
Raw Permalink Normal View History

2024-05-07 13:22:52 +00:00
import subprocess
import sys
import logging
from logging import info, error
import os
BIN_DIR = "bin/tests/ast/"
def run_check_build_tree():
info("started check tree build...")
p = subprocess.run(BIN_DIR + "build_tree", capture_output=True, text=True)
info("checking exit code...")
# check exit code
assert p.returncode == 0
def run_check_print_node():
info("started check node print...")
p = subprocess.run(BIN_DIR + "print_node", capture_output=True, text=True)
info("checking exit code...")
# check exit code
assert p.returncode == 0
2024-05-08 11:49:06 +00:00
assert """0 stmt
2024-05-12 21:33:15 +00:00
1 module
2 expr
2024-05-08 11:49:06 +00:00
3 value
4 value
2024-05-12 21:33:15 +00:00
5 value
6 while
7 if
8 else if
9 else
10 condition
11 decl
12 assign
13 def
14 value
15 +
16 -
17 *
18 /
19 &
20 |
21 ^
22 !
23 &&
24 ||
25 ^^
26 !!
27 ==
28 >
29 <
2024-05-21 10:21:54 +00:00
30 typecast
31 transmute
2024-05-15 14:26:40 +00:00
32 funcall
2024-05-12 21:33:15 +00:00
33 value
34 typedef
35 box
36 fun
2024-08-05 19:21:25 +00:00
37 fun
38 fun
39 fun
40 value
41 list
42 expr list
43 arg list
44 param list
45 stmt list
46 ident list
2024-05-15 14:26:40 +00:00
47 value
2024-08-05 19:21:25 +00:00
48 type
49 value
50 value
2024-05-15 14:26:40 +00:00
51 value
2024-08-05 19:21:25 +00:00
52 -
53 parameter
54 value
55 parameter-declaration
56 address of
57 deref
58 ref
59 value
60 value
61 ret
2024-05-08 11:49:06 +00:00
""" == p.stdout
def run_check_print_graphviz():
info("started check print graphviz...")
2024-05-08 11:55:40 +00:00
info("creating temporary folder...")
if not os.path.exists("tmp"):
2024-08-05 19:21:25 +00:00
os.makedirs("tmp", exist_ok=True)
2024-05-08 11:55:40 +00:00
info("cleaning temporary folder...")
if os.path.exists("tmp/graph.gv"):
os.remove("tmp/graph.gv")
if os.path.exists("tmp/graph.svg"):
os.remove("tmp/graph.svg")
2024-05-08 11:49:06 +00:00
p = subprocess.run(BIN_DIR + "print_graphviz", capture_output=True, text=True)
info("checking exit code...")
# check exit code
assert p.returncode == 0
info("converting gv to svg...")
p = subprocess.run(["dot", "-Tsvg", "tmp/graph.gv", "-otmp/graph.svg"])
info("checking exit code...")
assert p.returncode == 0
info("checking svg output...")
with open("tmp/graph.svg", "r") as file:
string = "".join(file.readlines())
assert "2.3" in string
assert "0.79" in string
assert "stmt" in string
assert "if" in string
2024-05-07 13:22:52 +00:00
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
target = sys.argv[1]
info(f"starting ast test suite with target: {target}")
match target:
case "check_build_tree":
run_check_build_tree()
case "check_print_node":
run_check_print_node()
2024-05-08 11:49:06 +00:00
case "check_print_graphviz":
run_check_print_graphviz()
2024-05-07 13:22:52 +00:00
case _:
error(f"unknown target: {target}")
exit(1)