1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Style config tests for yapf.reformatter."""
import textwrap
import unittest
from yapf.yapflib import reformatter
from yapf.yapflib import style
from yapftests import yapf_test_helper
class TestsForStyleConfig(yapf_test_helper.YAPFTest):
def setUp(self):
self.current_style = style.DEFAULT_STYLE
def testSetGlobalStyle(self):
try:
style.SetGlobalStyle(style.CreateYapfStyle())
unformatted_code = textwrap.dedent(u"""\
for i in range(5):
print('bar')
""")
expected_formatted_code = textwrap.dedent(u"""\
for i in range(5):
print('bar')
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(llines))
finally:
style.SetGlobalStyle(style.CreatePEP8Style())
style.DEFAULT_STYLE = self.current_style
unformatted_code = textwrap.dedent(u"""\
for i in range(5):
print('bar')
""")
expected_formatted_code = textwrap.dedent(u"""\
for i in range(5):
print('bar')
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines))
def testOperatorNoSpaceStyle(self):
try:
sympy_style = style.CreatePEP8Style()
sympy_style['NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS'] = \
style._StringSetConverter('*,/')
style.SetGlobalStyle(sympy_style)
unformatted_code = textwrap.dedent("""\
a = 1+2 * 3 - 4 / 5
b = '0' * 1
""")
expected_formatted_code = textwrap.dedent("""\
a = 1 + 2*3 - 4/5
b = '0'*1
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(llines))
finally:
style.SetGlobalStyle(style.CreatePEP8Style())
style.DEFAULT_STYLE = self.current_style
def testOperatorPrecedenceStyle(self):
try:
pep8_with_precedence = style.CreatePEP8Style()
pep8_with_precedence['ARITHMETIC_PRECEDENCE_INDICATION'] = True
style.SetGlobalStyle(pep8_with_precedence)
unformatted_code = textwrap.dedent("""\
1+2
(1 + 2) * (3 - (4 / 5))
a = 1 * 2 + 3 / 4
b = 1 / 2 - 3 * 4
c = (1 + 2) * (3 - 4)
d = (1 - 2) / (3 + 4)
e = 1 * 2 - 3
f = 1 + 2 + 3 + 4
g = 1 * 2 * 3 * 4
h = 1 + 2 - 3 + 4
i = 1 * 2 / 3 * 4
j = (1 * 2 - 3) + 4
k = (1 * 2 * 3) + (4 * 5 * 6 * 7 * 8)
""")
expected_formatted_code = textwrap.dedent("""\
1 + 2
(1+2) * (3 - (4/5))
a = 1*2 + 3/4
b = 1/2 - 3*4
c = (1+2) * (3-4)
d = (1-2) / (3+4)
e = 1*2 - 3
f = 1 + 2 + 3 + 4
g = 1 * 2 * 3 * 4
h = 1 + 2 - 3 + 4
i = 1 * 2 / 3 * 4
j = (1*2 - 3) + 4
k = (1*2*3) + (4*5*6*7*8)
""")
llines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(llines))
finally:
style.SetGlobalStyle(style.CreatePEP8Style())
style.DEFAULT_STYLE = self.current_style
def testNoSplitBeforeFirstArgumentStyle1(self):
try:
pep8_no_split_before_first = style.CreatePEP8Style()
pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False
pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = False
style.SetGlobalStyle(pep8_no_split_before_first)
formatted_code = textwrap.dedent("""\
# Example from in-code MustSplit comments
foo = outer_function_call(fitting_inner_function_call(inner_arg1, inner_arg2),
outer_arg1, outer_arg2)
foo = outer_function_call(
not_fitting_inner_function_call(inner_arg1, inner_arg2), outer_arg1,
outer_arg2)
# Examples Issue#424
a_super_long_version_of_print(argument1, argument2, argument3, argument4,
argument5, argument6, argument7)
CREDS_FILE = os.path.join(os.path.expanduser('~'),
'apis/super-secret-admin-creds.json')
# Examples Issue#556
i_take_a_lot_of_params(arg1, param1=very_long_expression1(),
param2=very_long_expression2(),
param3=very_long_expression3(),
param4=very_long_expression4())
# Examples Issue#590
plt.plot(numpy.linspace(0, 1, 10), numpy.linspace(0, 1, 10), marker="x",
color="r")
plt.plot(veryverylongvariablename, veryverylongvariablename, marker="x",
color="r")
""") # noqa
llines = yapf_test_helper.ParseAndUnwrap(formatted_code)
self.assertCodeEqual(formatted_code, reformatter.Reformat(llines))
finally:
style.SetGlobalStyle(style.CreatePEP8Style())
style.DEFAULT_STYLE = self.current_style
def testNoSplitBeforeFirstArgumentStyle2(self):
try:
pep8_no_split_before_first = style.CreatePEP8Style()
pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False
pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = True
style.SetGlobalStyle(pep8_no_split_before_first)
formatted_code = textwrap.dedent("""\
# Examples Issue#556
i_take_a_lot_of_params(arg1,
param1=very_long_expression1(),
param2=very_long_expression2(),
param3=very_long_expression3(),
param4=very_long_expression4())
# Examples Issue#590
plt.plot(numpy.linspace(0, 1, 10),
numpy.linspace(0, 1, 10),
marker="x",
color="r")
plt.plot(veryverylongvariablename,
veryverylongvariablename,
marker="x",
color="r")
""")
llines = yapf_test_helper.ParseAndUnwrap(formatted_code)
self.assertCodeEqual(formatted_code, reformatter.Reformat(llines))
finally:
style.SetGlobalStyle(style.CreatePEP8Style())
style.DEFAULT_STYLE = self.current_style
if __name__ == '__main__':
unittest.main()
|