Пример #1
0
class NamedExprTest(CSTNodeTest):
    @data_provider(
        (
            {
                "node": cst.BinaryOperation(
                    left=cst.Name("a"),
                    operator=cst.MatrixMultiply(),
                    right=cst.Name("b"),
                ),
                "code": "a @ b",
                "parser": parse_expression_as(python_version="3.8"),
            },
            {
                "node": cst.SimpleStatementLine(
                    body=(
                        cst.AugAssign(
                            target=cst.Name("a"),
                            operator=cst.MatrixMultiplyAssign(),
                            value=cst.Name("b"),
                        ),
                    ),
                ),
                "code": "a @= b\n",
                "parser": parse_statement_as(python_version="3.8"),
            },
        )
    )
    def test_valid(self, **kwargs: Any) -> None:
        self.validate_node(**kwargs)

    @data_provider(
        (
            {
                "code": "a @ b",
                "parser": parse_expression_as(python_version="3.6"),
                "expect_success": True,
            },
            {
                "code": "a @ b",
                "parser": parse_expression_as(python_version="3.3"),
                "expect_success": False,
            },
            {
                "code": "a @= b",
                "parser": parse_statement_as(python_version="3.6"),
                "expect_success": True,
            },
            {
                "code": "a @= b",
                "parser": parse_statement_as(python_version="3.3"),
                "expect_success": False,
            },
        )
    )
    def test_versions(self, **kwargs: Any) -> None:
        if is_native() and not kwargs.get("expect_success", True):
            self.skipTest("parse errors are disabled for native parser")
        self.assert_parses(**kwargs)
Пример #2
0
class BinaryOperationTest(CSTNodeTest):
    @data_provider(
        (
            # Simple binary operations
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.Add(),
                                    cst.Float("5.5")),
                "code":
                "foo + 5.5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.Subtract(),
                                    cst.Float("5.5")),
                "code":
                "foo - 5.5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.LeftShift(),
                                    cst.Integer("5")),
                "code":
                "foo << 5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.RightShift(),
                                    cst.Integer("5")),
                "code":
                "foo >> 5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.BitAnd(),
                                    cst.Name("bar")),
                "code":
                "foo & bar",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.BitXor(),
                                    cst.Name("bar")),
                "code":
                "foo ^ bar",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.BitOr(),
                                    cst.Name("bar")),
                "code":
                "foo | bar",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.Multiply(),
                                    cst.Float("5.5")),
                "code":
                "foo * 5.5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.MatrixMultiply(),
                                    cst.Float("5.5")),
                "code":
                "foo @ 5.5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.Divide(),
                                    cst.Float("5.5")),
                "code":
                "foo / 5.5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.Modulo(),
                                    cst.Float("5.5")),
                "code":
                "foo % 5.5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            {
                "node":
                cst.BinaryOperation(cst.Name("foo"), cst.FloorDivide(),
                                    cst.Float("5.5")),
                "code":
                "foo // 5.5",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            # Parenthesized binary operation
            {
                "node":
                cst.BinaryOperation(
                    lpar=(cst.LeftParen(), ),
                    left=cst.Name("foo"),
                    operator=cst.LeftShift(),
                    right=cst.Integer("5"),
                    rpar=(cst.RightParen(), ),
                ),
                "code":
                "(foo << 5)",
                "parser":
                parse_expression,
                "expected_position":
                None,
            },
            # Make sure that spacing works
            {
                "node":
                cst.BinaryOperation(
                    lpar=(cst.LeftParen(
                        whitespace_after=cst.SimpleWhitespace(" ")), ),
                    left=cst.Name("foo"),
                    operator=cst.Multiply(
                        whitespace_before=cst.SimpleWhitespace("  "),
                        whitespace_after=cst.SimpleWhitespace("  "),
                    ),
                    right=cst.Name("bar"),
                    rpar=(cst.RightParen(
                        whitespace_before=cst.SimpleWhitespace(" ")), ),
                ),
                "code":
                "( foo  *  bar )",
                "parser":
                parse_expression,
                "expected_position":
                CodeRange((1, 2), (1, 13)),
            },
        ))
    def test_valid(self, **kwargs: Any) -> None:
        self.validate_node(**kwargs)

    @data_provider((
        {
            "get_node": (lambda: cst.BinaryOperation(
                cst.Name("foo"),
                cst.Add(),
                cst.Name("bar"),
                lpar=(cst.LeftParen(), ),
            )),
            "expected_re":
            "left paren without right paren",
        },
        {
            "get_node": (lambda: cst.BinaryOperation(
                cst.Name("foo"),
                cst.Add(),
                cst.Name("bar"),
                rpar=(cst.RightParen(), ),
            )),
            "expected_re":
            "right paren without left paren",
        },
    ))
    def test_invalid(self, **kwargs: Any) -> None:
        self.assert_invalid(**kwargs)