def parse_if(self): """ if_stmt : IF expr block | IF expr block ELSE block | IF expr block ELSE if_stmt """ loc = self.curr_token.loc self.advance('IF') cond = self.parse_expr() then = self.parse_block() if self.curr_token.kind != 'ELSE': return ast.If(cond, then, loc=loc) self.advance('ELSE') if self.curr_token.kind == 'IF': otherwise = self.parse_if() else: otherwise = self.parse_block() return ast.If(cond, then, otherwise, loc=loc)
def test_if_with_else(): cond = ast.BooleanConstant(True) root = ast.If(cond, ast.Block([]), ast.Block([])) tp = TypePass() tp.visit(root)
def test_if(cond): root = ast.If(cond, ast.Block([])) tp = TypePass() tp.visit(root)
def test_if_bad_cond(cond): root = ast.If(cond, ast.Block([])) tp = TypePass() with pytest.raises(DumbTypeError): tp.visit(root)
ast.BooleanConstant(None), ast.StringConstant(None), ast.Identifier(None), ast.FuncCall(None, None) ]) def test_expr_visitor(node): visitor = StubExprVisitor() method_name = 'visit_' + type(node).__name__ with mock.patch.object(visitor, method_name): visitor.visit(node) getattr(visitor, method_name).assert_called_once_with(node) @pytest.mark.parametrize('node', [ ast.Block(None), ast.If(None, None), ast.While(None, None), ast.Break(), ast.Continue(), ast.Return(), ast.Var(None, None), ast.Expression(None) ]) def test_stmt_visitor(node): visitor = StubStmtVisitor() method_name = 'visit_' + type(node).__name__ with mock.patch.object(visitor, method_name): visitor.visit(node) getattr(visitor, method_name).assert_called_once_with(node)