def test_eval_method_with_false_condition_and_else_stmt(self): if_stmt = IfStatement( Integer(0), AssignStatement("x", Integer(10)), # else stmt AssignStatement("y", Integer(12)), ) env = {} if_stmt.eval(env) self.assertNotIn("x", env) self.assertIn("y", env) self.assertEqual(env["y"], 12)
def test_interpret_method(self): env = {} ast = AST([AssignStatement("x", Integer(10))]) interpreter = Interpreter(ast, env) interpreter.interpret() self.assertIn("x", env) self.assertEqual(env["x"], 10)
def test_eval_method_with_false_expression(self): env = {"x": 0} while_stmt = WhileStatement( RelOpexpr(">", Varexpr("x"), Integer(10)), AssignStatement("x", BinOpexpr("+", Varexpr("x"), Integer(1)))) while_stmt.eval(env) self.assertEqual(env["x"], 0)
def test_eval_method(self): ast = AST() ast.insert_node(AssignStatement("x", Integer(10))) env = {} ast.eval(env) self.assertIn("x", env) self.assertEqual(env["x"], 10)
def test_eval_method_with_false_condition_and_no_else_stmt(self): if_stmt = IfStatement( Integer(0), AssignStatement("x", Integer(10)), # eno lse stmt None) env = {} if_stmt.eval(env) self.assertNotIn("x", env)
def test_eval_method_with_true_condition_and_no_else_stmt(self): if_stmt = IfStatement( Integer(1), AssignStatement("x", Integer(10)), # no else stmt None) env = {} if_stmt.eval(env) self.assertIn("x", env) self.assertEqual(env["x"], 10)
def test_parse_while_statement_method(self): tokens = lex("while x > 22 do\nprint x;\nx := x - 1;\ndone\n") parser = Parser(tokens) parser.advance() ast = parser.parse_while_statement() expected_ast = WhileStatement( RelOpexpr(">", Varexpr("x"), Integer(22)), AST([ PrintStatement(Varexpr("x")), AssignStatement("x", BinOpexpr("-", Varexpr("x"), Integer(1))) ]), ) self.assertEqual(ast, expected_ast)
def test_eval_method(self): assign_stmt = AssignStatement("x", Integer(10)) env = {} assign_stmt.eval(env) self.assertIn("x", env) self.assertEqual(env["x"], 10)
def test___repr___method(self): env = {} interpreter = Interpreter(AST([AssignStatement("x", 10)]), env) interpreter_repr = repr(interpreter) expected_repr = "Interpreter(Statements([AssignStatement(x, 10)]), {})" self.assertEqual(interpreter_repr, expected_repr)
def test_parse_function(self): string = "x := 10;" ast = parse(string) expected_ast = AST([AssignStatement("x", Integer(10))]) self.assertEqual(ast, expected_ast)
def test_parse_var_assignment_method(self): tokens = lex("x := 10;") parser = Parser(tokens) ast = parser.parse_var_assignment() expected_ast = AssignStatement("x", Integer(10)) self.assertEqual(ast, expected_ast)
def test___repr___method(self): assign_stmt = AssignStatement("x", Integer(10)) assign_stmt_repr = repr(assign_stmt) expected_repr = "AssignStatement(x, Integer(10))" self.assertEqual(assign_stmt_repr, expected_repr)