gazprea-fuzzer-python/ast_parser/test/test_unparse.py

157 lines
7.1 KiB
Python
Raw Normal View History

2023-11-17 16:57:53 -07:00
import unittest
from ast_parser.gaz_unparser import GazUnparser
import xml.etree.ElementTree as ET
2023-11-17 16:57:53 -07:00
class TestParseCode(unittest.TestCase):
def test_unparse_variable_regular(self):
input = '<variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/>'
parser = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual("a", parser.source)
2023-11-17 16:57:53 -07:00
def test_unparse_variable_declaration(self):
input = '<variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/>'
parser = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_variable(parser.xml, True)
self.assertIsNotNone(parser.source)
self.assertEqual("var integer a", parser.source)
2023-11-17 16:57:53 -07:00
def test_unparse_rhs_single(self):
input = '<rhs><literal type="int" value="1" /></rhs>'
parser = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual("1", parser.source)
2023-11-17 16:57:53 -07:00
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 = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual("var integer a = 1;\n", parser.source)
2023-11-17 16:57:53 -07:00
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 = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual("a * 42 -> std_output;\n", parser.source)
2023-11-17 16:57:53 -07:00
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 = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual("{\n var integer a = 1;\n a * 42 -> std_output;\n return 0;\n}\n\n", parser.source)
def test_unparse_assignment(self):
with open("ast_parser/test/xml/assignment.xml", "r") as f:
input = f.read()
parser = GazUnparser(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.out", "r") as f:
output = f.read()
parser = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual(output, parser.source)
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.out", "r") as f:
output = f.read()
parser = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual(output, parser.source)
2023-11-17 16:57:53 -07:00
def test_unparse_operation_single(self):
input = '<operator op="multiplication" type="unset"> <lhs> <variable mut="var" type="int" name="a" ref_name="_lsiyjvtbvnpmlml"/> </lhs> <rhs> <literal type="int" value="42" /> </rhs> </operator>'
parser = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual("a * 42", parser.source)
2023-11-17 16:57:53 -07:00
def test_unparse_return(self):
input = '<return> <literal type="int" value="0" /> </return>'
parser = GazUnparser(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 = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual(output, parser.source)
2023-11-17 16:57:53 -07:00
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 = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
2023-11-17 16:57:53 -07:00
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.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.out", 'r') as f:
output = f.read()
parser = GazUnparser(ET.fromstring(input), True)
parser.source = ""
parser.unparse_node(parser.xml)
self.assertIsNotNone(parser.source)
self.assertEqual(output, parser.source)
2023-11-17 16:57:53 -07:00
def test_unparse_code(self):
with open("ast_parser/test/xml/test.xml", "r") as input:
parser = GazUnparser(ET.fromstring(input.read()), True)
2023-11-17 16:57:53 -07:00
parser.unparse()
self.assertIsNotNone(parser.source)
2023-11-17 16:57:53 -07:00
with open("ast_parser/test/xml/input.in", "r") as input:
2023-11-17 16:57:53 -07:00
i = input.read()
self.assertEqual(i, parser.source)
2023-11-17 16:57:53 -07:00
if __name__ == "__main__":
unittest.main()