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