• 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.blank_line_calculator."""
15
16import textwrap
17import unittest
18
19from yapf.yapflib import reformatter
20from yapf.yapflib import style
21from yapf.yapflib import yapf_api
22
23from yapftests import yapf_test_helper
24
25
26class BasicBlankLineCalculatorTest(yapf_test_helper.YAPFTest):
27
28  @classmethod
29  def setUpClass(cls):
30    style.SetGlobalStyle(style.CreateYapfStyle())
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    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
45    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
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    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
81    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
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    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
126    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
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    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
192    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
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    llines = yapf_test_helper.ParseAndUnwrap(code)
203    self.assertCodeEqual(code, reformatter.Reformat(llines))
204
205  def testCommentsBeforeClassDefs(self):
206    code = textwrap.dedent('''\
207        """Test."""
208
209        # Comment
210
211
212        class Foo(object):
213          pass
214        ''')
215    llines = yapf_test_helper.ParseAndUnwrap(code)
216    self.assertCodeEqual(code, reformatter.Reformat(llines))
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    llines = yapf_test_helper.ParseAndUnwrap(code)
226    self.assertCodeEqual(code, reformatter.Reformat(llines))
227
228    code = textwrap.dedent("""\
229        # Hello world
230
231
232        @foo()
233        def a():
234          pass
235        """)
236    llines = yapf_test_helper.ParseAndUnwrap(code)
237    self.assertCodeEqual(code, reformatter.Reformat(llines))
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        """)  # noqa
253    llines = yapf_test_helper.ParseAndUnwrap(code)
254    self.assertCodeEqual(code, reformatter.Reformat(llines))
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    llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
278    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
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        def C():
304          pass
305
306
307        def D():  # 9
308          pass  # 10
309        def E():
310          pass
311        """)
312    code, changed = yapf_api.FormatCode(
313        unformatted_code, lines=[(4, 5), (9, 10)])
314    self.assertCodeEqual(expected_formatted_code, code)
315    self.assertTrue(changed)
316
317  def testLinesRangeBoundaryNotOutside(self):
318    unformatted_code = textwrap.dedent(u"""\
319        def A():
320          pass
321
322
323
324        def B():  # 6
325          pass  # 7
326
327
328
329        def C():
330          pass
331        """)
332    expected_formatted_code = textwrap.dedent(u"""\
333        def A():
334          pass
335
336
337
338        def B():  # 6
339          pass  # 7
340
341
342
343        def C():
344          pass
345        """)
346    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 7)])
347    self.assertCodeEqual(expected_formatted_code, code)
348    self.assertFalse(changed)
349
350  def testLinesRangeRemove(self):
351    unformatted_code = textwrap.dedent(u"""\
352        def A():
353          pass
354
355
356
357        def B():  # 6
358          pass  # 7
359
360
361
362
363        def C():
364          pass
365        """)
366    expected_formatted_code = textwrap.dedent(u"""\
367        def A():
368          pass
369
370
371        def B():  # 6
372          pass  # 7
373
374
375
376
377        def C():
378          pass
379        """)
380    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(5, 9)])
381    self.assertCodeEqual(expected_formatted_code, code)
382    self.assertTrue(changed)
383
384  def testLinesRangeRemoveSome(self):
385    unformatted_code = textwrap.dedent(u"""\
386        def A():
387          pass
388
389
390
391
392        def B():  # 7
393          pass  # 8
394
395
396
397
398        def C():
399          pass
400        """)
401    expected_formatted_code = textwrap.dedent(u"""\
402        def A():
403          pass
404
405
406
407        def B():  # 7
408          pass  # 8
409
410
411
412
413        def C():
414          pass
415        """)
416    code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 9)])
417    self.assertCodeEqual(expected_formatted_code, code)
418    self.assertTrue(changed)
419
420
421if __name__ == '__main__':
422  unittest.main()
423