diff options
author | Batuhan Taşkaya <47358913+isidentical@users.noreply.github.com> | 2020-03-01 20:12:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-01 20:12:17 (GMT) |
commit | 397b96f6d7a89f778ebc0591e32216a8183fe667 (patch) | |
tree | bca7cb5940e1a5cb1cdc80b3578a238dfac318d1 /Lib/test/test_unparse.py | |
parent | 185903de12de8837bf0dc0008a16e5e56c66a019 (diff) | |
download | cpython-397b96f6d7a89f778ebc0591e32216a8183fe667.zip cpython-397b96f6d7a89f778ebc0591e32216a8183fe667.tar.gz cpython-397b96f6d7a89f778ebc0591e32216a8183fe667.tar.bz2 |
bpo-38870: Implement a precedence algorithm in ast.unparse (GH-17377)
Implement a simple precedence algorithm for ast.unparse in order to avoid redundant
parenthesis for nested structures in the final output.
Diffstat (limited to 'Lib/test/test_unparse.py')
-rw-r--r-- | Lib/test/test_unparse.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py index e8b0d4b..f7fcb2b 100644 --- a/Lib/test/test_unparse.py +++ b/Lib/test/test_unparse.py @@ -125,6 +125,13 @@ class ASTTestCase(unittest.TestCase): def check_invalid(self, node, raises=ValueError): self.assertRaises(raises, ast.unparse, node) + def check_src_roundtrip(self, code1, code2=None, strip=True): + code2 = code2 or code1 + code1 = ast.unparse(ast.parse(code1)) + if strip: + code1 = code1.strip() + self.assertEqual(code2, code1) + class UnparseTestCase(ASTTestCase): # Tests for specific bugs found in earlier versions of unparse @@ -281,6 +288,40 @@ class UnparseTestCase(ASTTestCase): def test_invalid_yield_from(self): self.check_invalid(ast.YieldFrom(value=None)) +class CosmeticTestCase(ASTTestCase): + """Test if there are cosmetic issues caused by unnecesary additions""" + + def test_simple_expressions_parens(self): + self.check_src_roundtrip("(a := b)") + self.check_src_roundtrip("await x") + self.check_src_roundtrip("x if x else y") + self.check_src_roundtrip("lambda x: x") + self.check_src_roundtrip("1 + 1") + self.check_src_roundtrip("1 + 2 / 3") + self.check_src_roundtrip("(1 + 2) / 3") + self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2)") + self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2) ** 2") + self.check_src_roundtrip("~ x") + self.check_src_roundtrip("x and y") + self.check_src_roundtrip("x and y and z") + self.check_src_roundtrip("x and (y and x)") + self.check_src_roundtrip("(x and y) and z") + self.check_src_roundtrip("(x ** y) ** z ** q") + self.check_src_roundtrip("x >> y") + self.check_src_roundtrip("x << y") + self.check_src_roundtrip("x >> y and x >> z") + self.check_src_roundtrip("x + y - z * q ^ t ** k") + self.check_src_roundtrip("P * V if P and V else n * R * T") + self.check_src_roundtrip("lambda P, V, n: P * V == n * R * T") + self.check_src_roundtrip("flag & (other | foo)") + self.check_src_roundtrip("not x == y") + self.check_src_roundtrip("x == (not y)") + self.check_src_roundtrip("yield x") + self.check_src_roundtrip("yield from x") + self.check_src_roundtrip("call((yield x))") + self.check_src_roundtrip("return x + (yield x)") + + class DirectoryTestCase(ASTTestCase): """Test roundtrip behaviour on all files in Lib and Lib/test.""" |