]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/yapftests/pytree_unwrapper_test.py
1 # Copyright 2015 Google Inc. All Rights Reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Tests for yapf.pytree_unwrapper."""
19 from yapf
.pytree
import pytree_utils
21 from yapftests
import yapf_test_helper
24 class PytreeUnwrapperTest(yapf_test_helper
.YAPFTest
):
26 def _CheckLogicalLines(self
, llines
, list_of_expected
):
27 """Check that the given LogicalLines match expectations.
30 llines: list of LogicalLine
31 list_of_expected: list of (depth, values) pairs. Non-semantic tokens are
32 filtered out from the expected values.
38 for ft
in lline
.tokens
39 if ft
.name
not in pytree_utils
.NONSEMANTIC_TOKENS
41 actual
.append((lline
.depth
, filtered_values
))
43 self
.assertEqual(list_of_expected
, actual
)
45 def testSimpleFileScope(self
):
46 code
= textwrap
.dedent("""\
51 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
52 self
._CheckLogicalLines
(llines
, [
58 def testSimpleMultilineStatement(self
):
59 code
= textwrap
.dedent("""\
63 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
64 self
._CheckLogicalLines
(llines
, [
65 (0, ['y', '=', '(', '1', '+', 'x', ')']),
68 def testFileScopeWithInlineComment(self
):
69 code
= textwrap
.dedent("""\
73 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
74 self
._CheckLogicalLines
(llines
, [
75 (0, ['x', '=', '1', '# a comment']),
79 def testSimpleIf(self
):
80 code
= textwrap
.dedent("""\
85 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
86 self
._CheckLogicalLines
(llines
, [
87 (0, ['if', 'foo', ':']),
92 def testSimpleIfWithComments(self
):
93 code
= textwrap
.dedent("""\
99 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
100 self
._CheckLogicalLines
(llines
, [
102 (0, ['if', 'foo', ':', '# c2']),
103 (1, ['x', '=', '1']),
104 (1, ['y', '=', '2']),
107 def testIfWithCommentsInside(self
):
108 code
= textwrap
.dedent("""\
115 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
116 self
._CheckLogicalLines
(llines
, [
117 (0, ['if', 'foo', ':']),
119 (1, ['x', '=', '1', '# c2']),
121 (1, ['y', '=', '2']),
124 def testIfElifElse(self
):
125 code
= textwrap
.dedent("""\
134 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
135 self
._CheckLogicalLines
(llines
, [
136 (0, ['if', 'x', ':']),
137 (1, ['x', '=', '1', '# c1']),
138 (0, ['elif', 'y', ':', '# c2']),
139 (1, ['y', '=', '1']),
142 (1, ['z', '=', '1']),
145 def testNestedCompoundTwoLevel(self
):
146 code
= textwrap
.dedent("""\
154 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
155 self
._CheckLogicalLines
(llines
, [
156 (0, ['if', 'x', ':']),
157 (1, ['x', '=', '1', '# c1']),
158 (1, ['while', 't', ':']),
160 (2, ['j', '=', '1']),
161 (1, ['k', '=', '1']),
164 def testSimpleWhile(self
):
165 code
= textwrap
.dedent("""\
170 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
171 self
._CheckLogicalLines
(llines
, [
172 (0, ['while', 'x', '>', '1', ':', '# c1']),
174 (1, ['x', '=', '1']),
177 def testSimpleTry(self
):
178 code
= textwrap
.dedent("""\
190 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
191 self
._CheckLogicalLines
(llines
, [
194 (0, ['except', ':']),
196 (0, ['except', ':']),
200 (0, ['finally', ':']),
204 def testSimpleFuncdef(self
):
205 code
= textwrap
.dedent("""\
210 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
211 self
._CheckLogicalLines
(llines
, [
212 (0, ['def', 'foo', '(', 'x', ')', ':', '# c1']),
214 (1, ['return', 'x']),
217 def testTwoFuncDefs(self
):
218 code
= textwrap
.dedent("""\
227 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
228 self
._CheckLogicalLines
(llines
, [
229 (0, ['def', 'foo', '(', 'x', ')', ':', '# c1']),
231 (1, ['return', 'x']),
232 (0, ['def', 'bar', '(', ')', ':', '# c3']),
234 (1, ['return', 'x']),
237 def testSimpleClassDef(self
):
238 code
= textwrap
.dedent("""\
243 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
244 self
._CheckLogicalLines
(llines
, [
245 (0, ['class', 'Klass', ':', '# c1']),
247 (1, ['p', '=', '1']),
250 def testSingleLineStmtInFunc(self
):
251 code
= textwrap
.dedent("""\
254 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
255 self
._CheckLogicalLines
(llines
, [
256 (0, ['def', 'f', '(', ')', ':']),
257 (1, ['return', '37']),
260 def testMultipleComments(self
):
261 code
= textwrap
.dedent("""\
268 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
269 self
._CheckLogicalLines
(llines
, [
270 (0, ['# Comment #1']),
271 (0, ['# Comment #2']),
272 (0, ['def', 'f', '(', ')', ':']),
276 def testSplitListWithComment(self
):
277 code
= textwrap
.dedent("""\
284 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
285 self
._CheckLogicalLines
(llines
, [(0, [
286 'a', '=', '[', "'a'", ',', "'b'", ',', "'c'", ',', '# hello world', ']'
290 class MatchBracketsTest(yapf_test_helper
.YAPFTest
):
292 def _CheckMatchingBrackets(self
, llines
, list_of_expected
):
293 """Check that the tokens have the expected matching bracket.
296 llines: list of LogicalLine.
297 list_of_expected: list of (index, index) pairs. The matching brackets at
298 the indexes need to match. Non-semantic tokens are filtered out from the
303 filtered_values
= [(ft
, ft
.matching_bracket
)
304 for ft
in lline
.tokens
305 if ft
.name
not in pytree_utils
.NONSEMANTIC_TOKENS
]
307 actual
.append(filtered_values
)
309 for index
, bracket_list
in enumerate(list_of_expected
):
310 lline
= actual
[index
]
313 self
.assertIsNone(value
[1])
315 for open_bracket
, close_bracket
in bracket_list
:
316 self
.assertEqual(lline
[open_bracket
][0], lline
[close_bracket
][1])
317 self
.assertEqual(lline
[close_bracket
][0], lline
[open_bracket
][1])
319 def testFunctionDef(self
):
320 code
= textwrap
.dedent("""\
321 def foo(a, b=['w','d'], c=[42, 37]):
324 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
325 self
._CheckMatchingBrackets
(llines
, [
326 [(2, 20), (7, 11), (15, 19)],
330 def testDecorator(self
):
331 code
= textwrap
.dedent("""\
336 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
337 self
._CheckMatchingBrackets
(llines
, [
343 def testClassDef(self
):
344 code
= textwrap
.dedent("""\
348 llines
= yapf_test_helper
.ParseAndUnwrap(code
)
349 self
._CheckMatchingBrackets
(llines
, [
355 if __name__
== '__main__':