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, 4)
30    self.assertEqual(pad, '')
31
32    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 2, 4)
33    self.assertEqual(pad, ' ' * 2)
34
35    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 2, 4)
36    self.assertEqual(pad, ' ' * 5)
37
38  def testFixed(self):
39    align_style = 'FIXED'
40
41    pad = format_token._TabbedContinuationAlignPadding(0, align_style, 4, 8)
42    self.assertEqual(pad, '')
43
44    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4, 8)
45    self.assertEqual(pad, '\t' * 2)
46
47    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4, 8)
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, 8)
54    self.assertEqual(pad, '')
55
56    pad = format_token._TabbedContinuationAlignPadding(2, align_style, 4, 8)
57    self.assertEqual(pad, '\t')
58
59    pad = format_token._TabbedContinuationAlignPadding(4, align_style, 4, 8)
60    self.assertEqual(pad, '\t')
61
62    pad = format_token._TabbedContinuationAlignPadding(5, align_style, 4, 8)
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("FormatToken(name=STRING, value='hello world')", str(tok))
71    self.assertTrue(tok.is_string)
72
73    tok = format_token.FormatToken(pytree.Leaf(token.COMMENT, '# A comment'))
74    self.assertEqual('FormatToken(name=COMMENT, value=# A comment)', str(tok))
75    self.assertTrue(tok.is_comment)
76
77  def testIsMultilineString(self):
78    tok = format_token.FormatToken(pytree.Leaf(token.STRING, '"""hello"""'))
79    self.assertTrue(tok.is_multiline_string)
80
81    tok = format_token.FormatToken(pytree.Leaf(token.STRING, 'r"""hello"""'))
82    self.assertTrue(tok.is_multiline_string)
83
84
85if __name__ == '__main__':
86  unittest.main()
87