1# Copyright 2016 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.reformatter."""
15
16import textwrap
17import unittest
18
19from yapf.yapflib import py3compat
20from yapf.yapflib import reformatter
21from yapf.yapflib import style
22from yapf.yapflib import verifier
23
24from yapftests import yapf_test_helper
25
26
27@unittest.skipIf(py3compat.PY3, 'Requires Python 2')
28class TestVerifyNoVerify(yapf_test_helper.YAPFTest):
29
30  @classmethod
31  def setUpClass(cls):
32    style.SetGlobalStyle(style.CreatePEP8Style())
33
34  def testVerifyException(self):
35    unformatted_code = textwrap.dedent("""\
36        class ABC(metaclass=type):
37          pass
38        """)
39    uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
40    with self.assertRaises(verifier.InternalError):
41      reformatter.Reformat(uwlines, verify=True)
42    reformatter.Reformat(uwlines)  # verify should be False by default.
43
44  def testNoVerify(self):
45    unformatted_code = textwrap.dedent("""\
46        class ABC(metaclass=type):
47          pass
48        """)
49    expected_formatted_code = textwrap.dedent("""\
50        class ABC(metaclass=type):
51            pass
52        """)
53    uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
54    self.assertCodeEqual(expected_formatted_code,
55                         reformatter.Reformat(uwlines, verify=False))
56
57  def testVerifyFutureImport(self):
58    unformatted_code = textwrap.dedent("""\
59        from __future__ import print_function
60
61        def call_my_function(the_function):
62          the_function("hi")
63
64        if __name__ == "__main__":
65          call_my_function(print)
66        """)
67    uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
68    with self.assertRaises(verifier.InternalError):
69      reformatter.Reformat(uwlines, verify=True)
70
71    expected_formatted_code = textwrap.dedent("""\
72        from __future__ import print_function
73
74
75        def call_my_function(the_function):
76            the_function("hi")
77
78
79        if __name__ == "__main__":
80            call_my_function(print)
81        """)
82    uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
83    self.assertCodeEqual(expected_formatted_code,
84                         reformatter.Reformat(uwlines, verify=False))
85
86  def testContinuationLineShouldBeDistinguished(self):
87    unformatted_code = textwrap.dedent("""\
88        class Foo(object):
89
90            def bar(self):
91                if self.solo_generator_that_is_long is None and len(
92                        self.generators + self.next_batch) == 1:
93                    pass
94        """)
95    expected_formatted_code = textwrap.dedent("""\
96        class Foo(object):
97            def bar(self):
98                if self.solo_generator_that_is_long is None and len(
99                        self.generators + self.next_batch) == 1:
100                    pass
101        """)
102    uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
103    self.assertCodeEqual(expected_formatted_code,
104                         reformatter.Reformat(uwlines, verify=False))
105
106
107if __name__ == '__main__':
108  unittest.main()
109