logical_line_test.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.logical_line."""
  15. import textwrap
  16. import unittest
  17. from yapf_third_party._ylib2to3 import pytree
  18. from yapf_third_party._ylib2to3.pgen2 import token
  19. from yapf.pytree import split_penalty
  20. from yapf.yapflib import format_token
  21. from yapf.yapflib import logical_line
  22. from yapftests import yapf_test_helper
  23. class LogicalLineBasicTest(unittest.TestCase):
  24. def testConstruction(self):
  25. toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),
  26. (token.VBAR, '|', 'VBAR')])
  27. lline = logical_line.LogicalLine(20, toks)
  28. self.assertEqual(20, lline.depth)
  29. self.assertEqual(['DOT', 'VBAR'], [tok.name for tok in lline.tokens])
  30. def testFirstLast(self):
  31. toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),
  32. (token.LPAR, '(', 'LPAR'),
  33. (token.VBAR, '|', 'VBAR')])
  34. lline = logical_line.LogicalLine(20, toks)
  35. self.assertEqual(20, lline.depth)
  36. self.assertEqual('DOT', lline.first.name)
  37. self.assertEqual('VBAR', lline.last.name)
  38. def testAsCode(self):
  39. toks = _MakeFormatTokenList([(token.DOT, '.', 'DOT'),
  40. (token.LPAR, '(', 'LPAR'),
  41. (token.VBAR, '|', 'VBAR')])
  42. lline = logical_line.LogicalLine(2, toks)
  43. self.assertEqual(' . ( |', lline.AsCode())
  44. def testAppendToken(self):
  45. lline = logical_line.LogicalLine(0)
  46. lline.AppendToken(_MakeFormatTokenLeaf(token.LPAR, '(', 'LPAR'))
  47. lline.AppendToken(_MakeFormatTokenLeaf(token.RPAR, ')', 'RPAR'))
  48. self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in lline.tokens])
  49. class LogicalLineFormattingInformationTest(yapf_test_helper.YAPFTest):
  50. def testFuncDef(self):
  51. code = textwrap.dedent(r"""
  52. def f(a, b):
  53. pass
  54. """)
  55. llines = yapf_test_helper.ParseAndUnwrap(code)
  56. f = llines[0].tokens[1]
  57. self.assertFalse(f.can_break_before)
  58. self.assertFalse(f.must_break_before)
  59. self.assertEqual(f.split_penalty, split_penalty.UNBREAKABLE)
  60. lparen = llines[0].tokens[2]
  61. self.assertFalse(lparen.can_break_before)
  62. self.assertFalse(lparen.must_break_before)
  63. self.assertEqual(lparen.split_penalty, split_penalty.UNBREAKABLE)
  64. def _MakeFormatTokenLeaf(token_type, token_value, name):
  65. return format_token.FormatToken(pytree.Leaf(token_type, token_value), name)
  66. def _MakeFormatTokenList(token_type_values):
  67. return [
  68. _MakeFormatTokenLeaf(token_type, token_value, token_name)
  69. for token_type, token_value, token_name in token_type_values
  70. ]
  71. if __name__ == '__main__':
  72. unittest.main()