def test_parse_method(self): tokens = lex("print 1 + 1;") parser = Parser(tokens) ast = parser.parse() expected_ast = AST( [PrintStatement(BinOpexpr("+", Integer(1), Integer(1)))]) self.assertEqual(ast, expected_ast)
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_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 parse_if_statement_method_with_else_statement(self): tokens = lex("if x > 22 then\nprint x;\nelse\nprint 0;\ndone\n") parser = Parser(tokens) parser.advance() ast = parser.parse_if_statement() expected_ast = IfStatement(RelOpexpr(">", Varexpr("x"), Integer(22)), AST([PrintStatement(Varexpr("x"))]), AST([PrintStatement(Integer(0))])) self.assertEqual(ast, expected_ast)
def test___repr___method(self): while_stmt = WhileStatement( Integer(1), BinOpexpr("+", Integer(1), Integer(2)), ) while_stmt_repr = repr(while_stmt) expected_repr = "WhileStatement(Integer(1), " \ "BinOpexpr(+, Integer(1), Integer(2)))" self.assertEqual(while_stmt_repr, expected_repr)
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___repr___method(self): if_stmt = IfStatement( Integer(1), BinOpexpr("+", Integer(1), Integer(2)), # no else stmt None) if_stmt_repr = repr(if_stmt) expected_repr = "IfStatement(Integer(1), " \ "BinOpexpr(+, Integer(1), Integer(2)), None)" self.assertEqual(if_stmt_repr, expected_repr)
def test_create_ast_from_postfix_method(self): postfix = [ Token(1, "NUMBER", "INT"), Token(1, "NUMBER", "INT"), Token("+", "RESERVED", "OPERATOR") ] parser = Parser([]) ast = parser.create_ast_from_postfix(postfix) expected_ast = BinOpexpr("+", Integer(1), Integer(1)) self.assertEqual(ast, expected_ast)
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_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_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(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_parse_print_statement_method(self): tokens = lex("print 10;") parser = Parser(tokens) parser.advance() ast = parser.parse_print_statement() expected_ast = PrintStatement(Integer(10)) self.assertEqual(ast, expected_ast)
def test_eval_method_with_true_expression(self): notexpr = Notexpr(Integer(0)) env = {} result = notexpr.eval(env) self.assertTrue(result)
def test_eval_method_with_false_expression(self): notexpr = Notexpr(Integer(1)) env = {} result = notexpr.eval(env) self.assertFalse(result)
def test_parse_statements_method(self): tokens = lex("print 1;") parser = Parser(tokens) parser.parse() expected_ast = AST([PrintStatement(Integer(1))]) self.assertEqual(parser.ast, expected_ast)
def test_insert_node_method(self): ast = AST() ast.insert_node(Integer(10)) ast.insert_node(Varexpr("x")) expected_nodes = [Integer(10), Varexpr("x")] self.assertEqual(ast.nodes, expected_nodes)
def test_create_ast_from_expr_method(self): parser = Parser([]) ast = parser.create_ast_from_expr("+", Integer(1), Integer(1)) expected_ast = BinOpexpr("+", Integer(1), Integer(1)) 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)
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_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): binopexpr = BinOpexpr("+", Integer(1), Integer(1)) binopexpr_repr = repr(binopexpr) expected_repr = "BinOpexpr(+, Integer(1), Integer(1))" self.assertEqual(binopexpr_repr, expected_repr)
def test_eval_method_with_times_operator(self): binopexpr = BinOpexpr("*", Integer(12), Integer(5)) env = {} result = binopexpr.eval(env) self.assertEqual(result, 60)
def test___repr___method(self): ast = AST() ast.insert_node(Integer(10)) ast_repr = repr(ast) expected_repr = "Statements([Integer(10)])" self.assertEqual(ast_repr, expected_repr)
def test___repr___method(self): notexpr = Notexpr(Integer(1)) notexpr_repr = repr(notexpr) expected_repr = "Notexpr(Integer(1))" self.assertEqual(notexpr_repr, expected_repr)
def test___repr___method(self): integer = Integer(10) integer_repr = repr(integer) expected_repr = "Integer(10)" self.assertEqual(integer_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_expr_method(self): tokens = lex("1+1;") parser = Parser(tokens) ast = parser.parse_expr() expected_ast = BinOpexpr("+", Integer(1), Integer(1)) self.assertEqual(ast, expected_ast)
def test_eval_method(self): integer = Integer(10) env = {} result = integer.eval(env) self.assertEqual(result, 10)
def test___repr___method(self): print_statment = PrintStatement(Integer(10)) print_statment_repr = repr(print_statment) expected_repr = "PrintStatement(Integer(10))" self.assertEqual(print_statment_repr, expected_repr)