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.CreateYapfStyle()) 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 llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) 42 self.assertCodeEqual(expected_formatted_code, 43 reformatter.Reformat(llines)) 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 llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) 57 self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) 58 59 def testOperatorNoSpaceStyle(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 b = '0' * 1 68 """) 69 expected_formatted_code = textwrap.dedent("""\ 70 a = 1 + 2*3 - 4/5 71 b = '0'*1 72 """) 73 74 llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) 75 self.assertCodeEqual(expected_formatted_code, 76 reformatter.Reformat(llines)) 77 finally: 78 style.SetGlobalStyle(style.CreatePEP8Style()) 79 style.DEFAULT_STYLE = self.current_style 80 81 def testOperatorPrecedenceStyle(self): 82 try: 83 pep8_with_precedence = style.CreatePEP8Style() 84 pep8_with_precedence['ARITHMETIC_PRECEDENCE_INDICATION'] = True 85 style.SetGlobalStyle(pep8_with_precedence) 86 unformatted_code = textwrap.dedent("""\ 87 1+2 88 (1 + 2) * (3 - (4 / 5)) 89 a = 1 * 2 + 3 / 4 90 b = 1 / 2 - 3 * 4 91 c = (1 + 2) * (3 - 4) 92 d = (1 - 2) / (3 + 4) 93 e = 1 * 2 - 3 94 f = 1 + 2 + 3 + 4 95 g = 1 * 2 * 3 * 4 96 h = 1 + 2 - 3 + 4 97 i = 1 * 2 / 3 * 4 98 j = (1 * 2 - 3) + 4 99 k = (1 * 2 * 3) + (4 * 5 * 6 * 7 * 8) 100 """) 101 expected_formatted_code = textwrap.dedent("""\ 102 1 + 2 103 (1+2) * (3 - (4/5)) 104 a = 1*2 + 3/4 105 b = 1/2 - 3*4 106 c = (1+2) * (3-4) 107 d = (1-2) / (3+4) 108 e = 1*2 - 3 109 f = 1 + 2 + 3 + 4 110 g = 1 * 2 * 3 * 4 111 h = 1 + 2 - 3 + 4 112 i = 1 * 2 / 3 * 4 113 j = (1*2 - 3) + 4 114 k = (1*2*3) + (4*5*6*7*8) 115 """) 116 117 llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) 118 self.assertCodeEqual(expected_formatted_code, 119 reformatter.Reformat(llines)) 120 finally: 121 style.SetGlobalStyle(style.CreatePEP8Style()) 122 style.DEFAULT_STYLE = self.current_style 123 124 def testNoSplitBeforeFirstArgumentStyle1(self): 125 try: 126 pep8_no_split_before_first = style.CreatePEP8Style() 127 pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False 128 pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = False 129 style.SetGlobalStyle(pep8_no_split_before_first) 130 formatted_code = textwrap.dedent("""\ 131 # Example from in-code MustSplit comments 132 foo = outer_function_call(fitting_inner_function_call(inner_arg1, inner_arg2), 133 outer_arg1, outer_arg2) 134 135 foo = outer_function_call( 136 not_fitting_inner_function_call(inner_arg1, inner_arg2), outer_arg1, 137 outer_arg2) 138 139 # Examples Issue#424 140 a_super_long_version_of_print(argument1, argument2, argument3, argument4, 141 argument5, argument6, argument7) 142 143 CREDS_FILE = os.path.join(os.path.expanduser('~'), 144 'apis/super-secret-admin-creds.json') 145 146 # Examples Issue#556 147 i_take_a_lot_of_params(arg1, param1=very_long_expression1(), 148 param2=very_long_expression2(), 149 param3=very_long_expression3(), 150 param4=very_long_expression4()) 151 152 # Examples Issue#590 153 plt.plot(numpy.linspace(0, 1, 10), numpy.linspace(0, 1, 10), marker="x", 154 color="r") 155 156 plt.plot(veryverylongvariablename, veryverylongvariablename, marker="x", 157 color="r") 158 """) # noqa 159 llines = yapf_test_helper.ParseAndUnwrap(formatted_code) 160 self.assertCodeEqual(formatted_code, reformatter.Reformat(llines)) 161 finally: 162 style.SetGlobalStyle(style.CreatePEP8Style()) 163 style.DEFAULT_STYLE = self.current_style 164 165 def testNoSplitBeforeFirstArgumentStyle2(self): 166 try: 167 pep8_no_split_before_first = style.CreatePEP8Style() 168 pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False 169 pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = True 170 style.SetGlobalStyle(pep8_no_split_before_first) 171 formatted_code = textwrap.dedent("""\ 172 # Examples Issue#556 173 i_take_a_lot_of_params(arg1, 174 param1=very_long_expression1(), 175 param2=very_long_expression2(), 176 param3=very_long_expression3(), 177 param4=very_long_expression4()) 178 179 # Examples Issue#590 180 plt.plot(numpy.linspace(0, 1, 10), 181 numpy.linspace(0, 1, 10), 182 marker="x", 183 color="r") 184 185 plt.plot(veryverylongvariablename, 186 veryverylongvariablename, 187 marker="x", 188 color="r") 189 """) 190 llines = yapf_test_helper.ParseAndUnwrap(formatted_code) 191 self.assertCodeEqual(formatted_code, reformatter.Reformat(llines)) 192 finally: 193 style.SetGlobalStyle(style.CreatePEP8Style()) 194 style.DEFAULT_STYLE = self.current_style 195 196 197if __name__ == '__main__': 198 unittest.main() 199