diff options
| author | Mark Dickinson <dickinsm@gmail.com> | 2010-06-28 19:31:41 +0000 |
|---|---|---|
| committer | Mark Dickinson <dickinsm@gmail.com> | 2010-06-28 19:31:41 +0000 |
| commit | 623b979553ae03ba5b2d5f60a668fbe288b2112d (patch) | |
| tree | 83dae5c3048678713a0da15537a88419fc84827c /Demo/parser/test_unparse.py | |
| parent | 8c996ef45859108faf4fec8773f7810eb5782282 (diff) | |
| download | cpython-git-623b979553ae03ba5b2d5f60a668fbe288b2112d.tar.gz | |
Fix some shallow bugs in Demo/parser/unparse.py, and add tests:
- insert commas between entries in del statement
- left and right shifts were represented as >> and << (respectively); reverse
- unindent properly after for: else: or while: else:
- add parens around the result of an unary operation
- add parens around negative numbers, to avoid turning (-1)**2 into -1**2.
Diffstat (limited to 'Demo/parser/test_unparse.py')
| -rw-r--r-- | Demo/parser/test_unparse.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/Demo/parser/test_unparse.py b/Demo/parser/test_unparse.py new file mode 100644 index 0000000000..bf68c1ac55 --- /dev/null +++ b/Demo/parser/test_unparse.py @@ -0,0 +1,61 @@ +import unittest +from test import test_support + +import cStringIO +import ast +import _ast +import unparse + +forelse = """\ +def f(): + for x in range(10): + break + else: + y = 2 + z = 3 +""" + +whileelse = """\ +def g(): + while True: + break + else: + y = 2 + z = 3 +""" + +class UnparseTestCase(unittest.TestCase): + # Tests for specific bugs found in earlier versions of unparse + + def check_roundtrip(self, code1, filename="internal"): + ast1 = compile(code1, filename, "exec", _ast.PyCF_ONLY_AST) + unparse_buffer = cStringIO.StringIO() + unparse.Unparser(ast1, unparse_buffer) + code2 = unparse_buffer.getvalue() + ast2 = compile(code2, filename, "exec", _ast.PyCF_ONLY_AST) + self.assertEqual(ast.dump(ast1), ast.dump(ast2)) + + def test_del_statement(self): + self.check_roundtrip("del x, y, z") + + def test_shifts(self): + self.check_roundtrip("45 << 2") + self.check_roundtrip("13 >> 7") + + def test_for_else(self): + self.check_roundtrip(forelse) + + def test_while_else(self): + self.check_roundtrip(forelse) + + def test_unary_parens(self): + self.check_roundtrip("(-1)**7") + self.check_roundtrip("not True or False") + self.check_roundtrip("True or not False") + + +def test_main(): + test_support.run_unittest(UnparseTestCase) + +if __name__ == '__main__': + test_main() |
