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 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 logical_line 24from yapf.yapflib import split_penalty 25 26from yapftests import yapf_test_helper 27 28 29class LogicalLineBasicTest(unittest.TestCase): 30 31 def testConstruction(self): 32 toks = _MakeFormatTokenList([(token.DOT, '.'), (token.VBAR, '|')]) 33 lline = logical_line.LogicalLine(20, toks) 34 self.assertEqual(20, lline.depth) 35 self.assertEqual(['DOT', 'VBAR'], [tok.name for tok in lline.tokens]) 36 37 def testFirstLast(self): 38 toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('), 39 (token.VBAR, '|')]) 40 lline = logical_line.LogicalLine(20, toks) 41 self.assertEqual(20, lline.depth) 42 self.assertEqual('DOT', lline.first.name) 43 self.assertEqual('VBAR', lline.last.name) 44 45 def testAsCode(self): 46 toks = _MakeFormatTokenList([(token.DOT, '.'), (token.LPAR, '('), 47 (token.VBAR, '|')]) 48 lline = logical_line.LogicalLine(2, toks) 49 self.assertEqual(' . ( |', lline.AsCode()) 50 51 def testAppendToken(self): 52 lline = logical_line.LogicalLine(0) 53 lline.AppendToken(_MakeFormatTokenLeaf(token.LPAR, '(')) 54 lline.AppendToken(_MakeFormatTokenLeaf(token.RPAR, ')')) 55 self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in lline.tokens]) 56 57 def testAppendNode(self): 58 lline = logical_line.LogicalLine(0) 59 lline.AppendNode(pytree.Leaf(token.LPAR, '(')) 60 lline.AppendNode(pytree.Leaf(token.RPAR, ')')) 61 self.assertEqual(['LPAR', 'RPAR'], [tok.name for tok in lline.tokens]) 62 63 64class LogicalLineFormattingInformationTest(yapf_test_helper.YAPFTest): 65 66 def testFuncDef(self): 67 code = textwrap.dedent(r""" 68 def f(a, b): 69 pass 70 """) 71 llines = yapf_test_helper.ParseAndUnwrap(code) 72 73 f = llines[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 = llines[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