line_joiner_test.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2015 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. """Tests for yapf.line_joiner."""
  15. import textwrap
  16. import unittest
  17. from yapf.yapflib import line_joiner
  18. from yapf.yapflib import style
  19. from yapftests import yapf_test_helper
  20. class LineJoinerTest(yapf_test_helper.YAPFTest):
  21. @classmethod
  22. def setUpClass(cls):
  23. style.SetGlobalStyle(style.CreatePEP8Style())
  24. def _CheckLineJoining(self, code, join_lines):
  25. """Check that the given LogicalLines are joined as expected.
  26. Arguments:
  27. code: The code to check to see if we can join it.
  28. join_lines: True if we expect the lines to be joined.
  29. """
  30. llines = yapf_test_helper.ParseAndUnwrap(code)
  31. self.assertCodeEqual(line_joiner.CanMergeMultipleLines(llines), join_lines)
  32. def testSimpleSingleLineStatement(self):
  33. code = textwrap.dedent("""\
  34. if isinstance(a, int): continue
  35. """)
  36. self._CheckLineJoining(code, join_lines=True)
  37. def testSimpleMultipleLineStatement(self):
  38. code = textwrap.dedent("""\
  39. if isinstance(b, int):
  40. continue
  41. """)
  42. self._CheckLineJoining(code, join_lines=False)
  43. def testSimpleMultipleLineComplexStatement(self):
  44. code = textwrap.dedent("""\
  45. if isinstance(c, int):
  46. while True:
  47. continue
  48. """)
  49. self._CheckLineJoining(code, join_lines=False)
  50. def testSimpleMultipleLineStatementWithComment(self):
  51. code = textwrap.dedent("""\
  52. if isinstance(d, int): continue # We're pleased that d's an int.
  53. """)
  54. self._CheckLineJoining(code, join_lines=True)
  55. def testSimpleMultipleLineStatementWithLargeIndent(self):
  56. code = textwrap.dedent("""\
  57. if isinstance(e, int): continue
  58. """)
  59. self._CheckLineJoining(code, join_lines=True)
  60. def testOverColumnLimit(self):
  61. code = textwrap.dedent("""\
  62. if instance(bbbbbbbbbbbbbbbbbbbbbbbbb, int): cccccccccccccccccccccccccc = ddddddddddddddddddddd
  63. """) # noqa
  64. self._CheckLineJoining(code, join_lines=False)
  65. if __name__ == '__main__':
  66. unittest.main()