def parse_break(self): """break_stmt : BREAK""" node = ast.Break(loc=self.curr_token.loc) self.advance('BREAK') return node
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) @pytest.mark.parametrize('node', [ast.Function(None, None),
def test_break(): root = ast.Block( [ast.While(ast.BooleanConstant(True), ast.Block([ast.Break()]))]) lp = LoopPass() lp.visit(root)
assert_tree(_tree, _expected) elif isinstance(tree, ast.Node): for attr_name, expected_value in vars(expected).items(): assert_tree(getattr(tree, attr_name), expected_value) else: assert tree == expected @pytest.mark.parametrize('with_dead_code,without_dead_code', [ ([], []), ([ ast.BinaryOp(ast.Operator.ADD, ast.IntegerConstant(0), ast.IntegerConstant(1)), ast.Return(), ast.Break() ], [ ast.BinaryOp(ast.Operator.ADD, ast.IntegerConstant(0), ast.IntegerConstant(1)), ast.Return() ]), ([ ast.BinaryOp(ast.Operator.ADD, ast.IntegerConstant(0), ast.IntegerConstant(1)), ast.Break(), ast.Return() ], [
def test_break_outside_loop(): root = ast.Block([ast.Break()]) lp = LoopPass() with pytest.raises(DumbSyntaxError): lp.visit(root)