class TestInterpreterFakultaet(unittest.TestCase): def setUp(self): with open(os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), 'fakultaet.json'), 'r') as file: TREE = json.load(file)['data'] self.interpreter = Interpreter(TREE) self.interpreter.run() def test_result(self): result = self.interpreter.globs['$result'] self.assertEqual(result[0], 'int') self.assertIn(result[1][0], [math.factorial(i) for i in range(0, 6)])
def setUp(self): with open(os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), 'fakultaet.json'), 'r') as file: TREE = json.load(file)['data'] self.interpreter = Interpreter(TREE) self.interpreter.run()
class TestInterpreterEinmaleins(unittest.TestCase): def setUp(self): with open(os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), 'einmaleins.json'), 'r') as file: TREE = json.load(file)['data'] self.interpreter = Interpreter(TREE) self.interpreter.run() def test_treechecker(self): self.assertEqual(self.interpreter.check_tree(), None) def test_tree(self): self.assertEqual(self.interpreter.tree, []) def test_term(self): # Example: ['times', [['int', [1]], ['int', [4]]]] self.assertIn('term', self.interpreter.globs) term = self.interpreter.globs['term'] self.assertEqual(term[0], 'times') self.assertIsInstance(term[1], list) self.assertIsInstance(term[1][0], list) self.assertIsInstance(term[1][1], list) self.assertEqual(term[1][0][0], 'int') self.assertEqual(term[1][1][0], 'int') self.assertIn(term[1][0][1][0], list(range(1, 11))) self.assertIn(term[1][1][1][0], list(range(1, 11))) def test_task(self): self.assertIn('$task', self.interpreter.globs) task = self.interpreter.globs['$task'] self.assertEqual(task[0], 'eq') self.assertEqual(task[1][0], ['question', []]) self.assertEqual(task[1][1][0], 'times') self.assertEqual(len(task[1][1][1]), 2) def test_result(self): globs = self.interpreter.globs self.assertIn('$result', globs) result = globs['$result'] self.assertEqual(result[0], 'int') self.assertIsInstance(result[1], list) self.assertEqual(len(result[1]), 1) self.assertIn(result[1][0], list(range(1, 101))) self.assertEqual(result[1][0], globs['a'][1][0] * globs['b'][1][0])
def test_minimal(self): TREE = [] interpreter = Interpreter(TREE) interpreter.run() self.assertEqual(interpreter.tree, [])
def test_math_times(self): TREE = [['import', ['math', ['times', [['int', [2]], ['int', [4]]]]]]] interpreter = Interpreter(TREE) interpreter.run() self.assertEqual(interpreter.tree, [['int', [8]]])
def test_symbol(self): TREE = [['symbol', ['foobar']]] interpreter = Interpreter(TREE) interpreter.globs['foobar'] = ['int', [5]] interpreter.run() self.assertEqual(interpreter.tree, [['int', [5]]])
def test_define(self): TREE = [['define', ['foobar', ['int', [1]]]]] interpreter = Interpreter(TREE) interpreter.run() self.assertIn('foobar', interpreter.globs) self.assertEqual(interpreter.globs['foobar'], ['int', [1]])