• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 Google Inc. All Rights Reserved.
2#
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
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
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."""
15
16import difflib
17import sys
18import unittest
19
20from yapf.yapflib import blank_line_calculator
21from yapf.yapflib import comment_splicer
22from yapf.yapflib import continuation_splicer
23from yapf.yapflib import identify_container
24from yapf.yapflib import pytree_unwrapper
25from yapf.yapflib import pytree_utils
26from yapf.yapflib import pytree_visitor
27from yapf.yapflib import split_penalty
28from yapf.yapflib import style
29from yapf.yapflib import subtype_assigner
30
31
32class YAPFTest(unittest.TestCase):
33
34  def assertCodeEqual(self, expected_code, code):
35    if code != expected_code:
36      msg = ['Code format mismatch:', 'Expected:']
37      linelen = style.Get('COLUMN_LIMIT')
38      for l in expected_code.splitlines():
39        if len(l) > linelen:
40          msg.append('!> %s' % l)
41        else:
42          msg.append(' > %s' % l)
43      msg.append('Actual:')
44      for l in code.splitlines():
45        if len(l) > linelen:
46          msg.append('!> %s' % l)
47        else:
48          msg.append(' > %s' % l)
49      msg.append('Diff:')
50      msg.extend(
51          difflib.unified_diff(
52              code.splitlines(),
53              expected_code.splitlines(),
54              fromfile='actual',
55              tofile='expected',
56              lineterm=''))
57      self.fail('\n'.join(msg))
58
59
60def ParseAndUnwrap(code, dumptree=False):
61  """Produces unwrapped lines from the given code.
62
63  Parses the code into a tree, performs comment splicing and runs the
64  unwrapper.
65
66  Arguments:
67    code: code to parse as a string
68    dumptree: if True, the parsed pytree (after comment splicing) is dumped
69              to stderr. Useful for debugging.
70
71  Returns:
72    List of unwrapped lines.
73  """
74  tree = pytree_utils.ParseCodeToTree(code)
75  comment_splicer.SpliceComments(tree)
76  continuation_splicer.SpliceContinuations(tree)
77  subtype_assigner.AssignSubtypes(tree)
78  identify_container.IdentifyContainers(tree)
79  split_penalty.ComputeSplitPenalties(tree)
80  blank_line_calculator.CalculateBlankLines(tree)
81
82  if dumptree:
83    pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)
84
85  uwlines = pytree_unwrapper.UnwrapPyTree(tree)
86  for uwl in uwlines:
87    uwl.CalculateFormattingInformation()
88
89  return uwlines
90