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.line_joiner.""" 15 16import textwrap 17import unittest 18 19from yapf.yapflib import line_joiner 20from yapf.yapflib import style 21 22from yapftests import yapf_test_helper 23 24 25class LineJoinerTest(yapf_test_helper.YAPFTest): 26 27 @classmethod 28 def setUpClass(cls): 29 style.SetGlobalStyle(style.CreatePEP8Style()) 30 31 def _CheckLineJoining(self, code, join_lines): 32 """Check that the given LogicalLines are joined as expected. 33 34 Arguments: 35 code: The code to check to see if we can join it. 36 join_lines: True if we expect the lines to be joined. 37 """ 38 llines = yapf_test_helper.ParseAndUnwrap(code) 39 self.assertCodeEqual(line_joiner.CanMergeMultipleLines(llines), join_lines) 40 41 def testSimpleSingleLineStatement(self): 42 code = textwrap.dedent(u"""\ 43 if isinstance(a, int): continue 44 """) 45 self._CheckLineJoining(code, join_lines=True) 46 47 def testSimpleMultipleLineStatement(self): 48 code = textwrap.dedent(u"""\ 49 if isinstance(b, int): 50 continue 51 """) 52 self._CheckLineJoining(code, join_lines=False) 53 54 def testSimpleMultipleLineComplexStatement(self): 55 code = textwrap.dedent(u"""\ 56 if isinstance(c, int): 57 while True: 58 continue 59 """) 60 self._CheckLineJoining(code, join_lines=False) 61 62 def testSimpleMultipleLineStatementWithComment(self): 63 code = textwrap.dedent(u"""\ 64 if isinstance(d, int): continue # We're pleased that d's an int. 65 """) 66 self._CheckLineJoining(code, join_lines=True) 67 68 def testSimpleMultipleLineStatementWithLargeIndent(self): 69 code = textwrap.dedent(u"""\ 70 if isinstance(e, int): continue 71 """) 72 self._CheckLineJoining(code, join_lines=True) 73 74 def testOverColumnLimit(self): 75 code = textwrap.dedent(u"""\ 76 if instance(bbbbbbbbbbbbbbbbbbbbbbbbb, int): cccccccccccccccccccccccccc = ddddddddddddddddddddd 77 """) # noqa 78 self._CheckLineJoining(code, join_lines=False) 79 80 81if __name__ == '__main__': 82 unittest.main() 83