Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | import test.support |
| 3 | |
| 4 | import io |
| 5 | import ast |
| 6 | import _ast |
| 7 | import unparse |
| 8 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 9 | for_else = """\ |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 10 | def f(): |
| 11 | for x in range(10): |
| 12 | break |
| 13 | else: |
| 14 | y = 2 |
| 15 | z = 3 |
| 16 | """ |
| 17 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 18 | while_else = """\ |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 19 | def g(): |
| 20 | while True: |
| 21 | break |
| 22 | else: |
| 23 | y = 2 |
| 24 | z = 3 |
| 25 | """ |
| 26 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 27 | relative_import = """\ |
| 28 | from . import fred |
| 29 | from .. import barney |
| 30 | from .australia import shrimp as prawns |
| 31 | """ |
| 32 | |
| 33 | nonlocal_ex = """\ |
| 34 | def f(): |
| 35 | x = 1 |
| 36 | def g(): |
| 37 | nonlocal x |
| 38 | x = 2 |
| 39 | y = 7 |
| 40 | def h(): |
| 41 | nonlocal x, y |
| 42 | """ |
| 43 | |
| 44 | # also acts as test for 'except ... as ...' |
| 45 | raise_from = """\ |
| 46 | try: |
| 47 | 1 / 0 |
| 48 | except ZeroDivisionError as e: |
| 49 | raise ArithmeticError from e |
| 50 | """ |
| 51 | |
| 52 | class_decorator = """\ |
| 53 | @f1(arg) |
| 54 | @f2 |
| 55 | class Foo: pass |
| 56 | """ |
| 57 | |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 58 | class UnparseTestCase(unittest.TestCase): |
| 59 | # Tests for specific bugs found in earlier versions of unparse |
| 60 | |
| 61 | def check_roundtrip(self, code1, filename="internal"): |
| 62 | ast1 = compile(code1, filename, "exec", _ast.PyCF_ONLY_AST) |
| 63 | unparse_buffer = io.StringIO() |
| 64 | unparse.Unparser(ast1, unparse_buffer) |
| 65 | code2 = unparse_buffer.getvalue() |
| 66 | ast2 = compile(code2, filename, "exec", _ast.PyCF_ONLY_AST) |
| 67 | self.assertEqual(ast.dump(ast1), ast.dump(ast2)) |
| 68 | |
| 69 | def test_del_statement(self): |
| 70 | self.check_roundtrip("del x, y, z") |
| 71 | |
| 72 | def test_shifts(self): |
| 73 | self.check_roundtrip("45 << 2") |
| 74 | self.check_roundtrip("13 >> 7") |
| 75 | |
| 76 | def test_for_else(self): |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 77 | self.check_roundtrip(for_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 78 | |
| 79 | def test_while_else(self): |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 80 | self.check_roundtrip(while_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 81 | |
| 82 | def test_unary_parens(self): |
| 83 | self.check_roundtrip("(-1)**7") |
| 84 | self.check_roundtrip("not True or False") |
| 85 | self.check_roundtrip("True or not False") |
| 86 | |
Mark Dickinson | f5451e5 | 2010-06-28 20:09:18 +0000 | [diff] [blame] | 87 | def test_chained_comparisons(self): |
| 88 | self.check_roundtrip("1 < 4 <= 5") |
| 89 | self.check_roundtrip("a is b is c is not d") |
| 90 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 91 | def test_function_arguments(self): |
| 92 | self.check_roundtrip("def f(): pass") |
| 93 | self.check_roundtrip("def f(a): pass") |
| 94 | self.check_roundtrip("def f(b = 2): pass") |
| 95 | self.check_roundtrip("def f(a, b): pass") |
| 96 | self.check_roundtrip("def f(a, b = 2): pass") |
| 97 | self.check_roundtrip("def f(a = 5, b = 2): pass") |
| 98 | self.check_roundtrip("def f(*, a = 1, b = 2): pass") |
| 99 | self.check_roundtrip("def f(*, a = 1, b): pass") |
| 100 | self.check_roundtrip("def f(*, a, b = 2): pass") |
| 101 | self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass") |
| 102 | self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass") |
| 103 | self.check_roundtrip("def f(*args, **kwargs): pass") |
| 104 | |
| 105 | def test_relative_import(self): |
| 106 | self.check_roundtrip(relative_import) |
| 107 | |
| 108 | def test_nonlocal(self): |
| 109 | self.check_roundtrip(nonlocal_ex) |
| 110 | |
| 111 | def test_raise_from(self): |
| 112 | self.check_roundtrip(raise_from) |
| 113 | |
| 114 | def test_bytes(self): |
| 115 | self.check_roundtrip("b'123'") |
| 116 | |
| 117 | def test_annotations(self): |
| 118 | self.check_roundtrip("def f(a : int): pass") |
| 119 | self.check_roundtrip("def f(a: int = 5): pass") |
| 120 | self.check_roundtrip("def f(*args: [int]): pass") |
| 121 | self.check_roundtrip("def f(**kwargs: dict): pass") |
| 122 | self.check_roundtrip("def f() -> None: pass") |
| 123 | |
| 124 | def test_set_literal(self): |
| 125 | self.check_roundtrip("{'a', 'b', 'c'}") |
| 126 | |
| 127 | def test_set_comprehension(self): |
| 128 | self.check_roundtrip("{x for x in range(5)}") |
| 129 | |
| 130 | def test_dict_comprehension(self): |
| 131 | self.check_roundtrip("{x: x*x for x in range(10)}") |
| 132 | |
| 133 | def test_class_decorators(self): |
| 134 | self.check_roundtrip(class_decorator) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 135 | |
| 136 | def test_main(): |
| 137 | test.support.run_unittest(UnparseTestCase) |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | test_main() |