• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Style config tests for yapf.reformatter."""
15
16import textwrap
17import unittest
18
19from yapf.yapflib import reformatter
20from yapf.yapflib import style
21
22from yapftests import yapf_test_helper
23
24
25class TestsForStyleConfig(yapf_test_helper.YAPFTest):
26
27  def setUp(self):
28    self.current_style = style.DEFAULT_STYLE
29
30  def testSetGlobalStyle(self):
31    try:
32      style.SetGlobalStyle(style.CreateChromiumStyle())
33      unformatted_code = textwrap.dedent(u"""\
34          for i in range(5):
35           print('bar')
36          """)
37      expected_formatted_code = textwrap.dedent(u"""\
38          for i in range(5):
39            print('bar')
40          """)
41      uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
42      self.assertCodeEqual(expected_formatted_code,
43                           reformatter.Reformat(uwlines))
44    finally:
45      style.SetGlobalStyle(style.CreatePEP8Style())
46      style.DEFAULT_STYLE = self.current_style
47
48    unformatted_code = textwrap.dedent(u"""\
49        for i in range(5):
50         print('bar')
51        """)
52    expected_formatted_code = textwrap.dedent(u"""\
53        for i in range(5):
54            print('bar')
55        """)
56    uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
57    self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
58
59  def testOperatorStyle(self):
60    try:
61      sympy_style = style.CreatePEP8Style()
62      sympy_style['NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS'] = \
63        style._StringSetConverter('*,/')
64      style.SetGlobalStyle(sympy_style)
65      unformatted_code = textwrap.dedent("""\
66          a = 1+2 * 3 - 4 / 5
67          """)
68      expected_formatted_code = textwrap.dedent("""\
69          a = 1 + 2*3 - 4/5
70          """)
71
72      uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
73      self.assertCodeEqual(expected_formatted_code,
74                           reformatter.Reformat(uwlines))
75    finally:
76      style.SetGlobalStyle(style.CreatePEP8Style())
77      style.DEFAULT_STYLE = self.current_style
78
79
80if __name__ == '__main__':
81  unittest.main()
82