135 lines
4 KiB
Python
135 lines
4 KiB
Python
|
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 MyTestCase(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()
|
||
|
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):
|
||
|
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:
|
||
|
exec(self.python_unparser.source)
|
||
|
except Exception as e:
|
||
|
print(self.python_unparser.source)
|
||
|
self.fail(e)
|
||
|
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|