aboutsummaryrefslogtreecommitdiff
path: root/yapftests/yapf_test.py
diff options
context:
space:
mode:
authorKrzysztof Kosiński <krzysio@google.com>2022-11-02 21:00:14 +0000
committerKrzysztof Kosiński <krzysio@google.com>2022-11-03 01:27:30 +0000
commitf5aef501eca8ae3d683a8e1e6b5e02abb1f9c762 (patch)
treedc88d4ff4bd74b39e2bcd06ea95e79574733f9d9 /yapftests/yapf_test.py
parent77c06b0dd12e88f42f39b0d2c4482180f744769a (diff)
parentd3873c0693fe08e6233177c2f342af62547dcaa6 (diff)
downloadyapf-f5aef501eca8ae3d683a8e1e6b5e02abb1f9c762.tar.gz
Upgrade yapf to v0.32.0.
Test: made a test change in packages/modules/Bluetooth, which uses yapf in a preupload hook Change-Id: I7dc86d095b9d70b2427a4a34cad342f45e413122
Diffstat (limited to 'yapftests/yapf_test.py')
-rw-r--r--yapftests/yapf_test.py796
1 files changed, 733 insertions, 63 deletions
diff --git a/yapftests/yapf_test.py b/yapftests/yapf_test.py
index 6df28b6..2330f4e 100644
--- a/yapftests/yapf_test.py
+++ b/yapftests/yapf_test.py
@@ -24,11 +24,15 @@ import tempfile
import textwrap
import unittest
+from lib2to3.pgen2 import tokenize
+
+from yapf.yapflib import errors
from yapf.yapflib import py3compat
from yapf.yapflib import style
from yapf.yapflib import yapf_api
from yapftests import utils
+from yapftests import yapf_test_helper
ROOT_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
@@ -36,12 +40,12 @@ ROOT_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
YAPF_BINARY = [sys.executable, '-m', 'yapf', '--verify', '--no-local-style']
-class FormatCodeTest(unittest.TestCase):
+class FormatCodeTest(yapf_test_helper.YAPFTest):
def _Check(self, unformatted_code, expected_formatted_code):
formatted_code, _ = yapf_api.FormatCode(
- unformatted_code, style_config='chromium')
- self.assertEqual(expected_formatted_code, formatted_code)
+ unformatted_code, style_config='yapf')
+ self.assertCodeEqual(expected_formatted_code, formatted_code)
def testSimple(self):
unformatted_code = textwrap.dedent("""\
@@ -59,13 +63,19 @@ class FormatCodeTest(unittest.TestCase):
""")
self._Check(unformatted_code, expected_formatted_code)
+ @unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6')
+ def testPrintAfterPeriod(self):
+ unformatted_code = textwrap.dedent("""a.print\n""")
+ expected_formatted_code = textwrap.dedent("""a.print\n""")
+ self._Check(unformatted_code, expected_formatted_code)
+
class FormatFileTest(unittest.TestCase):
- def setUp(self):
+ def setUp(self): # pylint: disable=g-missing-super-call
self.test_tmpdir = tempfile.mkdtemp()
- def tearDown(self):
+ def tearDown(self): # pylint: disable=g-missing-super-call
shutil.rmtree(self.test_tmpdir)
def assertCodeEqual(self, expected_code, code):
@@ -87,7 +97,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
""")
@@ -95,9 +105,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"""\
@@ -187,6 +196,19 @@ class FormatFileTest(unittest.TestCase):
formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')
self.assertCodeEqual(code, formatted_code)
+ def testEnabledDisabledSameComment(self):
+ code = textwrap.dedent(u"""\
+ # yapf: disable
+ a(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddddddddddddddddd, eeeeeeeeeeeeeeeeeeeeeeeeeee)
+ # yapf: enable
+ # yapf: disable
+ a(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccccccccccc, ddddddddddddddddddddddd, eeeeeeeeeeeeeeeeeeeeeeeeeee)
+ # yapf: enable
+ """) # noqa
+ with utils.TempFileContents(self.test_tmpdir, code) as filepath:
+ formatted_code, _, _ = yapf_api.FormatFile(filepath, style_config='pep8')
+ self.assertCodeEqual(code, formatted_code)
+
def testFormatFileLinesSelection(self):
unformatted_code = textwrap.dedent(u"""\
if a: b
@@ -224,7 +246,7 @@ class FormatFileTest(unittest.TestCase):
""")
with utils.TempFileContents(self.test_tmpdir, unformatted_code) as filepath:
diff, _, _ = yapf_api.FormatFile(filepath, print_diff=True)
- self.assertTrue(u'+ pass' in diff)
+ self.assertIn(u'+ pass', diff)
def testFormatFileInPlace(self):
unformatted_code = u'True==False\n'
@@ -340,8 +362,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):
@@ -360,15 +381,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)
@@ -376,11 +395,11 @@ class CommandLineTest(unittest.TestCase):
"""Test how calling yapf from the command line acts."""
@classmethod
- def setUpClass(cls):
+ def setUpClass(cls): # pylint: disable=g-missing-super-call
cls.test_tmpdir = tempfile.mkdtemp()
@classmethod
- def tearDownClass(cls):
+ def tearDownClass(cls): # pylint: disable=g-missing-super-call
shutil.rmtree(cls.test_tmpdir)
def assertYapfReformats(self,
@@ -406,10 +425,12 @@ class CommandLineTest(unittest.TestCase):
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env)
- reformatted_code, stderrdata = p.communicate(unformatted.encode('utf-8'))
+ reformatted_code, stderrdata = p.communicate(
+ unformatted.encode('utf-8-sig'))
self.assertEqual(stderrdata, b'')
self.assertMultiLineEqual(reformatted_code.decode('utf-8'), expected)
+ @unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6')
def testUnicodeEncodingPipedToFile(self):
unformatted_code = textwrap.dedent(u"""\
def foo():
@@ -480,7 +501,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
@@ -492,9 +513,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
@@ -505,8 +526,27 @@ class CommandLineTest(unittest.TestCase):
""")
style_file = textwrap.dedent(u'''\
[style]
- based_on_style = chromium
- SPACES_BEFORE_COMMENT = 4
+ based_on_style = yapf
+ spaces_before_comment = 4
+ ''')
+ with utils.TempFileContents(self.test_tmpdir, style_file) as stylepath:
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=['--style={0}'.format(stylepath)])
+
+ def testSetCustomStyleSpacesBeforeComment(self):
+ unformatted_code = textwrap.dedent("""\
+ a_very_long_statement_that_extends_way_beyond # Comment
+ short # This is a shorter statement
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ a_very_long_statement_that_extends_way_beyond # Comment
+ short # This is a shorter statement
+ """) # noqa
+ style_file = textwrap.dedent(u'''\
+ [style]
+ spaces_before_comment = 15, 20
''')
with utils.TempFileContents(self.test_tmpdir, style_file) as stylepath:
self.assertYapfReformats(
@@ -538,7 +578,8 @@ class CommandLineTest(unittest.TestCase):
try:
subprocess.check_call(YAPF_BINARY + ['--diff', filepath], stdout=out)
except subprocess.CalledProcessError as e:
- self.assertEqual(e.returncode, 1) # Indicates the text changed.
+ # Indicates the text changed.
+ self.assertEqual(e.returncode, 1) # pylint: disable=g-assert-in-except # noqa
def testReformattingSpecificLines(self):
unformatted_code = textwrap.dedent("""\
@@ -550,7 +591,7 @@ class CommandLineTest(unittest.TestCase):
def g():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def h():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
@@ -561,7 +602,7 @@ class CommandLineTest(unittest.TestCase):
def g():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
pass
- """)
+ """) # noqa
# TODO(ambv): the `expected_formatted_code` here is not PEP8 compliant,
# raising "E129 visually indented line with same indent as next logical
# line" with flake8.
@@ -601,7 +642,7 @@ class CommandLineTest(unittest.TestCase):
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
pass
# yapf: enable
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def h():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
@@ -614,7 +655,7 @@ class CommandLineTest(unittest.TestCase):
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
pass
# yapf: enable
- """)
+ """) # noqa
self.assertYapfReformats(unformatted_code, expected_formatted_code)
def testReformattingSkippingToEndOfFile(self):
@@ -634,7 +675,7 @@ class CommandLineTest(unittest.TestCase):
xxxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) ==
'bbbbbbb'):
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def h():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
@@ -653,7 +694,7 @@ class CommandLineTest(unittest.TestCase):
xxxxxxxxxxxxxxxxxxxxx(yyyyyyyyyyyyy[zzzzz].aaaaaaaa[0]) ==
'bbbbbbb'):
pass
- """)
+ """) # noqa
self.assertYapfReformats(unformatted_code, expected_formatted_code)
def testReformattingSkippingSingleLine(self):
@@ -665,7 +706,7 @@ class CommandLineTest(unittest.TestCase):
def g():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'): # yapf: disable
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def h():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
@@ -676,7 +717,7 @@ class CommandLineTest(unittest.TestCase):
def g():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'): # yapf: disable
pass
- """)
+ """) # noqa
self.assertYapfReformats(unformatted_code, expected_formatted_code)
def testDisableWholeDataStructure(self):
@@ -697,12 +738,14 @@ class CommandLineTest(unittest.TestCase):
def testDisableButAdjustIndentations(self):
unformatted_code = textwrap.dedent("""\
class SplitPenaltyTest(unittest.TestCase):
+
def testUnbreakable(self):
self._CheckPenalties(tree, [
]) # yapf: disable
""")
expected_formatted_code = textwrap.dedent("""\
class SplitPenaltyTest(unittest.TestCase):
+
def testUnbreakable(self):
self._CheckPenalties(tree, [
]) # yapf: disable
@@ -718,7 +761,7 @@ class CommandLineTest(unittest.TestCase):
def g():
if (xxxxxxxxxxxx.yyyyyyyy (zzzzzzzzzzzzz [0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'): # yapf: disable
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def h():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
@@ -729,7 +772,7 @@ class CommandLineTest(unittest.TestCase):
def g():
if (xxxxxxxxxxxx.yyyyyyyy (zzzzzzzzzzzzz [0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'): # yapf: disable
pass
- """)
+ """) # noqa
self.assertYapfReformats(unformatted_code, expected_formatted_code)
def testRetainingVerticalWhitespace(self):
@@ -744,7 +787,7 @@ class CommandLineTest(unittest.TestCase):
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
pass
- """)
+ """) # noqa
expected_formatted_code = textwrap.dedent("""\
def h():
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and
@@ -757,7 +800,7 @@ class CommandLineTest(unittest.TestCase):
if (xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0]) == 'aaaaaaaaaaa' and xxxxxxxxxxxx.yyyyyyyy(zzzzzzzzzzzzz[0].mmmmmmmm[0]) == 'bbbbbbb'):
pass
- """)
+ """) # noqa
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
@@ -812,6 +855,28 @@ class CommandLineTest(unittest.TestCase):
self.assertYapfReformats(
unformatted_code, unformatted_code, extra_options=['--lines', '2-2'])
+ def testVerticalSpacingWithCommentWithContinuationMarkers(self):
+ unformatted_code = """\
+# \\
+# \\
+# \\
+
+x = {
+}
+"""
+ expected_formatted_code = """\
+# \\
+# \\
+# \\
+
+x = {
+}
+"""
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=['--lines', '1-1'])
+
def testRetainingSemicolonsWhenSpecifyingLines(self):
unformatted_code = textwrap.dedent("""\
a = line_to_format
@@ -821,8 +886,6 @@ class CommandLineTest(unittest.TestCase):
""")
expected_formatted_code = textwrap.dedent("""\
a = line_to_format
-
-
def f():
x = y + 42; z = n * 42
if True: a += 1 ; b += 1 ; c += 1
@@ -842,11 +905,9 @@ class CommandLineTest(unittest.TestCase):
<b>Residence: </b>"""+palace["Winter"]+"""<br>
</body>
</html>"""
- ''')
+ ''') # noqa
expected_formatted_code = textwrap.dedent('''\
foo = 42
-
-
def f():
email_text += """<html>This is a really long docstring that goes over the column limit and is multi-line.<br><br>
<b>Czar: </b>"""+despot["Nicholas"]+"""<br>
@@ -854,7 +915,7 @@ class CommandLineTest(unittest.TestCase):
<b>Residence: </b>"""+palace["Winter"]+"""<br>
</body>
</html>"""
- ''')
+ ''') # noqa
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
@@ -950,12 +1011,36 @@ class CommandLineTest(unittest.TestCase):
'eeeeeeeeeeeeeeeeeeeeeeeee.%s' % c.ffffffffffff),
gggggggggggg.hhhhhhhhh(c, c.ffffffffffff))
iiiii = jjjjjjjjjjjjjj.iiiii
- """)
+ """) # noqa
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
extra_options=['--lines', '4-7'])
+ def testRetainVerticalFormattingBetweenDisabledLines(self):
+ unformatted_code = textwrap.dedent("""\
+ class A(object):
+ def aaaaaaaaaaaaa(self):
+ pass
+
+
+ def bbbbbbbbbbbbb(self): # 5
+ pass
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ class A(object):
+ def aaaaaaaaaaaaa(self):
+ pass
+
+
+ def bbbbbbbbbbbbb(self): # 5
+ pass
+ """)
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=['--lines', '4-4'])
+
def testFormatLinesSpecifiedInMiddleOfExpression(self):
unformatted_code = textwrap.dedent("""\
class A(object):
@@ -976,7 +1061,7 @@ class CommandLineTest(unittest.TestCase):
'eeeeeeeeeeeeeeeeeeeeeeeee.%s' % c.ffffffffffff),
gggggggggggg.hhhhhhhhh(c, c.ffffffffffff))
iiiii = jjjjjjjjjjjjjj.iiiii
- """)
+ """) # noqa
self.assertYapfReformats(
unformatted_code,
expected_formatted_code,
@@ -1046,7 +1131,7 @@ class CommandLineTest(unittest.TestCase):
first_argument_on_the_same_line, second_argument_makes_the_line_too_long
):
pass
- """)
+ """) # noqa
self.assertYapfReformats(
unformatted_code,
expected_formatted_fb_code,
@@ -1087,20 +1172,19 @@ class CommandLineTest(unittest.TestCase):
def testPseudoParenSpaces(self):
unformatted_code = textwrap.dedent("""\
- def foo():
+ def foo():
def bar():
return {msg_id: author for author, msg_id in reader}
""")
expected_formatted_code = textwrap.dedent("""\
def foo():
-
def bar():
return {msg_id: author for author, msg_id in reader}
""")
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("""\
@@ -1134,7 +1218,7 @@ class CommandLineTest(unittest.TestCase):
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("""\
@@ -1154,7 +1238,7 @@ class CommandLineTest(unittest.TestCase):
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 = """\
@@ -1166,10 +1250,10 @@ def foo_function():
def foo_function():
if True:
pass
-"""
+""" # noqa: W191,E101
style_contents = u"""\
[style]
-based_on_style = chromium
+based_on_style = yapf
USE_TABS = true
INDENT_WIDTH=1
"""
@@ -1190,10 +1274,10 @@ def f():
'hello',
'world',
]
-"""
+""" # noqa: W191,E101
style_contents = u"""\
[style]
-based_on_style = chromium
+based_on_style = yapf
USE_TABS = true
INDENT_WIDTH=1
"""
@@ -1209,16 +1293,16 @@ def foo_function(arg1, arg2, arg3):
return ['hello', 'world',]
"""
expected_formatted_code = """\
-def foo_function(arg1, arg2,
- arg3):
+def foo_function(
+ arg1, arg2, arg3):
return [
'hello',
'world',
]
-"""
+""" # noqa: W191,E101
style_contents = u"""\
[style]
-based_on_style = chromium
+based_on_style = yapf
USE_TABS = true
COLUMN_LIMIT=32
INDENT_WIDTH=4
@@ -1243,10 +1327,10 @@ def foo_function(arg1, arg2,
'hello',
'world',
]
-"""
+""" # noqa: W191,E101
style_contents = u"""\
[style]
-based_on_style = chromium
+based_on_style = yapf
USE_TABS = true
COLUMN_LIMIT=32
INDENT_WIDTH=4
@@ -1259,6 +1343,60 @@ CONTINUATION_ALIGN_STYLE = valign-right
expected_formatted_code,
extra_options=['--style={0}'.format(stylepath)])
+ def testUseSpacesContinuationAlignStyleFixed(self):
+ unformatted_code = """\
+def foo_function(arg1, arg2, arg3):
+ return ['hello', 'world',]
+"""
+ expected_formatted_code = """\
+def foo_function(
+ arg1, arg2, arg3):
+ return [
+ 'hello',
+ 'world',
+ ]
+"""
+ style_contents = u"""\
+[style]
+based_on_style = yapf
+COLUMN_LIMIT=32
+INDENT_WIDTH=4
+CONTINUATION_INDENT_WIDTH=8
+CONTINUATION_ALIGN_STYLE = fixed
+"""
+ with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=['--style={0}'.format(stylepath)])
+
+ def testUseSpacesContinuationAlignStyleVAlignRight(self):
+ unformatted_code = """\
+def foo_function(arg1, arg2, arg3):
+ return ['hello', 'world',]
+"""
+ expected_formatted_code = """\
+def foo_function(arg1, arg2,
+ arg3):
+ return [
+ 'hello',
+ 'world',
+ ]
+"""
+ style_contents = u"""\
+[style]
+based_on_style = yapf
+COLUMN_LIMIT=32
+INDENT_WIDTH=4
+CONTINUATION_INDENT_WIDTH=8
+CONTINUATION_ALIGN_STYLE = valign-right
+"""
+ with utils.TempFileContents(self.test_tmpdir, style_contents) as stylepath:
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=['--style={0}'.format(stylepath)])
+
def testStyleOutputRoundTrip(self):
unformatted_code = textwrap.dedent("""\
def foo_function():
@@ -1343,7 +1481,48 @@ 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'])
+
+ def testDisableWithLinesOption(self):
+ unformatted_code = textwrap.dedent("""\
+ # yapf_lines_bug.py
+ # yapf: disable
+ def outer_func():
+ def inner_func():
+ return
+ return
+ # yapf: enable
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ # yapf_lines_bug.py
+ # yapf: disable
+ def outer_func():
+ def inner_func():
+ return
+ return
+ # yapf: enable
+ """)
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=['--lines', '1-8'])
+
+ @unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6')
+ def testNoSpacesAroundBinaryOperators(self):
+ unformatted_code = """\
+a = 4-b/c@d**37
+"""
+ expected_formatted_code = """\
+a = 4-b / c@d**37
+"""
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=[
+ '--style',
+ '{based_on_style: pep8, '
+ 'no_spaces_around_selected_binary_operators: "@,**,-"}',
+ ])
@unittest.skipUnless(py3compat.PY36, 'Requires Python 3.6')
def testCP936Encoding(self):
@@ -1354,13 +1533,41 @@ CONTINUATION_ALIGN_STYLE = valign-right
expected_formatted_code,
env={'PYTHONIOENCODING': 'cp936'})
+ def testDisableWithLineRanges(self):
+ unformatted_code = """\
+# yapf: disable
+a = [
+ 1,
+ 2,
+
+ 3
+]
+"""
+ expected_formatted_code = """\
+# yapf: disable
+a = [
+ 1,
+ 2,
+
+ 3
+]
+"""
+ self.assertYapfReformats(
+ unformatted_code,
+ expected_formatted_code,
+ extra_options=['--style', 'yapf', '--lines', '1-100'])
+
class BadInputTest(unittest.TestCase):
"""Test yapf's behaviour when passed bad input."""
def testBadSyntax(self):
code = ' a = 1\n'
- self.assertRaises(SyntaxError, yapf_api.FormatCode, code)
+ self.assertRaises(errors.YapfError, yapf_api.FormatCode, code)
+
+ def testBadCode(self):
+ code = 'x = """hello\n'
+ self.assertRaises(errors.YapfError, yapf_api.FormatCode, code)
class DiffIndentTest(unittest.TestCase):
@@ -1389,5 +1596,468 @@ class DiffIndentTest(unittest.TestCase):
self._Check(unformatted_code, expected_formatted_code)
+class HorizontallyAlignedTrailingCommentsTest(yapf_test_helper.YAPFTest):
+
+ @staticmethod
+ def _OwnStyle():
+ my_style = style.CreatePEP8Style()
+ my_style['SPACES_BEFORE_COMMENT'] = [
+ 15,
+ 25,
+ 35,
+ ]
+ return my_style
+
+ def _Check(self, unformatted_code, expected_formatted_code):
+ formatted_code, _ = yapf_api.FormatCode(
+ unformatted_code, style_config=style.SetGlobalStyle(self._OwnStyle()))
+ self.assertCodeEqual(expected_formatted_code, formatted_code)
+
+ def testSimple(self):
+ unformatted_code = textwrap.dedent("""\
+ foo = '1' # Aligned at first list value
+
+ foo = '2__<15>' # Aligned at second list value
+
+ foo = '3____________<25>' # Aligned at third list value
+
+ foo = '4______________________<35>' # Aligned beyond list values
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ foo = '1' # Aligned at first list value
+
+ foo = '2__<15>' # Aligned at second list value
+
+ foo = '3____________<25>' # Aligned at third list value
+
+ foo = '4______________________<35>' # Aligned beyond list values
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testBlock(self):
+ unformatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testBlockWithLongLine(self):
+ unformatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func___________________(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func___________________(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testBlockFuncSuffix(self):
+ unformatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+
+ def Func():
+ pass
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+
+
+ def Func():
+ pass
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testBlockCommentSuffix(self):
+ unformatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5 - SpliceComments makes this part of the previous block
+ # Line 6
+
+ # Aligned with prev comment block
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5 - SpliceComments makes this part of the previous block
+ # Line 6
+
+ # Aligned with prev comment block
+ """) # noqa
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testBlockIndentedFuncSuffix(self):
+ unformatted_code = textwrap.dedent("""\
+ if True:
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5 - SpliceComments makes this a new block
+ # Line 6
+
+ # Aligned with Func
+
+ def Func():
+ pass
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ if True:
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+
+ # Line 5 - SpliceComments makes this a new block
+ # Line 6
+
+ # Aligned with Func
+
+
+ def Func():
+ pass
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testBlockIndentedCommentSuffix(self):
+ unformatted_code = textwrap.dedent("""\
+ if True:
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+
+ # Not aligned
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ if True:
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+
+ # Not aligned
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testBlockMultiIndented(self):
+ unformatted_code = textwrap.dedent("""\
+ if True:
+ if True:
+ if True:
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+
+ # Not aligned
+ """) # noqa
+ expected_formatted_code = textwrap.dedent("""\
+ if True:
+ if True:
+ if True:
+ func(1) # Line 1
+ func(2) # Line 2
+ # Line 3
+ func(3) # Line 4
+ # Line 5
+ # Line 6
+
+ # Not aligned
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testArgs(self):
+ unformatted_code = textwrap.dedent("""\
+ def MyFunc(
+ arg1, # Desc 1
+ arg2, # Desc 2
+ a_longer_var_name, # Desc 3
+ arg4,
+ arg5, # Desc 5
+ arg6,
+ ):
+ pass
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ def MyFunc(
+ arg1, # Desc 1
+ arg2, # Desc 2
+ a_longer_var_name, # Desc 3
+ arg4,
+ arg5, # Desc 5
+ arg6,
+ ):
+ pass
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testDisableBlock(self):
+ unformatted_code = textwrap.dedent("""\
+ a() # comment 1
+ b() # comment 2
+
+ # yapf: disable
+ c() # comment 3
+ d() # comment 4
+ # yapf: enable
+
+ e() # comment 5
+ f() # comment 6
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ a() # comment 1
+ b() # comment 2
+
+ # yapf: disable
+ c() # comment 3
+ d() # comment 4
+ # yapf: enable
+
+ e() # comment 5
+ f() # comment 6
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+ def testDisabledLine(self):
+ unformatted_code = textwrap.dedent("""\
+ short # comment 1
+ do_not_touch1 # yapf: disable
+ do_not_touch2 # yapf: disable
+ a_longer_statement # comment 2
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ short # comment 1
+ do_not_touch1 # yapf: disable
+ do_not_touch2 # yapf: disable
+ a_longer_statement # comment 2
+ """)
+ self._Check(unformatted_code, expected_formatted_code)
+
+
+class _SpacesAroundDictListTupleTestImpl(unittest.TestCase):
+
+ @staticmethod
+ def _OwnStyle():
+ my_style = style.CreatePEP8Style()
+ my_style['DISABLE_ENDING_COMMA_HEURISTIC'] = True
+ my_style['SPLIT_ALL_COMMA_SEPARATED_VALUES'] = False
+ my_style['SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED'] = False
+ return my_style
+
+ def _Check(self, unformatted_code, expected_formatted_code):
+ formatted_code, _ = yapf_api.FormatCode(
+ unformatted_code, style_config=style.SetGlobalStyle(self._OwnStyle()))
+ self.assertEqual(expected_formatted_code, formatted_code)
+
+ def setUp(self):
+ self.maxDiff = None
+
+
+class SpacesAroundDictTest(_SpacesAroundDictListTupleTestImpl):
+
+ @classmethod
+ def _OwnStyle(cls):
+ style = super(SpacesAroundDictTest, cls)._OwnStyle()
+ style['SPACES_AROUND_DICT_DELIMITERS'] = True
+
+ return style
+
+ def testStandard(self):
+ unformatted_code = textwrap.dedent("""\
+ {1 : 2}
+ {k:v for k, v in other.items()}
+ {k for k in [1, 2, 3]}
+
+ # The following statements should not change
+ {}
+ {1 : 2} # yapf: disable
+
+ # yapf: disable
+ {1 : 2}
+ # yapf: enable
+
+ # Dict settings should not impact lists or tuples
+ [1, 2]
+ (3, 4)
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ { 1: 2 }
+ { k: v for k, v in other.items() }
+ { k for k in [1, 2, 3] }
+
+ # The following statements should not change
+ {}
+ {1 : 2} # yapf: disable
+
+ # yapf: disable
+ {1 : 2}
+ # yapf: enable
+
+ # Dict settings should not impact lists or tuples
+ [1, 2]
+ (3, 4)
+ """)
+
+ self._Check(unformatted_code, expected_formatted_code)
+
+
+class SpacesAroundListTest(_SpacesAroundDictListTupleTestImpl):
+
+ @classmethod
+ def _OwnStyle(cls):
+ style = super(SpacesAroundListTest, cls)._OwnStyle()
+ style['SPACES_AROUND_LIST_DELIMITERS'] = True
+
+ return style
+
+ def testStandard(self):
+ unformatted_code = textwrap.dedent("""\
+ [a,b,c]
+ [4,5,]
+ [6, [7, 8], 9]
+ [v for v in [1,2,3] if v & 1]
+
+ # The following statements should not change
+ index[0]
+ index[a, b]
+ []
+ [v for v in [1,2,3] if v & 1] # yapf: disable
+
+ # yapf: disable
+ [a,b,c]
+ [4,5,]
+ # yapf: enable
+
+ # List settings should not impact dicts or tuples
+ {a: b}
+ (1, 2)
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ [ a, b, c ]
+ [ 4, 5, ]
+ [ 6, [ 7, 8 ], 9 ]
+ [ v for v in [ 1, 2, 3 ] if v & 1 ]
+
+ # The following statements should not change
+ index[0]
+ index[a, b]
+ []
+ [v for v in [1,2,3] if v & 1] # yapf: disable
+
+ # yapf: disable
+ [a,b,c]
+ [4,5,]
+ # yapf: enable
+
+ # List settings should not impact dicts or tuples
+ {a: b}
+ (1, 2)
+ """)
+
+ self._Check(unformatted_code, expected_formatted_code)
+
+
+class SpacesAroundTupleTest(_SpacesAroundDictListTupleTestImpl):
+
+ @classmethod
+ def _OwnStyle(cls):
+ style = super(SpacesAroundTupleTest, cls)._OwnStyle()
+ style['SPACES_AROUND_TUPLE_DELIMITERS'] = True
+
+ return style
+
+ def testStandard(self):
+ unformatted_code = textwrap.dedent("""\
+ (0, 1)
+ (2, 3)
+ (4, 5, 6,)
+ func((7, 8), 9)
+
+ # The following statements should not change
+ func(1, 2)
+ (this_func or that_func)(3, 4)
+ if (True and False): pass
+ ()
+
+ (0, 1) # yapf: disable
+
+ # yapf: disable
+ (0, 1)
+ (2, 3)
+ # yapf: enable
+
+ # Tuple settings should not impact dicts or lists
+ {a: b}
+ [3, 4]
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ ( 0, 1 )
+ ( 2, 3 )
+ ( 4, 5, 6, )
+ func(( 7, 8 ), 9)
+
+ # The following statements should not change
+ func(1, 2)
+ (this_func or that_func)(3, 4)
+ if (True and False): pass
+ ()
+
+ (0, 1) # yapf: disable
+
+ # yapf: disable
+ (0, 1)
+ (2, 3)
+ # yapf: enable
+
+ # Tuple settings should not impact dicts or lists
+ {a: b}
+ [3, 4]
+ """)
+
+ self._Check(unformatted_code, expected_formatted_code)
+
+
if __name__ == '__main__':
unittest.main()