Update chromium style to match what's used in Chromium (#789)

* Rename "chromium" style to "yapf".

Chromium has decided to just follow PEP-8

Relevant Chromium style discussion:
https://groups.google.com/a/chromium.org/g/chromium-dev/c/RcJgJdkNIdg/discussion

* Added "yapf" to the style list.

Co-authored-by: Andrew Grieve <agrieve@google.com>
Co-authored-by: Bill Wendling <morbo@google.com>
Co-authored-by: Bill Wendling <5993918+gwelymernans@users.noreply.github.com>
pull/837/head
agrieve 2 years ago committed by GitHub
parent 15ae9f63b1
commit 22ef70f3c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .style.yapf
  2. 2
      CHANGELOG
  3. 5
      CONTRIBUTING.rst
  4. 8
      README.rst
  5. 9
      yapf/yapflib/style.py
  6. 2
      yapftests/blank_line_calculator_test.py
  7. 2
      yapftests/format_decision_state_test.py
  8. 6
      yapftests/main_test.py
  9. 124
      yapftests/reformatter_basic_test.py
  10. 8
      yapftests/reformatter_buganizer_test.py
  11. 2
      yapftests/reformatter_style_config_test.py
  12. 2
      yapftests/split_penalty_test.py
  13. 50
      yapftests/style_test.py
  14. 44
      yapftests/yapf_test.py

@ -1,3 +1,2 @@
[style]
# YAPF uses the chromium style
based_on_style = chromium
based_on_style = yapf

@ -13,6 +13,8 @@
parentheses.
- New knob `SPACES_AROUND_SUBSCRIPT_COLON` to add spaces around the subscript /
slice operator.
### Changed
- Renamed "chromium" style to "yapf". Chromium will now use PEP-8 directly.
### Fixed
- Honor a disable directive at the end of a multiline comment.
- Don't require splitting before comments in a list when

@ -27,9 +27,8 @@ use Github pull requests for this purpose.
YAPF coding style
-----------------
YAPF follows the `Chromium Python Style Guide
<https://www.chromium.org/chromium-os/python-style-guidelines>`_. It's the same
as the Google Python Style guide with two exceptions:
YAPF follows the `Google Python Style Guide
<https://google.github.io/styleguide/pyguide.html>`_ with two exceptions:
- 2 spaces for indentation rather than 4.
- CamelCase for function and method names rather than snake_case.

@ -178,8 +178,8 @@ with a ``[yapf]`` heading. For example:
The ``based_on_style`` setting determines which of the predefined styles this
custom style is based on (think of it like subclassing). Four
styles are predefined: ``pep8`` (default), ``chromium``, ``google`` and
``facebook`` (see ``_STYLE_NAME_TO_FACTORY`` in style.py_).
styles are predefined: ``pep8`` (default), ``google``, ``yapf``, and ``facebook``
(see ``_STYLE_NAME_TO_FACTORY`` in style.py_).
.. _style.py: https://github.com/google/yapf/blob/master/yapf/yapflib/style.py#L445
@ -188,9 +188,9 @@ example:
.. code-block:: shell
--style='{based_on_style: chromium, indent_width: 4}'
--style='{based_on_style: pep8, indent_width: 2}'
This will take the ``chromium`` base style and modify it to have four space
This will take the ``pep8`` base style and modify it to have two space
indentations.
YAPF will search for the formatting style in the following manner:

@ -494,12 +494,11 @@ def CreateGoogleStyle():
return style
def CreateChromiumStyle():
"""Create the Chromium formatting style."""
def CreateYapfStyle():
"""Create the YAPF formatting style."""
style = CreateGoogleStyle()
style['ALLOW_MULTILINE_DICTIONARY_KEYS'] = True
style['ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS'] = False
style['INDENT_DICTIONARY_VALUE'] = True
style['INDENT_WIDTH'] = 2
style['SPLIT_BEFORE_BITWISE_OPERATOR'] = True
style['SPLIT_BEFORE_DOT'] = True
@ -527,16 +526,16 @@ def CreateFacebookStyle():
_STYLE_NAME_TO_FACTORY = dict(
pep8=CreatePEP8Style,
chromium=CreateChromiumStyle,
google=CreateGoogleStyle,
facebook=CreateFacebookStyle,
yapf=CreateYapfStyle,
)
_DEFAULT_STYLE_TO_FACTORY = [
(CreateChromiumStyle(), CreateChromiumStyle),
(CreateFacebookStyle(), CreateFacebookStyle),
(CreateGoogleStyle(), CreateGoogleStyle),
(CreatePEP8Style(), CreatePEP8Style),
(CreateYapfStyle(), CreateYapfStyle),
]

@ -27,7 +27,7 @@ class BasicBlankLineCalculatorTest(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testDecorators(self):
unformatted_code = textwrap.dedent("""\

@ -28,7 +28,7 @@ class FormatDecisionStateTest(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testSimpleFunctionDefWithNoSplitting(self):
code = textwrap.dedent(r"""

@ -113,12 +113,12 @@ class MainTest(unittest.TestCase):
def testEchoInputWithStyle(self):
code = 'def f(a = 1):\n return 2*a\n'
chromium_code = 'def f(a=1):\n return 2 * a\n'
yapf_code = 'def f(a=1):\n return 2 * a\n'
with patched_input(code):
with captured_output() as (out, _):
ret = yapf.main(['-', '--style=chromium'])
ret = yapf.main(['-', '--style=yapf'])
self.assertEqual(ret, 0)
self.assertEqual(out.getvalue(), chromium_code)
self.assertEqual(out.getvalue(), yapf_code)
def testEchoBadInput(self):
bad_syntax = ' a = 1\n'

@ -26,8 +26,8 @@ from yapftests import yapf_test_helper
class BasicReformatterTest(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls): # pylint: disable=g-missing-super-call
style.SetGlobalStyle(style.CreateChromiumStyle())
def setUpClass(cls):
style.SetGlobalStyle(style.CreateYapfStyle())
def testSplittingAllArgs(self):
style.SetGlobalStyle(
@ -306,13 +306,13 @@ class foo(object):\n \n def foobar(self):\n \n pass\n \n def barfoo(se
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, indent_blank_lines: true}'))
'{based_on_style: yapf, indent_blank_lines: true}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
unformatted_code, expected_formatted_code = (expected_formatted_code,
unformatted_code)
@ -1850,12 +1850,12 @@ class A(object):
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, allow_multiline_lambdas: true}'))
'{based_on_style: yapf, allow_multiline_lambdas: true}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testMultilineDictionaryKeys(self):
unformatted_code = textwrap.dedent("""\
@ -1882,13 +1882,13 @@ class A(object):
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium, '
style.CreateStyleFromConfig('{based_on_style: yapf, '
'allow_multiline_dictionary_keys: true}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testStableDictionaryFormatting(self):
code = textwrap.dedent("""\
@ -1919,7 +1919,34 @@ class A(object):
reformatted_code = reformatter.Reformat(uwlines)
self.assertCodeEqual(code, reformatted_code)
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testStableInlinedDictionaryFormatting(self):
try:
style.SetGlobalStyle(style.CreatePEP8Style())
unformatted_code = textwrap.dedent("""\
def _():
url = "http://{0}/axis-cgi/admin/param.cgi?{1}".format(
value, urllib.urlencode({'action': 'update', 'parameter': value}))
""")
expected_formatted_code = textwrap.dedent("""\
def _():
url = "http://{0}/axis-cgi/admin/param.cgi?{1}".format(
value, urllib.urlencode({
'action': 'update',
'parameter': value
}))
""")
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
reformatted_code = reformatter.Reformat(uwlines)
self.assertCodeEqual(expected_formatted_code, reformatted_code)
uwlines = yapf_test_helper.ParseAndUnwrap(reformatted_code)
reformatted_code = reformatter.Reformat(uwlines)
self.assertCodeEqual(expected_formatted_code, reformatted_code)
finally:
style.SetGlobalStyle(style.CreateYapfStyle())
def testDontSplitKeywordValueArguments(self):
unformatted_code = textwrap.dedent("""\
@ -1990,7 +2017,7 @@ class A(object):
reformatted_code = reformatter.Reformat(uwlines)
self.assertCodeEqual(code, reformatted_code)
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testNotSplittingAfterSubscript(self):
unformatted_code = textwrap.dedent("""\
@ -2102,7 +2129,7 @@ class A(object):
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, '
'{based_on_style: yapf, '
'split_arguments_when_comma_terminated: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
@ -2113,7 +2140,7 @@ class A(object):
reformatted_code = reformatter.Reformat(uwlines)
self.assertCodeEqual(expected_formatted_code, reformatted_code)
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testImportAsList(self):
code = textwrap.dedent("""\
@ -2402,14 +2429,14 @@ class A(object):
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, '
'{based_on_style: yapf, '
'blank_line_before_class_docstring: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testBlankLineBeforeModuleDocstring(self):
unformatted_code = textwrap.dedent('''\
@ -2464,7 +2491,7 @@ class A(object):
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testTupleCohesion(self):
unformatted_code = textwrap.dedent("""\
@ -2559,13 +2586,13 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, split_before_first_argument: True}'))
'{based_on_style: yapf, split_before_first_argument: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testSplittingBeforeFirstArgumentOnFunctionDefinition(self):
"""Tests split_before_first_argument on a function definition."""
@ -2583,13 +2610,13 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, split_before_first_argument: True}'))
'{based_on_style: yapf, split_before_first_argument: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testSplittingBeforeFirstArgumentOnCompoundStatement(self):
"""Tests split_before_first_argument on a compound statement."""
@ -2609,13 +2636,13 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, split_before_first_argument: True}'))
'{based_on_style: yapf, split_before_first_argument: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testCoalesceBracketsOnDict(self):
"""Tests coalesce_brackets on a dictionary."""
@ -2645,13 +2672,13 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, coalesce_brackets: True}'))
'{based_on_style: yapf, coalesce_brackets: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testSplitAfterComment(self):
code = textwrap.dedent("""\
@ -2666,13 +2693,32 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig(
'{based_on_style: chromium, coalesce_brackets: True, '
'{based_on_style: yapf, coalesce_brackets: True, '
'dedent_closing_brackets: true}'))
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateYapfStyle())
@unittest.skipUnless(not py3compat.PY3, 'Requires Python 2.7')
def testAsyncAsNonKeyword(self):
try:
style.SetGlobalStyle(style.CreatePEP8Style())
# In Python 2, async may be used as a non-keyword identifier.
code = textwrap.dedent("""\
from util import async
class A(object):
def foo(self):
async.run()
""")
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testDisableEndingCommaHeuristic(self):
code = textwrap.dedent("""\
@ -2681,13 +2727,13 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium,'
style.CreateStyleFromConfig('{based_on_style: yapf,'
' disable_ending_comma_heuristic: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testDedentClosingBracketsWithTypeAnnotationExceedingLineLength(self):
unformatted_code = textwrap.dedent("""\
@ -2713,14 +2759,14 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium,'
style.CreateStyleFromConfig('{based_on_style: yapf,'
' dedent_closing_brackets: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testIndentClosingBracketsWithTypeAnnotationExceedingLineLength(self):
unformatted_code = textwrap.dedent("""\
@ -2746,14 +2792,14 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium,'
style.CreateStyleFromConfig('{based_on_style: yapf,'
' indent_closing_brackets: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testIndentClosingBracketsInFunctionCall(self):
unformatted_code = textwrap.dedent("""\
@ -2781,14 +2827,14 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium,'
style.CreateStyleFromConfig('{based_on_style: yapf,'
' indent_closing_brackets: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testIndentClosingBracketsInTuple(self):
unformatted_code = textwrap.dedent("""\
@ -2816,14 +2862,14 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium,'
style.CreateStyleFromConfig('{based_on_style: yapf,'
' indent_closing_brackets: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testIndentClosingBracketsInList(self):
unformatted_code = textwrap.dedent("""\
@ -2851,14 +2897,14 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium,'
style.CreateStyleFromConfig('{based_on_style: yapf,'
' indent_closing_brackets: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testIndentClosingBracketsInDict(self):
unformatted_code = textwrap.dedent("""\
@ -2892,14 +2938,14 @@ my_dict = {
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium,'
style.CreateStyleFromConfig('{based_on_style: yapf,'
' indent_closing_brackets: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code,
reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testMultipleDictionariesInList(self):
unformatted_code = textwrap.dedent("""\

@ -26,7 +26,7 @@ class BuganizerFixes(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testB137580392(self):
code = """\
@ -1720,13 +1720,13 @@ class _():
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{based_on_style: chromium, '
style.CreateStyleFromConfig('{based_on_style: yapf, '
'split_before_logical_operator: True}'))
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testB22527411(self):
unformatted_code = textwrap.dedent("""\
@ -1884,7 +1884,7 @@ class _():
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def testB19353268(self):
code = textwrap.dedent("""\

@ -29,7 +29,7 @@ class TestsForStyleConfig(yapf_test_helper.YAPFTest):
def testSetGlobalStyle(self):
try:
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
unformatted_code = textwrap.dedent(u"""\
for i in range(5):
print('bar')

@ -36,7 +36,7 @@ class SplitPenaltyTest(yapf_test_helper.YAPFTest):
@classmethod
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
style.SetGlobalStyle(style.CreateYapfStyle())
def _ParseAndComputePenalties(self, code, dumptree=False):
"""Parses the code and computes split penalties.

@ -74,23 +74,20 @@ class UtilsTest(unittest.TestCase):
self.assertEqual(style._IntOrIntListConverter('1, 2, 3'), [1, 2, 3])
def _LooksLikeChromiumStyle(cfg):
return (cfg['INDENT_WIDTH'] == 2 and
cfg['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF'])
def _LooksLikeGoogleStyle(cfg):
return (cfg['INDENT_WIDTH'] == 4 and
cfg['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF'])
return cfg['COLUMN_LIMIT'] == 80 and cfg['SPLIT_COMPLEX_COMPREHENSION']
def _LooksLikePEP8Style(cfg):
return (cfg['INDENT_WIDTH'] == 4 and
not cfg['BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF'])
return cfg['COLUMN_LIMIT'] == 79
def _LooksLikeFacebookStyle(cfg):
return cfg['INDENT_WIDTH'] == 4 and cfg['DEDENT_CLOSING_BRACKETS']
return cfg['DEDENT_CLOSING_BRACKETS']
def _LooksLikeYapfStyle(cfg):
return cfg['SPLIT_BEFORE_DOT']
class PredefinedStylesByNameTest(unittest.TestCase):
@ -114,10 +111,10 @@ class PredefinedStylesByNameTest(unittest.TestCase):
cfg = style.CreateStyleFromConfig(google_name)
self.assertTrue(_LooksLikeGoogleStyle(cfg))
def testChromiumByName(self):
for chromium_name in ('chromium', 'Chromium', 'CHROMIUM'):
cfg = style.CreateStyleFromConfig(chromium_name)
self.assertTrue(_LooksLikeChromiumStyle(cfg))
def testYapfByName(self):
for yapf_name in ('yapf', 'YAPF'):
cfg = style.CreateStyleFromConfig(yapf_name)
self.assertTrue(_LooksLikeYapfStyle(cfg))
def testFacebookByName(self):
for fb_name in ('facebook', 'FACEBOOK', 'Facebook'):
@ -157,17 +154,6 @@ class StyleFromFileTest(unittest.TestCase):
self.assertTrue(_LooksLikePEP8Style(cfg))
self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 40)
def testDefaultBasedOnChromiumStyle(self):
cfg = textwrap.dedent(u'''\
[style]
based_on_style = chromium
continuation_indent_width = 30
''')
with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:
cfg = style.CreateStyleFromConfig(filepath)
self.assertTrue(_LooksLikeChromiumStyle(cfg))
self.assertEqual(cfg['CONTINUATION_INDENT_WIDTH'], 30)
def testDefaultBasedOnGoogleStyle(self):
cfg = textwrap.dedent(u'''\
[style]
@ -193,25 +179,25 @@ class StyleFromFileTest(unittest.TestCase):
def testBoolOptionValue(self):
cfg = textwrap.dedent(u'''\
[style]
based_on_style = chromium
based_on_style = pep8
SPLIT_BEFORE_NAMED_ASSIGNS=False
split_before_logical_operator = true
''')
with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:
cfg = style.CreateStyleFromConfig(filepath)
self.assertTrue(_LooksLikeChromiumStyle(cfg))
self.assertTrue(_LooksLikePEP8Style(cfg))
self.assertEqual(cfg['SPLIT_BEFORE_NAMED_ASSIGNS'], False)
self.assertEqual(cfg['SPLIT_BEFORE_LOGICAL_OPERATOR'], True)
def testStringListOptionValue(self):
cfg = textwrap.dedent(u'''\
[style]
based_on_style = chromium
based_on_style = pep8
I18N_FUNCTION_CALL = N_, V_, T_
''')
with utils.TempFileContents(self.test_tmpdir, cfg) as filepath:
cfg = style.CreateStyleFromConfig(filepath)
self.assertTrue(_LooksLikeChromiumStyle(cfg))
self.assertTrue(_LooksLikePEP8Style(cfg))
self.assertEqual(cfg['I18N_FUNCTION_CALL'], ['N_', 'V_', 'T_'])
def testErrorNoStyleFile(self):
@ -254,7 +240,7 @@ class StyleFromDict(unittest.TestCase):
'blank_line_before_nested_class_or_def': True
}
cfg = style.CreateStyleFromConfig(config_dict)
self.assertTrue(_LooksLikeChromiumStyle(cfg))
self.assertTrue(_LooksLikePEP8Style(cfg))
self.assertEqual(cfg['INDENT_WIDTH'], 2)
def testDefaultBasedOnStyleBadDict(self):
@ -277,7 +263,7 @@ class StyleFromCommandLine(unittest.TestCase):
'{based_on_style: pep8,'
' indent_width: 2,'
' blank_line_before_nested_class_or_def: True}')
self.assertTrue(_LooksLikeChromiumStyle(cfg))
self.assertTrue(_LooksLikePEP8Style(cfg))
self.assertEqual(cfg['INDENT_WIDTH'], 2)
def testDefaultBasedOnStyleNotStrict(self):
@ -285,7 +271,7 @@ class StyleFromCommandLine(unittest.TestCase):
'{based_on_style : pep8'
' ,indent_width=2'
' blank_line_before_nested_class_or_def:True}')
self.assertTrue(_LooksLikeChromiumStyle(cfg))
self.assertTrue(_LooksLikePEP8Style(cfg))
self.assertEqual(cfg['INDENT_WIDTH'], 2)
def testDefaultBasedOnExplicitlyUnicodeTypeString(self):

@ -43,7 +43,7 @@ class FormatCodeTest(yapf_test_helper.YAPFTest):
def _Check(self, unformatted_code, expected_formatted_code):
formatted_code, _ = yapf_api.FormatCode(
unformatted_code, style_config='chromium')
unformatted_code, style_config='yapf')
self.assertCodeEqual(expected_formatted_code, formatted_code)
def testSimple(self):
@ -95,7 +95,7 @@ class FormatFileTest(unittest.TestCase):
if True:
pass
""")
expected_formatted_code_chromium = textwrap.dedent(u"""\
expected_formatted_code_yapf = textwrap.dedent(u"""\
if True:
pass
""")
@ -103,9 +103,8 @@ class FormatFileTest(unittest.TestCase):
formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')
self.assertCodeEqual(expected_formatted_code_pep8, formatted_code)
formatted_code, _, _ = yapf_api.FormatFile(
filepath, style_config='chromium')
self.assertCodeEqual(expected_formatted_code_chromium, formatted_code)
formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')
self.assertCodeEqual(expected_formatted_code_yapf, formatted_code)
def testDisableLinesPattern(self):
unformatted_code = textwrap.dedent(u"""\
@ -361,8 +360,7 @@ class FormatFileTest(unittest.TestCase):
]
""")
with utils.TempFileContents(self.test_tmpdir, code) as filepath:
formatted_code, _, _ = yapf_api.FormatFile(
filepath, style_config='chromium')
formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')
self.assertCodeEqual(code, formatted_code)
def testDisabledWithPrecedingText(self):
@ -381,15 +379,13 @@ class FormatFileTest(unittest.TestCase):
]
""")
with utils.TempFileContents(self.test_tmpdir, code) as filepath:
formatted_code, _, _ = yapf_api.FormatFile(
filepath, style_config='chromium')
formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')
self.assertCodeEqual(code, formatted_code)
def testCRLFLineEnding(self):
code = u'class _():\r\n pass\r\n'
with utils.TempFileContents(self.test_tmpdir, code) as filepath:
formatted_code, _, _ = yapf_api.FormatFile(
filepath, style_config='chromium')
formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='yapf')
self.assertCodeEqual(code, formatted_code)
@ -502,7 +498,7 @@ class CommandLineTest(unittest.TestCase):
""")
self.assertYapfReformats(unformatted_code, expected_formatted_code)
def testSetChromiumStyle(self):
def testSetYapfStyle(self):
unformatted_code = textwrap.dedent("""\
def foo(): # trail
x = 37
@ -514,9 +510,9 @@ class CommandLineTest(unittest.TestCase):
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--style=chromium'])
extra_options=['--style=yapf'])
def testSetCustomStyleBasedOnChromium(self):
def testSetCustomStyleBasedOnYapf(self):
unformatted_code = textwrap.dedent("""\
def foo(): # trail
x = 37
@ -527,7 +523,7 @@ class CommandLineTest(unittest.TestCase):
""")
style_file = textwrap.dedent(u'''\
[style]
based_on_style = chromium
based_on_style = yapf
spaces_before_comment = 4
''')
with utils.TempFileContents(self.test_tmpdir, style_file) as stylepath:
@ -1164,7 +1160,7 @@ x = {
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--lines', '1-1', '--style', 'chromium'])
extra_options=['--lines', '1-1', '--style', 'yapf'])
def testMultilineCommentFormattingDisabled(self):
unformatted_code = textwrap.dedent("""\
@ -1198,7 +1194,7 @@ x = {
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--lines', '1-1', '--style', 'chromium'])
extra_options=['--lines', '1-1', '--style', 'yapf'])
def testTrailingCommentsWithDisabledFormatting(self):
unformatted_code = textwrap.dedent("""\
@ -1218,7 +1214,7 @@ x = {
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--lines', '1-1', '--style', 'chromium'])
extra_options=['--lines', '1-1', '--style', 'yapf'])
def testUseTabs(self):
unformatted_code = """\
@ -1233,7 +1229,7 @@ def foo_function():
"""
style_contents = u"""\
[style]
based_on_style = chromium
based_on_style = yapf
USE_TABS = true
INDENT_WIDTH=1
"""
@ -1257,7 +1253,7 @@ def f():
"""
style_contents = u"""\
[style]
based_on_style = chromium
based_on_style = yapf
USE_TABS = true
INDENT_WIDTH=1
"""
@ -1282,7 +1278,7 @@ def foo_function(arg1, arg2,
"""
style_contents = u"""\
[style]
based_on_style = chromium
based_on_style = yapf
USE_TABS = true
COLUMN_LIMIT=32
INDENT_WIDTH=4
@ -1310,7 +1306,7 @@ def foo_function(arg1, arg2,
"""
style_contents = u"""\
[style]
based_on_style = chromium
based_on_style = yapf
USE_TABS = true
COLUMN_LIMIT=32
INDENT_WIDTH=4
@ -1407,7 +1403,7 @@ CONTINUATION_ALIGN_STYLE = valign-right
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--style', 'chromium', '--lines', '1-1'])
extra_options=['--style', 'yapf', '--lines', '1-1'])
@unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6')
def testNoSpacesAroundBinaryOperators(self):
@ -1457,7 +1453,7 @@ a = [
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--style', 'chromium', '--lines', '1-100'])
extra_options=['--style', 'yapf', '--lines', '1-100'])
class BadInputTest(unittest.TestCase):

Loading…
Cancel
Save