import unittest import xml.etree.ElementTree as ET from ast_parser.python_unparser import PythonUnparser class TestPythonUnparseCode(unittest.TestCase): def test_unparse_variable_regular(self): input = '' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual("a", parser.source) def test_unparse_variable_declaration(self): input = '' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_variable(parser.xml, True) self.assertIsNotNone(parser.source) self.assertEqual("a: int", parser.source) def test_unparse_rhs_single(self): input = '' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual("1", parser.source) def test_unparse_declaration(self): input = '' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual("a: int = 1\n", parser.source) def test_unparse_stream(self): input = ' ' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual("print(a * 42, end='')\n", parser.source) def test_unparse_block(self): input = ' ' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual(parser.source, "\n a: int = 1\n print(a * 42, end='')\n return 0\n pass\n\n\n") def test_unparse_assignment(self): with open("ast_parser/test/xml/assignment.xml", "r") as f: input = f.read() parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual("C = 30\n", parser.source) def test_unparse_conditional(self): # TODO test the else-if statements with open("ast_parser/test/xml/conditional.xml", "r") as f: input = f.read() with open("ast_parser/test/xml/conditional_py.out", "r") as f: output = f.read() parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual(parser.source, output) def test_unparse_loop(self): with open("ast_parser/test/xml/loop.xml", "r") as f: input = f.read() with open("ast_parser/test/xml/loop_py.out", "r") as f: output = f.read() parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual(output, parser.source) def test_unparse_operation_single(self): input = ' ' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual("a * 42", parser.source) def test_unparse_return(self): input = ' ' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual("return 0\n", parser.source) def test_unparse_unary(self): with open("ast_parser/test/xml/unary.xml", "r") as f: input = f.read() with open("ast_parser/test/xml/unary.out", "r") as f: output = f.read() parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual(output, parser.source) def test_unparse_routine(self): input = '' parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) i = ' ' * parser.indentation self.assertEqual("def main() -> int:\n a: int = 1\n print(a * 42, end='')\n return 0\n pass\n\n\n", parser.source) def test_unparse_args(self): with open("ast_parser/test/xml/many_args.xml", 'r') as f: input = f.read() with open("ast_parser/test/xml/many_args_pt.out", 'r') as f: output = f.read() parser = PythonUnparser(ET.fromstring(input), True) parser.source = "" parser.unparse_node(parser.xml) self.assertIsNotNone(parser.source) self.assertEqual(parser.source, output) def test_unparse_code(self): with open("ast_parser/test/xml/test.xml", "r") as input: parser = PythonUnparser(ET.fromstring(input.read()), True) parser.unparse() self.assertIsNotNone(parser.source) with open("ast_parser/test/xml/input_py.in", "r") as input: i = input.read() self.assertEqual(i, parser.source) if __name__ == "__main__": unittest.main()