107 lines
5.1 KiB
Python
107 lines
5.1 KiB
Python
|
import unittest
|
||
|
|
||
|
from ast_parser.ast_parser import AstParser
|
||
|
|
||
|
|
||
|
class TestParseCode(unittest.TestCase):
|
||
|
def test_parse_code(self):
|
||
|
input = """
|
||
|
procedure main() returns integer {
|
||
|
integer a = 1;
|
||
|
a * 42 -> std_output;
|
||
|
return 0;
|
||
|
}
|
||
|
"""
|
||
|
|
||
|
parser = AstParser(input)
|
||
|
parser.parse()
|
||
|
self.assertIsNotNone(parser.xml)
|
||
|
self.assertEqual(parser.xml.getroot().tag, "block")
|
||
|
|
||
|
def test_unparse_variable_regular(self):
|
||
|
input = '<variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual(" a ", parser.input)
|
||
|
|
||
|
def test_unparse_variable_declaration(self):
|
||
|
input = '<variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._variable_unparse(parser.xml, True)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual("var integer a ", parser.input)
|
||
|
|
||
|
|
||
|
def test_unparse_rhs_single(self):
|
||
|
input = '<rhs><literal type="int" value="1" /></rhs>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual(" 1 ", parser.input)
|
||
|
|
||
|
def test_unparse_declaration(self):
|
||
|
input = '<declaration><variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/><rhs><literal type="int" value="1" /></rhs></declaration>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual("var integer a = 1 ;\n", parser.input)
|
||
|
|
||
|
def test_unparse_stream(self):
|
||
|
input = ' <stream type="std_output"> <operator op="*" type="unset"> <lhs> <variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/> </lhs> <rhs> <literal type="int" value="42" /> </rhs> </operator></stream>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual(" a * 42 -> std_output;\n", parser.input)
|
||
|
|
||
|
def test_unparse_block(self):
|
||
|
input = '<block> <declaration> <variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/> <rhs> <literal type="int" value="1" /> </rhs> </declaration> <stream type="std_output"> <operator op="*" type="unset"> <lhs> <variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/> </lhs> <rhs> <literal type="int" value="42" /> </rhs> </operator> </stream> <return> <literal type="int" value="0" /> </return> </block>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual("{\n var integer a = 1 ;\n a * 42 -> std_output;\n return 0 ;\n}\n\n", parser.input)
|
||
|
|
||
|
def test_unparse_operation_single(self):
|
||
|
input = '<operator op="*" type="unset"> <lhs> <variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/> </lhs> <rhs> <literal type="int" value="42" /> </rhs> </operator>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual(" a * 42 ", parser.input)
|
||
|
|
||
|
def test_unparse_return(self):
|
||
|
input = '<return> <literal type="int" value="0" /> </return>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
self.assertEqual("return 0 ;\n", parser.input)
|
||
|
|
||
|
def test_unparse_routine(self):
|
||
|
input = '<procedure name="main" ref_name="_jemrvvseyj" return_type="int" args="()" ><block><declaration><variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/><rhs><literal type="int" value="1" /></rhs></declaration><stream type="std_output"><operator op="*" type="unset"><lhs><variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/></lhs><rhs><literal type="int" value="42" /></rhs></operator></stream><return><literal type="int" value="0" /></return></block></procedure>'
|
||
|
parser = AstParser(input, True)
|
||
|
parser.input = ""
|
||
|
parser._unparse_node(parser.xml)
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
i = ' ' * parser.indentation
|
||
|
self.assertEqual("procedure main() returns integer {\n var integer a = 1 ;\n a * 42 -> std_output;\n return 0 ;\n}\n\n", parser.input)
|
||
|
|
||
|
def test_unparse_code(self):
|
||
|
with open("test.xml", "r") as input:
|
||
|
parser = AstParser(input.read(), True)
|
||
|
parser.unparse()
|
||
|
self.assertIsNotNone(parser.input)
|
||
|
|
||
|
with open("input.in", "r") as input:
|
||
|
i = input.read()
|
||
|
self.assertEqual(i, parser.input)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
unittest.main()
|