Home | History | Annotate | Download | only in yapftests
      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.blank_line_calculator."""
     15 
     16 import textwrap
     17 import unittest
     18 
     19 from yapf.yapflib import reformatter
     20 from yapf.yapflib import style
     21 from yapf.yapflib import yapf_api
     22 
     23 from yapftests import yapf_test_helper
     24 
     25 
     26 class BasicBlankLineCalculatorTest(yapf_test_helper.YAPFTest):
     27 
     28   @classmethod
     29   def setUpClass(cls):
     30     style.SetGlobalStyle(style.CreateChromiumStyle())
     31 
     32   def testDecorators(self):
     33     unformatted_code = textwrap.dedent("""\
     34         @bork()
     35 
     36         def foo():
     37           pass
     38         """)
     39     expected_formatted_code = textwrap.dedent("""\
     40         @bork()
     41         def foo():
     42           pass
     43         """)
     44     uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
     45     self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
     46 
     47   def testComplexDecorators(self):
     48     unformatted_code = textwrap.dedent("""\
     49         import sys
     50         @bork()
     51 
     52         def foo():
     53           pass
     54         @fork()
     55 
     56         class moo(object):
     57           @bar()
     58           @baz()
     59 
     60           def method(self):
     61             pass
     62         """)
     63     expected_formatted_code = textwrap.dedent("""\
     64         import sys
     65 
     66 
     67         @bork()
     68         def foo():
     69           pass
     70 
     71 
     72         @fork()
     73         class moo(object):
     74 
     75           @bar()
     76           @baz()
     77           def method(self):
     78             pass
     79         """)
     80     uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
     81     self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
     82 
     83   def testCodeAfterFunctionsAndClasses(self):
     84     unformatted_code = textwrap.dedent("""\
     85         def foo():
     86           pass
     87         top_level_code = True
     88         class moo(object):
     89           def method_1(self):
     90             pass
     91           ivar_a = 42
     92           ivar_b = 13
     93           def method_2(self):
     94             pass
     95         try:
     96           raise Error
     97         except Error as error:
     98           pass
     99         """)
    100     expected_formatted_code = textwrap.dedent("""\
    101         def foo():
    102           pass
    103 
    104 
    105         top_level_code = True
    106 
    107 
    108         class moo(object):
    109 
    110           def method_1(self):
    111             pass
    112 
    113           ivar_a = 42
    114           ivar_b = 13
    115 
    116           def method_2(self):
    117             pass
    118 
    119 
    120         try:
    121           raise Error
    122         except Error as error:
    123           pass
    124         """)
    125     uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
    126     self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
    127 
    128   def testCommentSpacing(self):
    129     unformatted_code = textwrap.dedent("""\
    130         # This is the first comment
    131         # And it's multiline
    132 
    133         # This is the second comment
    134 
    135         def foo():
    136           pass
    137 
    138         # multiline before a
    139         # class definition
    140 
    141         # This is the second comment
    142 
    143         class qux(object):
    144           pass
    145 
    146 
    147         # An attached comment.
    148         class bar(object):
    149           '''class docstring'''
    150           # Comment attached to
    151           # function
    152           def foo(self):
    153             '''Another docstring.'''
    154             # Another multiline
    155             # comment
    156             pass
    157         """)
    158     expected_formatted_code = textwrap.dedent("""\
    159         # This is the first comment
    160         # And it's multiline
    161 
    162         # This is the second comment
    163 
    164 
    165         def foo():
    166           pass
    167 
    168 
    169         # multiline before a
    170         # class definition
    171 
    172         # This is the second comment
    173 
    174 
    175         class qux(object):
    176           pass
    177 
    178 
    179         # An attached comment.
    180         class bar(object):
    181           '''class docstring'''
    182 
    183           # Comment attached to
    184           # function
    185           def foo(self):
    186             '''Another docstring.'''
    187             # Another multiline
    188             # comment
    189             pass
    190         """)
    191     uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
    192     self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
    193 
    194   def testCommentBeforeMethod(self):
    195     code = textwrap.dedent("""\
    196         class foo(object):
    197 
    198           # pylint: disable=invalid-name
    199           def f(self):
    200             pass
    201         """)
    202     uwlines = yapf_test_helper.ParseAndUnwrap(code)
    203     self.assertCodeEqual(code, reformatter.Reformat(uwlines))
    204 
    205   def testCommentsBeforeClassDefs(self):
    206     code = textwrap.dedent('''\
    207         """Test."""
    208 
    209         # Comment
    210 
    211 
    212         class Foo(object):
    213           pass
    214         ''')
    215     uwlines = yapf_test_helper.ParseAndUnwrap(code)
    216     self.assertCodeEqual(code, reformatter.Reformat(uwlines))
    217 
    218   def testCommentsBeforeDecorator(self):
    219     code = textwrap.dedent("""\
    220         # The @foo operator adds bork to a().
    221         @foo()
    222         def a():
    223           pass
    224         """)
    225     uwlines = yapf_test_helper.ParseAndUnwrap(code)
    226     self.assertCodeEqual(code, reformatter.Reformat(uwlines))
    227 
    228     code = textwrap.dedent("""\
    229         # Hello world
    230 
    231 
    232         @foo()
    233         def a():
    234           pass
    235         """)
    236     uwlines = yapf_test_helper.ParseAndUnwrap(code)
    237     self.assertCodeEqual(code, reformatter.Reformat(uwlines))
    238 
    239   def testCommentsAfterDecorator(self):
    240     code = textwrap.dedent("""\
    241         class _():
    242 
    243           def _():
    244             pass
    245 
    246           @pytest.mark.xfail(reason="#709 and #710")
    247           # also
    248           #@pytest.mark.xfail(setuptools.tests.is_ascii,
    249           #    reason="https://github.com/pypa/setuptools/issues/706")
    250           def test_unicode_filename_in_sdist(self, sdist_unicode, tmpdir, monkeypatch):
    251             pass
    252         """)
    253     uwlines = yapf_test_helper.ParseAndUnwrap(code)
    254     self.assertCodeEqual(code, reformatter.Reformat(uwlines))
    255 
    256   def testInnerClasses(self):
    257     unformatted_code = textwrap.dedent("""\
    258       class DeployAPIClient(object):
    259           class Error(Exception): pass
    260 
    261           class TaskValidationError(Error): pass
    262 
    263           class DeployAPIHTTPError(Error): pass
    264         """)
    265     expected_formatted_code = textwrap.dedent("""\
    266       class DeployAPIClient(object):
    267 
    268         class Error(Exception):
    269           pass
    270 
    271         class TaskValidationError(Error):
    272           pass
    273 
    274         class DeployAPIHTTPError(Error):
    275           pass
    276         """)
    277     uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
    278     self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
    279 
    280   def testLinesOnRangeBoundary(self):
    281     unformatted_code = textwrap.dedent(u"""\
    282         def A():
    283           pass
    284 
    285         def B():  # 4
    286           pass  # 5
    287 
    288         def C():
    289           pass
    290         def D():  # 9
    291           pass  # 10
    292         def E():
    293           pass
    294         """)
    295     expected_formatted_code = textwrap.dedent(u"""\
    296         def A():
    297           pass
    298 
    299 
    300         def B():  # 4
    301           pass  # 5
    302 
    303 
    304         def C():
    305           pass
    306 
    307 
    308         def D():  # 9
    309           pass  # 10
    310 
    311 
    312         def E():
    313           pass
    314         """)
    315     code, changed = yapf_api.FormatCode(
    316         unformatted_code, lines=[(4, 5), (9, 10)])
    317     self.assertCodeEqual(expected_formatted_code, code)
    318     self.assertTrue(changed)
    319 
    320   def testLinesRangeBoundaryNotOutside(self):
    321     unformatted_code = textwrap.dedent(u"""\
    322         def A():
    323           pass
    324 
    325 
    326 
    327         def B():  # 6
    328           pass  # 7
    329 
    330 
    331 
    332         def C():
    333           pass
    334         """)
    335     expected_formatted_code = textwrap.dedent(u"""\
    336         def A():
    337           pass
    338 
    339 
    340 
    341         def B():  # 6
    342           pass  # 7
    343 
    344 
    345 
    346         def C():
    347           pass
    348         """)
    349     code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 7)])
    350     self.assertCodeEqual(expected_formatted_code, code)
    351     self.assertFalse(changed)
    352 
    353 
    354 if __name__ == '__main__':
    355   unittest.main()
    356