Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | import test.support |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 3 | import io |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 4 | import os |
| 5 | import tokenize |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 6 | import ast |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 7 | import unparse |
| 8 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 9 | def read_pyfile(filename): |
| 10 | """Read and return the contents of a Python source file (as a |
| 11 | string), taking into account the file encoding.""" |
| 12 | with open(filename, "rb") as pyfile: |
| 13 | encoding = tokenize.detect_encoding(pyfile.readline)[0] |
| 14 | with open(filename, "r", encoding=encoding) as pyfile: |
| 15 | source = pyfile.read() |
| 16 | return source |
| 17 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 18 | for_else = """\ |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 19 | def f(): |
| 20 | for x in range(10): |
| 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 | while_else = """\ |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 28 | def g(): |
| 29 | while True: |
| 30 | break |
| 31 | else: |
| 32 | y = 2 |
| 33 | z = 3 |
| 34 | """ |
| 35 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 36 | relative_import = """\ |
| 37 | from . import fred |
| 38 | from .. import barney |
| 39 | from .australia import shrimp as prawns |
| 40 | """ |
| 41 | |
| 42 | nonlocal_ex = """\ |
| 43 | def f(): |
| 44 | x = 1 |
| 45 | def g(): |
| 46 | nonlocal x |
| 47 | x = 2 |
| 48 | y = 7 |
| 49 | def h(): |
| 50 | nonlocal x, y |
| 51 | """ |
| 52 | |
| 53 | # also acts as test for 'except ... as ...' |
| 54 | raise_from = """\ |
| 55 | try: |
| 56 | 1 / 0 |
| 57 | except ZeroDivisionError as e: |
| 58 | raise ArithmeticError from e |
| 59 | """ |
| 60 | |
| 61 | class_decorator = """\ |
| 62 | @f1(arg) |
| 63 | @f2 |
| 64 | class Foo: pass |
| 65 | """ |
| 66 | |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 67 | elif1 = """\ |
| 68 | if cond1: |
| 69 | suite1 |
| 70 | elif cond2: |
| 71 | suite2 |
| 72 | else: |
| 73 | suite3 |
| 74 | """ |
| 75 | |
| 76 | elif2 = """\ |
| 77 | if cond1: |
| 78 | suite1 |
| 79 | elif cond2: |
| 80 | suite2 |
| 81 | """ |
| 82 | |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 83 | try_except_finally = """\ |
| 84 | try: |
| 85 | suite1 |
| 86 | except ex1: |
| 87 | suite2 |
| 88 | except ex2: |
| 89 | suite3 |
| 90 | else: |
| 91 | suite4 |
| 92 | finally: |
| 93 | suite5 |
| 94 | """ |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 95 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 96 | with_simple = """\ |
| 97 | with f(): |
| 98 | suite1 |
| 99 | """ |
| 100 | |
| 101 | with_as = """\ |
| 102 | with f() as x: |
| 103 | suite1 |
| 104 | """ |
| 105 | |
| 106 | with_two_items = """\ |
| 107 | with f() as x, g() as y: |
| 108 | suite1 |
| 109 | """ |
| 110 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 111 | class ASTTestCase(unittest.TestCase): |
| 112 | def assertASTEqual(self, ast1, ast2): |
| 113 | self.assertEqual(ast.dump(ast1), ast.dump(ast2)) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 114 | |
| 115 | def check_roundtrip(self, code1, filename="internal"): |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 116 | ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 117 | unparse_buffer = io.StringIO() |
| 118 | unparse.Unparser(ast1, unparse_buffer) |
| 119 | code2 = unparse_buffer.getvalue() |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 120 | ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST) |
| 121 | self.assertASTEqual(ast1, ast2) |
| 122 | |
| 123 | class UnparseTestCase(ASTTestCase): |
| 124 | # Tests for specific bugs found in earlier versions of unparse |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 125 | |
| 126 | def test_del_statement(self): |
| 127 | self.check_roundtrip("del x, y, z") |
| 128 | |
| 129 | def test_shifts(self): |
| 130 | self.check_roundtrip("45 << 2") |
| 131 | self.check_roundtrip("13 >> 7") |
| 132 | |
| 133 | def test_for_else(self): |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 134 | self.check_roundtrip(for_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 135 | |
| 136 | def test_while_else(self): |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 137 | self.check_roundtrip(while_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 138 | |
| 139 | def test_unary_parens(self): |
| 140 | self.check_roundtrip("(-1)**7") |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 141 | self.check_roundtrip("(-1.)**8") |
| 142 | self.check_roundtrip("(-1j)**6") |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 143 | self.check_roundtrip("not True or False") |
| 144 | self.check_roundtrip("True or not False") |
| 145 | |
Mark Dickinson | 3eb0290 | 2010-06-29 08:52:36 +0000 | [diff] [blame] | 146 | def test_integer_parens(self): |
| 147 | self.check_roundtrip("3 .__abs__()") |
| 148 | |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 149 | def test_huge_float(self): |
| 150 | self.check_roundtrip("1e1000") |
| 151 | self.check_roundtrip("-1e1000") |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 152 | self.check_roundtrip("1e1000j") |
| 153 | self.check_roundtrip("-1e1000j") |
| 154 | |
| 155 | def test_min_int(self): |
| 156 | self.check_roundtrip(str(-2**31)) |
| 157 | self.check_roundtrip(str(-2**63)) |
| 158 | |
| 159 | def test_imaginary_literals(self): |
| 160 | self.check_roundtrip("7j") |
| 161 | self.check_roundtrip("-7j") |
| 162 | self.check_roundtrip("0j") |
| 163 | self.check_roundtrip("-0j") |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 164 | |
| 165 | def test_lambda_parentheses(self): |
| 166 | self.check_roundtrip("(lambda: int)()") |
| 167 | |
Mark Dickinson | f5451e5 | 2010-06-28 20:09:18 +0000 | [diff] [blame] | 168 | def test_chained_comparisons(self): |
| 169 | self.check_roundtrip("1 < 4 <= 5") |
| 170 | self.check_roundtrip("a is b is c is not d") |
| 171 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 172 | def test_function_arguments(self): |
| 173 | self.check_roundtrip("def f(): pass") |
| 174 | self.check_roundtrip("def f(a): pass") |
| 175 | self.check_roundtrip("def f(b = 2): pass") |
| 176 | self.check_roundtrip("def f(a, b): pass") |
| 177 | self.check_roundtrip("def f(a, b = 2): pass") |
| 178 | self.check_roundtrip("def f(a = 5, b = 2): pass") |
| 179 | self.check_roundtrip("def f(*, a = 1, b = 2): pass") |
| 180 | self.check_roundtrip("def f(*, a = 1, b): pass") |
| 181 | self.check_roundtrip("def f(*, a, b = 2): pass") |
| 182 | self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass") |
| 183 | self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass") |
| 184 | self.check_roundtrip("def f(*args, **kwargs): pass") |
| 185 | |
| 186 | def test_relative_import(self): |
| 187 | self.check_roundtrip(relative_import) |
| 188 | |
| 189 | def test_nonlocal(self): |
| 190 | self.check_roundtrip(nonlocal_ex) |
| 191 | |
| 192 | def test_raise_from(self): |
| 193 | self.check_roundtrip(raise_from) |
| 194 | |
| 195 | def test_bytes(self): |
| 196 | self.check_roundtrip("b'123'") |
| 197 | |
| 198 | def test_annotations(self): |
| 199 | self.check_roundtrip("def f(a : int): pass") |
| 200 | self.check_roundtrip("def f(a: int = 5): pass") |
| 201 | self.check_roundtrip("def f(*args: [int]): pass") |
| 202 | self.check_roundtrip("def f(**kwargs: dict): pass") |
| 203 | self.check_roundtrip("def f() -> None: pass") |
| 204 | |
| 205 | def test_set_literal(self): |
| 206 | self.check_roundtrip("{'a', 'b', 'c'}") |
| 207 | |
| 208 | def test_set_comprehension(self): |
| 209 | self.check_roundtrip("{x for x in range(5)}") |
| 210 | |
| 211 | def test_dict_comprehension(self): |
| 212 | self.check_roundtrip("{x: x*x for x in range(10)}") |
| 213 | |
| 214 | def test_class_decorators(self): |
| 215 | self.check_roundtrip(class_decorator) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 216 | |
Mark Dickinson | 578aa56 | 2010-06-29 18:38:59 +0000 | [diff] [blame] | 217 | def test_class_definition(self): |
| 218 | self.check_roundtrip("class A(metaclass=type, *[], **{}): pass") |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 219 | |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 220 | def test_elifs(self): |
| 221 | self.check_roundtrip(elif1) |
| 222 | self.check_roundtrip(elif2) |
| 223 | |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 224 | def test_try_except_finally(self): |
| 225 | self.check_roundtrip(try_except_finally) |
| 226 | |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 227 | def test_starred_assignment(self): |
| 228 | self.check_roundtrip("a, *b, c = seq") |
| 229 | self.check_roundtrip("a, (*b, c) = seq") |
| 230 | self.check_roundtrip("a, *b[0], c = seq") |
| 231 | self.check_roundtrip("a, *(b, c) = seq") |
| 232 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 233 | def test_with_simple(self): |
| 234 | self.check_roundtrip(with_simple) |
| 235 | |
| 236 | def test_with_as(self): |
| 237 | self.check_roundtrip(with_as) |
| 238 | |
| 239 | def test_with_two_items(self): |
| 240 | self.check_roundtrip(with_two_items) |
| 241 | |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 242 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 243 | class DirectoryTestCase(ASTTestCase): |
| 244 | """Test roundtrip behaviour on all files in Lib and Lib/test.""" |
| 245 | |
| 246 | # test directories, relative to the root of the distribution |
| 247 | test_directories = 'Lib', os.path.join('Lib', 'test') |
| 248 | |
Mark Dickinson | abe52d7 | 2010-06-30 11:14:30 +0000 | [diff] [blame] | 249 | def test_files(self): |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 250 | # get names of files to test |
| 251 | dist_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir) |
| 252 | |
| 253 | names = [] |
| 254 | for d in self.test_directories: |
| 255 | test_dir = os.path.join(dist_dir, d) |
| 256 | for n in os.listdir(test_dir): |
| 257 | if n.endswith('.py') and not n.startswith('bad'): |
| 258 | names.append(os.path.join(test_dir, n)) |
| 259 | |
| 260 | for filename in names: |
| 261 | if test.support.verbose: |
| 262 | print('Testing %s' % filename) |
| 263 | source = read_pyfile(filename) |
| 264 | self.check_roundtrip(source) |
| 265 | |
| 266 | |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 267 | def test_main(): |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 268 | test.support.run_unittest(UnparseTestCase, DirectoryTestCase) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 269 | |
| 270 | if __name__ == '__main__': |
| 271 | test_main() |