format_token_test.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.format_token."""
  15. import unittest
  16. from yapf_third_party._ylib2to3 import pytree
  17. from yapf_third_party._ylib2to3.pgen2 import token
  18. from yapf.yapflib import format_token
  19. class TabbedContinuationAlignPaddingTest(unittest.TestCase):
  20. def testSpace(self):
  21. align_style = 'SPACE'
  22. pad = format_token._TabbedContinuationAlignPadding(0, align_style, 2)
  23. self.assertEqual(pad, '')
  24. pad = format_token._TabbedContinuationAlignPadding(2, align_style, 2)
  25. self.assertEqual(pad, ' ' * 2)
  26. pad = format_token._TabbedContinuationAlignPadding(5, align_style, 2)
  27. self.assertEqual(pad, ' ' * 5)
  28. def testFixed(self):
  29. align_style = 'FIXED'
  30. pad = format_token._TabbedContinuationAlignPadding(0, align_style, 4)
  31. self.assertEqual(pad, '')
  32. pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)
  33. self.assertEqual(pad, '\t')
  34. pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)
  35. self.assertEqual(pad, '\t' * 2)
  36. def testVAlignRight(self):
  37. align_style = 'VALIGN-RIGHT'
  38. pad = format_token._TabbedContinuationAlignPadding(0, align_style, 4)
  39. self.assertEqual(pad, '')
  40. pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)
  41. self.assertEqual(pad, '\t')
  42. pad = format_token._TabbedContinuationAlignPadding(4, align_style, 4)
  43. self.assertEqual(pad, '\t')
  44. pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)
  45. self.assertEqual(pad, '\t' * 2)
  46. class FormatTokenTest(unittest.TestCase):
  47. def testSimple(self):
  48. tok = format_token.FormatToken(
  49. pytree.Leaf(token.STRING, "'hello world'"), 'STRING')
  50. self.assertEqual(
  51. "FormatToken(name=DOCSTRING, value='hello world', column=0, "
  52. 'lineno=0, splitpenalty=0)', str(tok))
  53. self.assertTrue(tok.is_string)
  54. tok = format_token.FormatToken(
  55. pytree.Leaf(token.COMMENT, '# A comment'), 'COMMENT')
  56. self.assertEqual(
  57. 'FormatToken(name=COMMENT, value=# A comment, column=0, '
  58. 'lineno=0, splitpenalty=0)', str(tok))
  59. self.assertTrue(tok.is_comment)
  60. def testIsMultilineString(self):
  61. tok = format_token.FormatToken(
  62. pytree.Leaf(token.STRING, '"""hello"""'), 'STRING')
  63. self.assertTrue(tok.is_multiline_string)
  64. tok = format_token.FormatToken(
  65. pytree.Leaf(token.STRING, 'r"""hello"""'), 'STRING')
  66. self.assertTrue(tok.is_multiline_string)
  67. if __name__ == '__main__':
  68. unittest.main()