• 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.unwrapped_line."""
15
16import textwrap
17import unittest
18
19from lib2to3 import pytree
20from lib2to3.pgen2 import token
21
22from yapf.yapflib import format_token
23from yapf.yapflib import split_penalty
24from yapf.yapflib import unwrapped_line
25
26from yapftests import yapf_test_helper
27
28
29class UnwrappedLineBasicTest(unittest.TestCase):
30
31  def testConstruction(self):
32    toks = _MakeFormatTokenList([(token.DOT, '.'), (token.VBAR, '|')])
33    uwl = unwrapped_line.UnwrappedLine(20, toks)
34    self.assertEqual(20, uwl.depth)
35    self.assertEqual(['DOT', 'VBAR'], [tok.name for tok in uwl.tokens])
36
37  def testFirstLast(self):
38    toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('),
39                                 (token.VBAR, '|')])
40    uwl = unwrapped_line.UnwrappedLine(20, toks)
41    self.assertEqual(20, uwl.depth)
42    self.assertEqual('DOT', uwl.first.name)
43    self.assertEqual('VBAR', uwl.last.name)
44
45  def testAsCode(self):
46    toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('),
47                                 (token.VBAR, '|')])
48    uwl = unwrapped_line.UnwrappedLine(2, toks)
49    self.assertEqual('    . ( |', uwl.AsCode())
50
51  def testAppendToken(self):
52    uwl = unwrapped_line.UnwrappedLine(0)
53    uwl.AppendToken(_MakeFormatTokenLeaf(token.LPAR, '('))
54    uwl.AppendToken(_MakeFormatTokenLeaf(token.RPAR, ')'))
55    self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in uwl.tokens])
56
57  def testAppendNode(self):
58    uwl = unwrapped_line.UnwrappedLine(0)
59    uwl.AppendNode(pytree.Leaf(token.LPAR, '('))
60    uwl.AppendNode(pytree.Leaf(token.RPAR, ')'))
61    self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in uwl.tokens])
62
63
64class UnwrappedLineFormattingInformationTest(yapf_test_helper.YAPFTest):
65
66  def testFuncDef(self):
67    code = textwrap.dedent(r"""
68        def f(a, b):
69          pass
70        """)
71    uwlines = yapf_test_helper.ParseAndUnwrap(code)
72
73    f = uwlines[0].tokens[1]
74    self.assertFalse(f.can_break_before)
75    self.assertFalse(f.must_break_before)
76    self.assertEqual(f.split_penalty, split_penalty.UNBREAKABLE)
77
78    lparen = uwlines[0].tokens[2]
79    self.assertFalse(lparen.can_break_before)
80    self.assertFalse(lparen.must_break_before)
81    self.assertEqual(lparen.split_penalty, split_penalty.UNBREAKABLE)
82
83
84def _MakeFormatTokenLeaf(token_type, token_value):
85  return format_token.FormatToken(pytree.Leaf(token_type, token_value))
86
87
88def _MakeFormatTokenList(token_type_values):
89  return [
90      _MakeFormatTokenLeaf(token_type, token_value)
91      for token_type, token_value in token_type_values
92  ]
93
94
95if __name__ == '__main__':
96  unittest.main()
97