Repository navigation
Expand file tree
/
Copy pathtest_ancestorpatterns_mixin.py
More file actions
93 lines (76 loc) · 2.92 KB
/
Copy pathtest_ancestorpatterns_mixin.py
File metadata and controls
93 lines (76 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from textwrap import dedent
import libcst as cst
from libcst.codemod import Codemod, CodemodContext
from codemodder.codemods.utils_mixin import AncestorPatternsMixin
class TestNameResolutionMixin:
def test_get_parent(self):
class TestCodemod(Codemod, AncestorPatternsMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
maybe_parent = self.get_parent(stmt)
assert maybe_parent == tree
return tree
input_code = dedent(
"""\
a = 1
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)
def test_get_parent_root(self):
class TestCodemod(Codemod, AncestorPatternsMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
maybe_parent = self.get_parent(tree)
assert maybe_parent is None
return tree
input_code = dedent(
"""\
a = 1
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)
def test_get_path_to_root(self):
class TestCodemod(Codemod, AncestorPatternsMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
node = stmt.body[0]
path = self.path_to_root(node)
assert len(path) == 2
return tree
input_code = dedent(
"""\
a = 1
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)
def test_get_path_to_root_set(self):
class TestCodemod(Codemod, AncestorPatternsMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
node = stmt.body[0]
path = self.path_to_root_as_set(node)
assert len(path) == 3
return tree
input_code = dedent(
"""\
a = 1
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)
def test_is_ancestor(self):
class TestCodemod(Codemod, AncestorPatternsMixin):
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
stmt = cst.ensure_type(tree.body[-1], cst.SimpleStatementLine)
node = stmt.body[0]
assert self.is_ancestor(node, tree)
return tree
input_code = dedent(
"""\
a = 1
"""
)
tree = cst.parse_module(input_code)
TestCodemod(CodemodContext()).transform_module(tree)