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