mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
Collapse else: if: ... into elif:
This commit is contained in:
parent
719e4e3ba5
commit
8d6d760422
2 changed files with 32 additions and 1 deletions
|
@ -64,6 +64,24 @@ class_decorator = """\
|
||||||
class Foo: pass
|
class Foo: pass
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
elif1 = """\
|
||||||
|
if cond1:
|
||||||
|
suite1
|
||||||
|
elif cond2:
|
||||||
|
suite2
|
||||||
|
else:
|
||||||
|
suite3
|
||||||
|
"""
|
||||||
|
|
||||||
|
elif2 = """\
|
||||||
|
if cond1:
|
||||||
|
suite1
|
||||||
|
elif cond2:
|
||||||
|
suite2
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ASTTestCase(unittest.TestCase):
|
class ASTTestCase(unittest.TestCase):
|
||||||
def assertASTEqual(self, ast1, ast2):
|
def assertASTEqual(self, ast1, ast2):
|
||||||
self.assertEqual(ast.dump(ast1), ast.dump(ast2))
|
self.assertEqual(ast.dump(ast1), ast.dump(ast2))
|
||||||
|
@ -159,6 +177,10 @@ class UnparseTestCase(ASTTestCase):
|
||||||
def test_class_definition(self):
|
def test_class_definition(self):
|
||||||
self.check_roundtrip("class A(metaclass=type, *[], **{}): pass")
|
self.check_roundtrip("class A(metaclass=type, *[], **{}): pass")
|
||||||
|
|
||||||
|
def test_elifs(self):
|
||||||
|
self.check_roundtrip(elif1)
|
||||||
|
self.check_roundtrip(elif2)
|
||||||
|
|
||||||
class DirectoryTestCase(ASTTestCase):
|
class DirectoryTestCase(ASTTestCase):
|
||||||
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
|
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
|
||||||
|
|
||||||
|
|
|
@ -256,9 +256,18 @@ class Unparser:
|
||||||
self.fill("if ")
|
self.fill("if ")
|
||||||
self.dispatch(t.test)
|
self.dispatch(t.test)
|
||||||
self.enter()
|
self.enter()
|
||||||
# XXX elif?
|
|
||||||
self.dispatch(t.body)
|
self.dispatch(t.body)
|
||||||
self.leave()
|
self.leave()
|
||||||
|
# collapse nested ifs into equivalent elifs.
|
||||||
|
while (t.orelse and len(t.orelse) == 1 and
|
||||||
|
isinstance(t.orelse[0], ast.If)):
|
||||||
|
t = t.orelse[0]
|
||||||
|
self.fill("elif ")
|
||||||
|
self.dispatch(t.test)
|
||||||
|
self.enter()
|
||||||
|
self.dispatch(t.body)
|
||||||
|
self.leave()
|
||||||
|
# final else
|
||||||
if t.orelse:
|
if t.orelse:
|
||||||
self.fill("else")
|
self.fill("else")
|
||||||
self.enter()
|
self.enter()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue