]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/yapftests/split_penalty_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.split_penalty."""
20 from yapf_third_party
._ylib
2to
3 import pytree
22 from yapf
.pytree
import pytree_utils
23 from yapf
.pytree
import pytree_visitor
24 from yapf
.pytree
import split_penalty
25 from yapf
.yapflib
import style
27 from yapftests
import yapf_test_helper
29 UNBREAKABLE
= split_penalty
.UNBREAKABLE
30 VERY_STRONGLY_CONNECTED
= split_penalty
.VERY_STRONGLY_CONNECTED
31 DOTTED_NAME
= split_penalty
.DOTTED_NAME
32 STRONGLY_CONNECTED
= split_penalty
.STRONGLY_CONNECTED
35 class SplitPenaltyTest(yapf_test_helper
.YAPFTest
):
39 style
.SetGlobalStyle(style
.CreateYapfStyle())
41 def _ParseAndComputePenalties(self
, code
, dumptree
=False):
42 """Parses the code and computes split penalties.
45 code: code to parse as a string
46 dumptree: if True, the parsed pytree (after penalty assignment) is dumped
47 to stderr. Useful for debugging.
52 tree
= pytree_utils
.ParseCodeToTree(code
)
53 split_penalty
.ComputeSplitPenalties(tree
)
55 pytree_visitor
.DumpPyTree(tree
, target_stream
=sys
.stderr
)
58 def _CheckPenalties(self
, tree
, list_of_expected
):
59 """Check that the tokens in the tree have the correct penalties.
63 list_of_expected: list of (name, penalty) pairs. Non-semantic tokens are
64 filtered out from the expected values.
68 if pytree_utils
.NodeName(tree
) in pytree_utils
.NONSEMANTIC_TOKENS
:
70 if isinstance(tree
, pytree
.Leaf
):
72 pytree_utils
.GetNodeAnnotation(
73 tree
, pytree_utils
.Annotation
.SPLIT_PENALTY
))]
75 for node
in tree
.children
:
76 nodes
+= FlattenRec(node
)
79 self
.assertEqual(list_of_expected
, FlattenRec(tree
))
81 def testUnbreakable(self
):
82 # Test function definitions.
83 code
= textwrap
.dedent("""\
87 tree
= self
._ParseAndComputePenalties
(code
)
88 self
._CheckPenalties
(tree
, [
93 (')', STRONGLY_CONNECTED
),
98 # Test function definition with trailing comment.
99 code
= textwrap
.dedent("""\
100 def foo(x): # trailing comment
103 tree
= self
._ParseAndComputePenalties
(code
)
104 self
._CheckPenalties
(tree
, [
106 ('foo', UNBREAKABLE
),
109 (')', STRONGLY_CONNECTED
),
114 # Test class definitions.
115 code
= textwrap
.dedent("""\
121 tree
= self
._ParseAndComputePenalties
(code
)
122 self
._CheckPenalties
(tree
, [
136 # Test lambda definitions.
137 code
= textwrap
.dedent("""\
140 tree
= self
._ParseAndComputePenalties
(code
)
141 self
._CheckPenalties
(tree
, [
143 ('a', VERY_STRONGLY_CONNECTED
),
144 (',', VERY_STRONGLY_CONNECTED
),
145 ('b', VERY_STRONGLY_CONNECTED
),
146 (':', VERY_STRONGLY_CONNECTED
),
147 ('None', VERY_STRONGLY_CONNECTED
),
151 code
= textwrap
.dedent("""\
154 tree
= self
._ParseAndComputePenalties
(code
)
155 self
._CheckPenalties
(tree
, [
164 def testStronglyConnected(self
):
165 # Test dictionary keys.
166 code
= textwrap
.dedent("""\
172 tree
= self
._ParseAndComputePenalties
(code
)
173 self
._CheckPenalties
(tree
, [
178 (':', STRONGLY_CONNECTED
),
183 ('lambda', STRONGLY_CONNECTED
),
184 ('a', VERY_STRONGLY_CONNECTED
),
185 (':', VERY_STRONGLY_CONNECTED
),
186 ('23', VERY_STRONGLY_CONNECTED
),
187 (')', VERY_STRONGLY_CONNECTED
),
188 (':', STRONGLY_CONNECTED
),
194 # Test list comprehension.
195 code
= textwrap
.dedent("""\
196 [a for a in foo if a.x == 37]
198 tree
= self
._ParseAndComputePenalties
(code
)
199 self
._CheckPenalties
(tree
, [
203 ('a', STRONGLY_CONNECTED
),
204 ('in', STRONGLY_CONNECTED
),
205 ('foo', STRONGLY_CONNECTED
),
207 ('a', STRONGLY_CONNECTED
),
208 ('.', VERY_STRONGLY_CONNECTED
),
210 ('==', STRONGLY_CONNECTED
),
211 ('37', STRONGLY_CONNECTED
),
215 def testFuncCalls(self
):
216 code
= textwrap
.dedent("""\
219 tree
= self
._ParseAndComputePenalties
(code
)
220 self
._CheckPenalties
(tree
, [
228 (')', VERY_STRONGLY_CONNECTED
),
231 # Now a method call, which has more than one trailer
232 code
= textwrap
.dedent("""\
235 tree
= self
._ParseAndComputePenalties
(code
)
236 self
._CheckPenalties
(tree
, [
238 ('.', VERY_STRONGLY_CONNECTED
),
239 ('bar', DOTTED_NAME
),
240 ('.', VERY_STRONGLY_CONNECTED
),
241 ('baz', DOTTED_NAME
),
242 ('(', STRONGLY_CONNECTED
),
248 (')', VERY_STRONGLY_CONNECTED
),
251 # Test single generator argument.
252 code
= textwrap
.dedent("""\
253 max(i for i in xrange(10))
255 tree
= self
._ParseAndComputePenalties
(code
)
256 self
._CheckPenalties
(tree
, [
261 ('i', STRONGLY_CONNECTED
),
262 ('in', STRONGLY_CONNECTED
),
263 ('xrange', STRONGLY_CONNECTED
),
265 ('10', STRONGLY_CONNECTED
),
266 (')', VERY_STRONGLY_CONNECTED
),
267 (')', VERY_STRONGLY_CONNECTED
),
271 if __name__
== '__main__':