import concurrent.futures import os 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 class TestCorrectness(unittest.TestCase): @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 self.ast_gen.generate_declaration(mut='var') 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): 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() self.python_unparser.xml = self.ast_gen.ast self.python_unparser.unparse() try: compile(self.python_unparser.source, "beeeans", "exec") except Exception as e: print(self.python_unparser.source) self.fail(e) 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() 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) 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") if __name__ == '__main__': unittest.main()