xref: /aosp_15_r20/external/yapf/yapftests/format_token_test.py (revision 7249d1a64f4850ccf838e62a46276f891f72998e)
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
16import unittest
17
18from lib2to3 import pytree
19from lib2to3.pgen2 import token
20
21from yapf.yapflib import format_token
22
23
24class TabbedContinuationAlignPaddingTest(unittest.TestCase):
25
26  def testSpace(self):
27    align_style = 'SPACE'
28
29    pad = format_token._TabbedContinuationAlignPadding(0, align_style, 2)
30    self.assertEqual(pad, '')
31
32    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 2)
33    self.assertEqual(pad, ' ' * 2)
34
35    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 2)
36    self.assertEqual(pad, ' ' * 5)
37
38  def testFixed(self):
39    align_style = 'FIXED'
40
41    pad = format_token._TabbedContinuationAlignPadding(0, align_style, 4)
42    self.assertEqual(pad, '')
43
44    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)
45    self.assertEqual(pad, '\t')
46
47    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)
48    self.assertEqual(pad, '\t' * 2)
49
50  def testVAlignRight(self):
51    align_style = 'VALIGN-RIGHT'
52
53    pad = format_token._TabbedContinuationAlignPadding(0, align_style, 4)
54    self.assertEqual(pad, '')
55
56    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4)
57    self.assertEqual(pad, '\t')
58
59    pad = format_token._TabbedContinuationAlignPadding(4, align_style, 4)
60    self.assertEqual(pad, '\t')
61
62    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4)
63    self.assertEqual(pad, '\t' * 2)
64
65
66class FormatTokenTest(unittest.TestCase):
67
68  def testSimple(self):
69    tok = format_token.FormatToken(pytree.Leaf(token.STRING, "'hello world'"))
70    self.assertEqual(
71        "FormatToken(name=DOCSTRING, value='hello world', column=0, "
72        "lineno=0, splitpenalty=0)", str(tok))
73    self.assertTrue(tok.is_string)
74
75    tok = format_token.FormatToken(pytree.Leaf(token.COMMENT, '# A comment'))
76    self.assertEqual(
77        'FormatToken(name=COMMENT, value=# A comment, column=0, '
78        'lineno=0, splitpenalty=0)', str(tok))
79    self.assertTrue(tok.is_comment)
80
81  def testIsMultilineString(self):
82    tok = format_token.FormatToken(pytree.Leaf(token.STRING, '"""hello"""'))
83    self.assertTrue(tok.is_multiline_string)
84
85    tok = format_token.FormatToken(pytree.Leaf(token.STRING, 'r"""hello"""'))
86    self.assertTrue(tok.is_multiline_string)
87
88
89if __name__ == '__main__':
90  unittest.main()
91