2023-11-20 20:28:55 -07:00
|
|
|
import concurrent.futures
|
|
|
|
import os
|
2023-11-19 20:49:25 -07:00
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from contextlib import redirect_stdout
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
import signal
|
|
|
|
|
|
|
|
from ast_generator.ast_generator import AstGenerator
|
|
|
|
from ast_parser.python_unparser import PythonUnparser
|
|
|
|
|
|
|
|
|
2023-11-20 18:52:14 -07:00
|
|
|
class TestCorrectness(unittest.TestCase):
|
2023-11-19 20:49:25 -07:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
with open("config.yaml", 'r') as stream:
|
|
|
|
cls.props = yaml.safe_load(stream)
|
|
|
|
|
|
|
|
def setUp(cls):
|
|
|
|
cls.ast_gen = AstGenerator(cls.props)
|
|
|
|
cls.python_unparser = PythonUnparser(cls.ast_gen.ast, True)
|
|
|
|
def test_assignment(self):
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
2023-11-20 20:28:55 -07:00
|
|
|
self.ast_gen.generate_declaration(mut='var')
|
2023-11-19 20:49:25 -07:00
|
|
|
self.ast_gen.generate_assignment()
|
|
|
|
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
except Exception as e:
|
|
|
|
self.fail(e)
|
|
|
|
|
|
|
|
def test_binary_operator(self):
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
|
|
|
self.ast_gen.generate_declaration()
|
|
|
|
self.ast_gen.generate_binary('addition', 'int')
|
|
|
|
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
except Exception as e:
|
|
|
|
self.fail(e)
|
|
|
|
|
|
|
|
def test_unary_operator(self):
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
|
|
|
self.ast_gen.generate_declaration()
|
|
|
|
self.ast_gen.generate_unary('negation', 'int')
|
|
|
|
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
except Exception as e:
|
|
|
|
print(self.python_unparser.source)
|
|
|
|
self.fail(e)
|
|
|
|
|
|
|
|
def test_output(self):
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
|
|
|
self.ast_gen.generate_out_stream()
|
|
|
|
|
|
|
|
output = StringIO()
|
|
|
|
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
|
|
|
|
|
|
|
with redirect_stdout(output):
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(output.getvalue())
|
|
|
|
except Exception as e:
|
|
|
|
self.fail(e)
|
|
|
|
|
|
|
|
outstring = output.getvalue()
|
|
|
|
self.assertNotEqual("", outstring, self.python_unparser.source)
|
|
|
|
|
|
|
|
def test_input(self): #FIXME this doesn't actually accept user input, I think I need to create a global dict
|
|
|
|
# to store the input and then access it from the python in some way...
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
|
|
|
self.ast_gen.generate_in_stream()
|
|
|
|
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
except Exception as e:
|
|
|
|
self.fail(e)
|
|
|
|
|
|
|
|
def test_conditional(self):
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
|
|
|
self.ast_gen.generate_conditional()
|
|
|
|
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
except Exception as e:
|
|
|
|
print(self.python_unparser.source)
|
|
|
|
self.fail(e)
|
|
|
|
|
|
|
|
def test_loop(self):
|
2023-11-20 20:28:55 -07:00
|
|
|
for i in range(1000):
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
|
|
|
self.ast_gen.generate_loop()
|
2023-11-19 20:49:25 -07:00
|
|
|
|
2023-11-20 20:28:55 -07:00
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
2023-11-19 20:49:25 -07:00
|
|
|
|
2023-11-20 20:28:55 -07:00
|
|
|
try:
|
|
|
|
compile(self.python_unparser.source, "beeeans", "exec")
|
|
|
|
except Exception as e:
|
|
|
|
print(self.python_unparser.source)
|
|
|
|
self.fail(e)
|
2023-11-19 20:49:25 -07:00
|
|
|
|
2023-11-20 18:52:14 -07:00
|
|
|
def test_routine(self):
|
|
|
|
self.ast_gen.ast = ET.Element("block")
|
|
|
|
self.ast_gen.current_ast_element = self.ast_gen.ast
|
|
|
|
self.ast_gen.generate_routine()
|
|
|
|
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
2023-11-19 20:49:25 -07:00
|
|
|
|
2023-11-20 18:52:14 -07:00
|
|
|
try:
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
except Exception as e:
|
|
|
|
print(self.python_unparser.source)
|
|
|
|
self.fail(e)
|
|
|
|
|
|
|
|
def test_program(self):
|
|
|
|
self.ast_gen.generate_ast()
|
|
|
|
self.python_unparser.xml = self.ast_gen.ast
|
|
|
|
self.python_unparser.unparse()
|
|
|
|
|
|
|
|
try:
|
|
|
|
exec(self.python_unparser.source)
|
|
|
|
except Exception as e:
|
|
|
|
print(self.python_unparser.source)
|
|
|
|
self.fail(e)
|
2023-11-19 20:49:25 -07:00
|
|
|
|
2023-11-21 10:34:24 -07:00
|
|
|
def test_no_infinite_loops(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_infinite_loops_with_break_terminate(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_builtins(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_all_vars_get_printed(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
class TestTypeCorrectness(unittest.TestCase):
|
|
|
|
def test_int_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_float_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_char_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_bool_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_tuple_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_vector_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_matrix_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
|
|
|
def test_iterable_expr_correctness(self):
|
|
|
|
self.fail("TODO")
|
|
|
|
|
2023-11-19 20:49:25 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|