yapf_test_helper.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. import difflib
  16. import sys
  17. import unittest
  18. from yapf.pytree import blank_line_calculator
  19. from yapf.pytree import comment_splicer
  20. from yapf.pytree import continuation_splicer
  21. from yapf.pytree import pytree_unwrapper
  22. from yapf.pytree import pytree_utils
  23. from yapf.pytree import pytree_visitor
  24. from yapf.pytree import split_penalty
  25. from yapf.pytree import subtype_assigner
  26. from yapf.yapflib import identify_container
  27. from yapf.yapflib import style
  28. class YAPFTest(unittest.TestCase):
  29. def __init__(self, *args):
  30. super(YAPFTest, self).__init__(*args)
  31. def assertCodeEqual(self, expected_code, code):
  32. if code != expected_code:
  33. msg = ['Code format mismatch:', 'Expected:']
  34. linelen = style.Get('COLUMN_LIMIT')
  35. for line in expected_code.splitlines():
  36. if len(line) > linelen:
  37. msg.append('!> %s' % line)
  38. else:
  39. msg.append(' > %s' % line)
  40. msg.append('Actual:')
  41. for line in code.splitlines():
  42. if len(line) > linelen:
  43. msg.append('!> %s' % line)
  44. else:
  45. msg.append(' > %s' % line)
  46. msg.append('Diff:')
  47. msg.extend(
  48. difflib.unified_diff(
  49. code.splitlines(),
  50. expected_code.splitlines(),
  51. fromfile='actual',
  52. tofile='expected',
  53. lineterm=''))
  54. self.fail('\n'.join(msg))
  55. def ParseAndUnwrap(code, dumptree=False):
  56. """Produces logical lines from the given code.
  57. Parses the code into a tree, performs comment splicing and runs the
  58. unwrapper.
  59. Arguments:
  60. code: code to parse as a string
  61. dumptree: if True, the parsed pytree (after comment splicing) is dumped
  62. to stderr. Useful for debugging.
  63. Returns:
  64. List of logical lines.
  65. """
  66. tree = pytree_utils.ParseCodeToTree(code)
  67. comment_splicer.SpliceComments(tree)
  68. continuation_splicer.SpliceContinuations(tree)
  69. subtype_assigner.AssignSubtypes(tree)
  70. identify_container.IdentifyContainers(tree)
  71. split_penalty.ComputeSplitPenalties(tree)
  72. blank_line_calculator.CalculateBlankLines(tree)
  73. if dumptree:
  74. pytree_visitor.DumpPyTree(tree, target_stream=sys.stderr)
  75. llines = pytree_unwrapper.UnwrapPyTree(tree)
  76. for lline in llines:
  77. lline.CalculateFormattingInformation()
  78. return llines