diff --git a/slither/vyper_parsing/ast/ast.py b/slither/vyper_parsing/ast/ast.py index 36269a7450..01ca2c352a 100644 --- a/slither/vyper_parsing/ast/ast.py +++ b/slither/vyper_parsing/ast/ast.py @@ -259,8 +259,8 @@ def parse_unary_op(raw: dict) -> UnaryOp: "Mod": "%", "BitAnd": "&", "BitOr": "|", - "Shr": "<<", - "Shl": ">>", + "Shr": ">>", + "Shl": "<<", "NotEq": "!=", "Eq": "==", "LtE": "<=", @@ -378,13 +378,13 @@ def parse_enum_def(raw: dict) -> EnumDef: "Add": "+=", "Mult": "*=", "Sub": "-=", - "Div": "-=", + "Div": "/=", "Pow": "**=", "Mod": "%=", "BitAnd": "&=", "BitOr": "|=", - "Shr": "<<=", - "Shl": ">>=", + "Shr": ">>=", + "Shl": "<<=", } diff --git a/tests/unit/vyper_parsing/__init__.py b/tests/unit/vyper_parsing/__init__.py new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/tests/unit/vyper_parsing/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit/vyper_parsing/test_ast.py b/tests/unit/vyper_parsing/test_ast.py new file mode 100644 index 0000000000..7142963f51 --- /dev/null +++ b/tests/unit/vyper_parsing/test_ast.py @@ -0,0 +1,49 @@ +from slither.core.expressions import AssignmentOperationType, BinaryOperationType +from slither.vyper_parsing.ast.ast import parse_aug_assign, parse_bin_op + + +def _base_node(node_id: int = 0) -> dict: + return {"src": "0:0:0", "node_id": node_id} + + +def _int_node(value: int, node_id: int) -> dict: + return {"ast_type": "Int", "value": value, **_base_node(node_id)} + + +def _name_node(name: str, node_id: int) -> dict: + return {"ast_type": "Name", "id": name, **_base_node(node_id)} + + +def _binary_operation_type(vyper_ast_type: str) -> BinaryOperationType: + expression = parse_bin_op( + { + "left": _int_node(1, 1), + "op": {"ast_type": vyper_ast_type}, + "right": _int_node(2, 2), + **_base_node(), + } + ) + return BinaryOperationType.get_type(expression.op) + + +def _assignment_operation_type(vyper_ast_type: str) -> AssignmentOperationType: + expression = parse_aug_assign( + { + "target": _name_node("x", 1), + "op": {"ast_type": vyper_ast_type}, + "value": _int_node(2, 2), + **_base_node(), + } + ) + return AssignmentOperationType.get_type(expression.op) + + +def test_vyper_shift_binary_operator_mapping() -> None: + assert _binary_operation_type("Shl") == BinaryOperationType.LEFT_SHIFT + assert _binary_operation_type("Shr") == BinaryOperationType.RIGHT_SHIFT + + +def test_vyper_augmented_assignment_operator_mapping() -> None: + assert _assignment_operation_type("Div") == AssignmentOperationType.ASSIGN_DIVISION + assert _assignment_operation_type("Shl") == AssignmentOperationType.ASSIGN_LEFT_SHIFT + assert _assignment_operation_type("Shr") == AssignmentOperationType.ASSIGN_RIGHT_SHIFT