|
1 | 1 | from collections import defaultdict |
| 2 | +from textwrap import dedent |
2 | 3 |
|
3 | 4 | import libcst as cst |
| 5 | +from libcst._position import CodePosition, CodeRange |
4 | 6 | from libcst.codemod import CodemodContext |
5 | 7 | from libcst.metadata import PositionProvider |
6 | 8 |
|
@@ -29,6 +31,30 @@ def leave_SimpleStatementLine( |
29 | 31 | return original_node |
30 | 32 |
|
31 | 33 |
|
| 34 | +class AssertPositionCodemod(BaseTransformer): |
| 35 | + METADATA_DEPENDENCIES = (PositionProvider,) |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + context, |
| 40 | + results, |
| 41 | + expected_node_position, |
| 42 | + line_exclude=None, |
| 43 | + line_include=None, |
| 44 | + ): |
| 45 | + BaseTransformer.__init__( |
| 46 | + self, context, results, line_include or [], line_exclude or [] |
| 47 | + ) |
| 48 | + self.expected_node_position = expected_node_position |
| 49 | + |
| 50 | + def leave_FunctionDef( |
| 51 | + self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef |
| 52 | + ) -> cst.FunctionDef: |
| 53 | + pos_to_match = self.node_position(original_node) |
| 54 | + assert pos_to_match == self.expected_node_position |
| 55 | + return updated_node |
| 56 | + |
| 57 | + |
32 | 58 | class TestBaseVisitor: |
33 | 59 | def run_and_assert(self, input_code, expected, line_exclude, line_include): |
34 | 60 | input_tree = cst.parse_module(input_code) |
@@ -59,3 +85,43 @@ def test_includes_excludes(self): |
59 | 85 | line_exclude = [1] |
60 | 86 | line_include = [1] |
61 | 87 | self.run_and_assert(input_code, expected, line_exclude, line_include) |
| 88 | + |
| 89 | + |
| 90 | +class TestNodePosition: |
| 91 | + def run_and_assert(self, input_code, expected_pos): |
| 92 | + input_tree = cst.parse_module(dedent(input_code)) |
| 93 | + command_instance = AssertPositionCodemod( |
| 94 | + CodemodContext(), defaultdict(list), expected_pos |
| 95 | + ) |
| 96 | + command_instance.transform_module(input_tree) |
| 97 | + |
| 98 | + def test_funcdef(self): |
| 99 | + input_code = """ |
| 100 | + def hello(): |
| 101 | + pass |
| 102 | + """ |
| 103 | + expected_pos = CodeRange( |
| 104 | + start=CodePosition(line=2, column=0), end=CodePosition(line=2, column=11) |
| 105 | + ) |
| 106 | + self.run_and_assert(input_code, expected_pos) |
| 107 | + |
| 108 | + def test_instance(self): |
| 109 | + input_code = """ |
| 110 | + class MyClass: |
| 111 | + def instance_method(): |
| 112 | + print("instance_method") |
| 113 | + """ |
| 114 | + expected_pos = CodeRange( |
| 115 | + start=CodePosition(line=3, column=4), end=CodePosition(line=3, column=25) |
| 116 | + ) |
| 117 | + self.run_and_assert(input_code, expected_pos) |
| 118 | + |
| 119 | + def test_funcdef_args(self): |
| 120 | + input_code = """ |
| 121 | + def hello(one, *args, **kwargs): |
| 122 | + pass |
| 123 | + """ |
| 124 | + expected_pos = CodeRange( |
| 125 | + start=CodePosition(line=2, column=0), end=CodePosition(line=2, column=31) |
| 126 | + ) |
| 127 | + self.run_and_assert(input_code, expected_pos) |
0 commit comments