-
Notifications
You must be signed in to change notification settings - Fork 176
/
test_footer_behavior.py
232 lines (228 loc) · 10.4 KB
/
test_footer_behavior.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from textwrap import dedent
import libcst as cst
from libcst import parse_module
from libcst._nodes.deep_equals import deep_equals
from libcst.testing.utils import data_provider, UnitTest
class FooterBehaviorTest(UnitTest):
@data_provider(
{
# Literally the most basic example
"simple_module": {
"code": "",
"expected_module": cst.Module(body=(), has_trailing_newline=False),
},
# A module with a header comment
"header_only_module": {
"code": "# This is a header comment\n",
"expected_module": cst.Module(
header=[
cst.EmptyLine(
comment=cst.Comment(value="# This is a header comment")
)
],
body=[],
),
},
# A module with a header and footer
"simple_header_footer_module": {
"code": "# This is a header comment\npass\n# This is a footer comment\n",
"expected_module": cst.Module(
header=[
cst.EmptyLine(
comment=cst.Comment(value="# This is a header comment")
)
],
body=[cst.SimpleStatementLine([cst.Pass()])],
footer=[
cst.EmptyLine(
comment=cst.Comment(value="# This is a footer comment")
)
],
),
},
# A module which should have a footer comment taken from the
# if statement's indented block.
"simple_reparented_footer_module": {
"code": "# This is a header comment\nif True:\n pass\n# This is a footer comment\n",
"expected_module": cst.Module(
header=[
cst.EmptyLine(
comment=cst.Comment(value="# This is a header comment")
)
],
body=[
cst.If(
test=cst.Name(value="True"),
body=cst.IndentedBlock(
header=cst.TrailingWhitespace(),
body=[
cst.SimpleStatementLine(
body=[cst.Pass()],
trailing_whitespace=cst.TrailingWhitespace(),
)
],
),
)
],
footer=[
cst.EmptyLine(
comment=cst.Comment(value="# This is a footer comment")
)
],
),
},
# Verifying that we properly parse and spread out footer comments to the
# relative indents they go with.
"complex_reparented_footer_module": {
"code": (
"# This is a header comment\nif True:\n if True:\n pass"
+ "\n # This is an inner indented block comment\n # This "
+ "is an outer indented block comment\n# This is a footer comment\n"
),
"expected_module": cst.Module(
body=[
cst.If(
test=cst.Name(value="True"),
body=cst.IndentedBlock(
body=[
cst.If(
test=cst.Name(value="True"),
body=cst.IndentedBlock(
body=[
cst.SimpleStatementLine(
body=[cst.Pass()]
)
],
footer=[
cst.EmptyLine(
comment=cst.Comment(
value="# This is an inner indented block comment"
)
)
],
),
)
],
footer=[
cst.EmptyLine(
comment=cst.Comment(
value="# This is an outer indented block comment"
)
)
],
),
)
],
header=[
cst.EmptyLine(
comment=cst.Comment(value="# This is a header comment")
)
],
footer=[
cst.EmptyLine(
comment=cst.Comment(value="# This is a footer comment")
)
],
),
},
# Verify that comments belonging to statements are still owned even
# after an indented block.
"statement_comment_reparent": {
"code": "if foo:\n return\n# comment\nx = 7\n",
"expected_module": cst.Module(
body=[
cst.If(
test=cst.Name(value="foo"),
body=cst.IndentedBlock(
body=[
cst.SimpleStatementLine(
body=[
cst.Return(
whitespace_after_return=cst.SimpleWhitespace(
value=""
)
)
]
)
]
),
),
cst.SimpleStatementLine(
body=[
cst.Assign(
targets=[
cst.AssignTarget(target=cst.Name(value="x"))
],
value=cst.Integer(value="7"),
)
],
leading_lines=[
cst.EmptyLine(comment=cst.Comment(value="# comment"))
],
),
]
),
},
# Verify that even if there are completely empty lines, we give all lines
# up to and including the last line that's indented correctly. That way
# comments that line up with indented block's indentation level aren't
# parented to the next line just because there's a blank line or two
# between them.
"statement_comment_with_empty_lines": {
"code": (
"def foo():\n if True:\n pass\n\n # Empty "
+ "line before me\n\n else:\n pass\n"
),
"expected_module": cst.Module(
body=[
cst.FunctionDef(
name=cst.Name(value="foo"),
params=cst.Parameters(),
body=cst.IndentedBlock(
body=[
cst.If(
test=cst.Name(value="True"),
body=cst.IndentedBlock(
body=[
cst.SimpleStatementLine(
body=[cst.Pass()]
)
],
footer=[
cst.EmptyLine(indent=False),
cst.EmptyLine(
comment=cst.Comment(
value="# Empty line before me"
)
),
],
),
orelse=cst.Else(
body=cst.IndentedBlock(
body=[
cst.SimpleStatementLine(
body=[cst.Pass()]
)
]
),
leading_lines=[cst.EmptyLine(indent=False)],
),
)
]
),
)
]
),
},
}
)
def test_parsers(self, code: str, expected_module: cst.CSTNode) -> None:
parsed_module = parse_module(dedent(code))
self.assertTrue(
deep_equals(parsed_module, expected_module),
msg=f"\n{parsed_module!r}\nis not deeply equal to \n{expected_module!r}",
)