]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/yapftests/yapf_test_helper.py
1 # Copyright 2016 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 """Support module for tests for yapf."""
20 from yapf
.pytree
import blank_line_calculator
21 from yapf
.pytree
import comment_splicer
22 from yapf
.pytree
import continuation_splicer
23 from yapf
.pytree
import pytree_unwrapper
24 from yapf
.pytree
import pytree_utils
25 from yapf
.pytree
import pytree_visitor
26 from yapf
.pytree
import split_penalty
27 from yapf
.pytree
import subtype_assigner
28 from yapf
.yapflib
import identify_container
29 from yapf
.yapflib
import style
32 class YAPFTest(unittest
.TestCase
):
34 def __init__(self
, *args
):
35 super(YAPFTest
, self
).__init
__(*args
)
37 def assertCodeEqual(self
, expected_code
, code
):
38 if code
!= expected_code
:
39 msg
= ['Code format mismatch:', 'Expected:']
40 linelen
= style
.Get('COLUMN_LIMIT')
41 for line
in expected_code
.splitlines():
42 if len(line
) > linelen
:
43 msg
.append('!> %s' % line
)
45 msg
.append(' > %s' % line
)
47 for line
in code
.splitlines():
48 if len(line
) > linelen
:
49 msg
.append('!> %s' % line
)
51 msg
.append(' > %s' % line
)
56 expected_code
.splitlines(),
60 self
.fail('\n'.join(msg
))
63 def ParseAndUnwrap(code
, dumptree
=False):
64 """Produces logical lines from the given code.
66 Parses the code into a tree, performs comment splicing and runs the
70 code: code to parse as a string
71 dumptree: if True, the parsed pytree (after comment splicing) is dumped
72 to stderr. Useful for debugging.
75 List of logical lines.
77 tree
= pytree_utils
.ParseCodeToTree(code
)
78 comment_splicer
.SpliceComments(tree
)
79 continuation_splicer
.SpliceContinuations(tree
)
80 subtype_assigner
.AssignSubtypes(tree
)
81 identify_container
.IdentifyContainers(tree
)
82 split_penalty
.ComputeSplitPenalties(tree
)
83 blank_line_calculator
.CalculateBlankLines(tree
)
86 pytree_visitor
.DumpPyTree(tree
, target_stream
=sys
.stderr
)
88 llines
= pytree_unwrapper
.UnwrapPyTree(tree
)
90 lline
.CalculateFormattingInformation()