1 # -*- coding: utf-8 -*-
2 # Copyright 2015 Google Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Tests for yapf.style."""
23 from yapf
.yapflib
import style
25 from yapftests
import utils
26 from yapftests
import yapf_test_helper
29 class UtilsTest(yapf_test_helper
.YAPFTest
):
31 def testContinuationAlignStyleStringConverter(self
):
32 for cont_align_space
in ('', 'space', '"space"', '\'space\''):
34 style
._ContinuationAlignStyleStringConverter
(cont_align_space
),
36 for cont_align_fixed
in ('fixed', '"fixed"', '\'fixed\''):
38 style
._ContinuationAlignStyleStringConverter
(cont_align_fixed
),
40 for cont_align_valignright
in (
49 style
._ContinuationAlignStyleStringConverter
(cont_align_valignright
),
51 with self
.assertRaises(ValueError) as ctx
:
52 style
._ContinuationAlignStyleStringConverter
('blahblah')
53 self
.assertIn("unknown continuation align style: 'blahblah'",
56 def testStringListConverter(self
):
57 self
.assertEqual(style
._StringListConverter
('foo, bar'), ['foo', 'bar'])
58 self
.assertEqual(style
._StringListConverter
('foo,bar'), ['foo', 'bar'])
59 self
.assertEqual(style
._StringListConverter
(' foo'), ['foo'])
61 style
._StringListConverter
('joe ,foo, bar'), ['joe', 'foo', 'bar'])
63 def testBoolConverter(self
):
64 self
.assertEqual(style
._BoolConverter
('true'), True)
65 self
.assertEqual(style
._BoolConverter
('1'), True)
66 self
.assertEqual(style
._BoolConverter
('false'), False)
67 self
.assertEqual(style
._BoolConverter
('0'), False)
69 def testIntListConverter(self
):
70 self
.assertEqual(style
._IntListConverter
('1, 2, 3'), [1, 2, 3])
71 self
.assertEqual(style
._IntListConverter
('[ 1, 2, 3 ]'), [1, 2, 3])
72 self
.assertEqual(style
._IntListConverter
('[ 1, 2, 3, ]'), [1, 2, 3])
74 def testIntOrIntListConverter(self
):
75 self
.assertEqual(style
._IntOrIntListConverter
('10'), 10)
76 self
.assertEqual(style
._IntOrIntListConverter
('1, 2, 3'), [1, 2, 3])
79 def _LooksLikeGoogleStyle(cfg
):
80 return cfg
['COLUMN_LIMIT'] == 80 and cfg
['SPLIT_COMPLEX_COMPREHENSION']
83 def _LooksLikePEP8Style(cfg
):
84 return cfg
['COLUMN_LIMIT'] == 79
87 def _LooksLikeFacebookStyle(cfg
):
88 return cfg
['DEDENT_CLOSING_BRACKETS']
91 def _LooksLikeYapfStyle(cfg
):
92 return cfg
['SPLIT_BEFORE_DOT']
95 class PredefinedStylesByNameTest(yapf_test_helper
.YAPFTest
):
98 def setUpClass(cls
): # pylint: disable=g-missing-super-call
99 style
.SetGlobalStyle(style
.CreatePEP8Style())
101 def testDefault(self
):
103 cfg
= style
.CreateStyleFromConfig(None)
104 self
.assertTrue(_LooksLikePEP8Style(cfg
))
106 def testPEP8ByName(self
):
107 for pep8_name
in ('PEP8', 'pep8', 'Pep8'):
108 cfg
= style
.CreateStyleFromConfig(pep8_name
)
109 self
.assertTrue(_LooksLikePEP8Style(cfg
))
111 def testGoogleByName(self
):
112 for google_name
in ('google', 'Google', 'GOOGLE'):
113 cfg
= style
.CreateStyleFromConfig(google_name
)
114 self
.assertTrue(_LooksLikeGoogleStyle(cfg
))
116 def testYapfByName(self
):
117 for yapf_name
in ('yapf', 'YAPF'):
118 cfg
= style
.CreateStyleFromConfig(yapf_name
)
119 self
.assertTrue(_LooksLikeYapfStyle(cfg
))
121 def testFacebookByName(self
):
122 for fb_name
in ('facebook', 'FACEBOOK', 'Facebook'):
123 cfg
= style
.CreateStyleFromConfig(fb_name
)
124 self
.assertTrue(_LooksLikeFacebookStyle(cfg
))
127 class StyleFromFileTest(yapf_test_helper
.YAPFTest
):
130 def setUpClass(cls
): # pylint: disable=g-missing-super-call
131 cls
.test_tmpdir
= tempfile
.mkdtemp()
132 style
.SetGlobalStyle(style
.CreatePEP8Style())
135 def tearDownClass(cls
): # pylint: disable=g-missing-super-call
136 shutil
.rmtree(cls
.test_tmpdir
)
138 def testDefaultBasedOnStyle(self
):
139 cfg
= textwrap
.dedent("""\
141 continuation_indent_width = 20
143 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
144 cfg
= style
.CreateStyleFromConfig(filepath
)
145 self
.assertTrue(_LooksLikePEP8Style(cfg
))
146 self
.assertEqual(cfg
['CONTINUATION_INDENT_WIDTH'], 20)
148 def testDefaultBasedOnPEP8Style(self
):
149 cfg
= textwrap
.dedent("""\
151 based_on_style = pep8
152 continuation_indent_width = 40
154 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
155 cfg
= style
.CreateStyleFromConfig(filepath
)
156 self
.assertTrue(_LooksLikePEP8Style(cfg
))
157 self
.assertEqual(cfg
['CONTINUATION_INDENT_WIDTH'], 40)
159 def testDefaultBasedOnGoogleStyle(self
):
160 cfg
= textwrap
.dedent("""\
162 based_on_style = google
163 continuation_indent_width = 20
165 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
166 cfg
= style
.CreateStyleFromConfig(filepath
)
167 self
.assertTrue(_LooksLikeGoogleStyle(cfg
))
168 self
.assertEqual(cfg
['CONTINUATION_INDENT_WIDTH'], 20)
170 def testDefaultBasedOnFacebookStyle(self
):
171 cfg
= textwrap
.dedent("""\
173 based_on_style = facebook
174 continuation_indent_width = 20
176 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
177 cfg
= style
.CreateStyleFromConfig(filepath
)
178 self
.assertTrue(_LooksLikeFacebookStyle(cfg
))
179 self
.assertEqual(cfg
['CONTINUATION_INDENT_WIDTH'], 20)
181 def testBoolOptionValue(self
):
182 cfg
= textwrap
.dedent("""\
184 based_on_style = pep8
185 SPLIT_BEFORE_NAMED_ASSIGNS=False
186 split_before_logical_operator = true
188 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
189 cfg
= style
.CreateStyleFromConfig(filepath
)
190 self
.assertTrue(_LooksLikePEP8Style(cfg
))
191 self
.assertEqual(cfg
['SPLIT_BEFORE_NAMED_ASSIGNS'], False)
192 self
.assertEqual(cfg
['SPLIT_BEFORE_LOGICAL_OPERATOR'], True)
194 def testStringListOptionValue(self
):
195 cfg
= textwrap
.dedent("""\
197 based_on_style = pep8
198 I18N_FUNCTION_CALL = N_, V_, T_
200 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
201 cfg
= style
.CreateStyleFromConfig(filepath
)
202 self
.assertTrue(_LooksLikePEP8Style(cfg
))
203 self
.assertEqual(cfg
['I18N_FUNCTION_CALL'], ['N_', 'V_', 'T_'])
205 def testErrorNoStyleFile(self
):
206 with self
.assertRaisesRegex(style
.StyleConfigError
,
207 'is not a valid style or file path'):
208 style
.CreateStyleFromConfig('/8822/xyznosuchfile')
210 def testErrorNoStyleSection(self
):
211 cfg
= textwrap
.dedent("""\
215 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
216 with self
.assertRaisesRegex(style
.StyleConfigError
,
217 'Unable to find section'):
218 style
.CreateStyleFromConfig(filepath
)
220 def testErrorUnknownStyleOption(self
):
221 cfg
= textwrap
.dedent("""\
226 with utils
.TempFileContents(self
.test_tmpdir
, cfg
) as filepath
:
227 with self
.assertRaisesRegex(style
.StyleConfigError
,
228 'Unknown style option'):
229 style
.CreateStyleFromConfig(filepath
)
231 def testPyprojectTomlNoYapfSection(self
):
233 import tomli
# noqa: F401
237 filepath
= os
.path
.join(self
.test_tmpdir
, 'pyproject.toml')
238 _
= open(filepath
, 'w')
239 with self
.assertRaisesRegex(style
.StyleConfigError
,
240 'Unable to find section'):
241 style
.CreateStyleFromConfig(filepath
)
243 def testPyprojectTomlParseYapfSection(self
):
245 import tomli
# noqa: F401
249 cfg
= textwrap
.dedent("""\
251 based_on_style = "pep8"
252 continuation_indent_width = 40
254 filepath
= os
.path
.join(self
.test_tmpdir
, 'pyproject.toml')
255 with
open(filepath
, 'w') as f
:
257 cfg
= style
.CreateStyleFromConfig(filepath
)
258 self
.assertTrue(_LooksLikePEP8Style(cfg
))
259 self
.assertEqual(cfg
['CONTINUATION_INDENT_WIDTH'], 40)
262 class StyleFromDict(yapf_test_helper
.YAPFTest
):
265 def setUpClass(cls
): # pylint: disable=g-missing-super-call
266 style
.SetGlobalStyle(style
.CreatePEP8Style())
268 def testDefaultBasedOnStyle(self
):
270 'based_on_style': 'pep8',
272 'blank_line_before_nested_class_or_def': True
274 cfg
= style
.CreateStyleFromConfig(config_dict
)
275 self
.assertTrue(_LooksLikePEP8Style(cfg
))
276 self
.assertEqual(cfg
['INDENT_WIDTH'], 2)
278 def testDefaultBasedOnStyleBadDict(self
):
279 self
.assertRaisesRegex(style
.StyleConfigError
, 'Unknown style option',
280 style
.CreateStyleFromConfig
,
281 {'based_on_styl': 'pep8'})
282 self
.assertRaisesRegex(style
.StyleConfigError
, 'not a valid',
283 style
.CreateStyleFromConfig
,
284 {'INDENT_WIDTH': 'FOUR'})
287 class StyleFromCommandLine(yapf_test_helper
.YAPFTest
):
290 def setUpClass(cls
): # pylint: disable=g-missing-super-call
291 style
.SetGlobalStyle(style
.CreatePEP8Style())
293 def testDefaultBasedOnStyle(self
):
294 cfg
= style
.CreateStyleFromConfig(
295 '{based_on_style: pep8,'
297 ' blank_line_before_nested_class_or_def: True}')
298 self
.assertTrue(_LooksLikePEP8Style(cfg
))
299 self
.assertEqual(cfg
['INDENT_WIDTH'], 2)
301 def testDefaultBasedOnStyleNotStrict(self
):
302 cfg
= style
.CreateStyleFromConfig(
303 '{based_on_style : pep8,'
305 ' blank_line_before_nested_class_or_def:True}')
306 self
.assertTrue(_LooksLikePEP8Style(cfg
))
307 self
.assertEqual(cfg
['INDENT_WIDTH'], 2)
309 def testDefaultBasedOnExplicitlyUnicodeTypeString(self
):
310 cfg
= style
.CreateStyleFromConfig('{}')
311 self
.assertIsInstance(cfg
, dict)
313 def testDefaultBasedOnDetaultTypeString(self
):
314 cfg
= style
.CreateStyleFromConfig('{}')
315 self
.assertIsInstance(cfg
, dict)
317 def testDefaultBasedOnStyleBadString(self
):
318 self
.assertRaisesRegex(style
.StyleConfigError
, 'Unknown style option',
319 style
.CreateStyleFromConfig
, '{based_on_styl: pep8}')
320 self
.assertRaisesRegex(style
.StyleConfigError
, 'not a valid',
321 style
.CreateStyleFromConfig
, '{INDENT_WIDTH: FOUR}')
322 self
.assertRaisesRegex(style
.StyleConfigError
, 'Invalid style dict',
323 style
.CreateStyleFromConfig
, '{based_on_style: pep8')
326 class StyleHelp(yapf_test_helper
.YAPFTest
):
328 def testHelpKeys(self
):
329 settings
= sorted(style
.Help())
330 expected
= sorted(style
._style
)
331 self
.assertListEqual(settings
, expected
)
334 if __name__
== '__main__':