mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Merged revisions 82338,82340-82341 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r82338 | mark.dickinson | 2010-06-28 20:31:41 +0100 (Mon, 28 Jun 2010) | 9 lines
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.
........
r82340 | mark.dickinson | 2010-06-28 20:34:15 +0100 (Mon, 28 Jun 2010) | 1 line
Fix typo in test_unparse.py.
........
r82341 | mark.dickinson | 2010-06-28 20:38:19 +0100 (Mon, 28 Jun 2010) | 1 line
Set svn:eol-style on test_unparse.py.
........
This commit is contained in:
parent
e893af5ab7
commit
ae1000533a
2 changed files with 77 additions and 6 deletions
61
Demo/parser/test_unparse.py
Normal file
61
Demo/parser/test_unparse.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import unittest
|
||||
import test.support
|
||||
|
||||
import io
|
||||
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 = io.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(whileelse)
|
||||
|
||||
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue