Zachary Ware | 2b0a610 | 2014-07-16 14:26:09 -0500 | [diff] [blame] | 1 | """Tests for the unparse.py script in the Tools/parser directory.""" |
| 2 | |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import test.support |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 5 | import pathlib |
Mark Dickinson | be4fb69 | 2012-06-23 09:27:47 +0100 | [diff] [blame] | 6 | import random |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 7 | import tokenize |
Mark Dickinson | be4fb69 | 2012-06-23 09:27:47 +0100 | [diff] [blame] | 8 | import ast |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 9 | |
Zachary Ware | 2b0a610 | 2014-07-16 14:26:09 -0500 | [diff] [blame] | 10 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 11 | def read_pyfile(filename): |
| 12 | """Read and return the contents of a Python source file (as a |
| 13 | string), taking into account the file encoding.""" |
Hakan Çelik | 6a5bf15 | 2020-04-16 13:11:55 +0300 | [diff] [blame] | 14 | with tokenize.open(filename) as stream: |
| 15 | return stream.read() |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 16 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 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 | |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 111 | docstring_prefixes = [ |
| 112 | "", |
Batuhan Taskaya | 25160cd | 2020-05-17 00:53:25 +0300 | [diff] [blame] | 113 | "class foo:\n ", |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 114 | "def foo():\n ", |
| 115 | "async def foo():\n ", |
| 116 | ] |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 117 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 118 | class ASTTestCase(unittest.TestCase): |
| 119 | def assertASTEqual(self, ast1, ast2): |
| 120 | self.assertEqual(ast.dump(ast1), ast.dump(ast2)) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 121 | |
Batuhan Taşkaya | 5b66ec1 | 2020-03-15 22:56:57 +0300 | [diff] [blame] | 122 | def check_ast_roundtrip(self, code1, **kwargs): |
| 123 | ast1 = ast.parse(code1, **kwargs) |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 124 | code2 = ast.unparse(ast1) |
Batuhan Taşkaya | 5b66ec1 | 2020-03-15 22:56:57 +0300 | [diff] [blame] | 125 | ast2 = ast.parse(code2, **kwargs) |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 126 | self.assertASTEqual(ast1, ast2) |
| 127 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 128 | def check_invalid(self, node, raises=ValueError): |
| 129 | self.assertRaises(raises, ast.unparse, node) |
| 130 | |
Batuhan Taskaya | 493bf1c | 2020-05-03 20:11:51 +0300 | [diff] [blame] | 131 | def get_source(self, code1, code2=None): |
Batuhan Taşkaya | 397b96f | 2020-03-01 23:12:17 +0300 | [diff] [blame] | 132 | code2 = code2 or code1 |
| 133 | code1 = ast.unparse(ast.parse(code1)) |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 134 | return code1, code2 |
| 135 | |
Batuhan Taskaya | 493bf1c | 2020-05-03 20:11:51 +0300 | [diff] [blame] | 136 | def check_src_roundtrip(self, code1, code2=None): |
| 137 | code1, code2 = self.get_source(code1, code2) |
Batuhan Taşkaya | 397b96f | 2020-03-01 23:12:17 +0300 | [diff] [blame] | 138 | self.assertEqual(code2, code1) |
| 139 | |
Batuhan Taskaya | 493bf1c | 2020-05-03 20:11:51 +0300 | [diff] [blame] | 140 | def check_src_dont_roundtrip(self, code1, code2=None): |
| 141 | code1, code2 = self.get_source(code1, code2) |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 142 | self.assertNotEqual(code2, code1) |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 143 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 144 | class UnparseTestCase(ASTTestCase): |
| 145 | # Tests for specific bugs found in earlier versions of unparse |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 146 | |
Eric V. Smith | 608adf9 | 2015-09-20 15:09:15 -0400 | [diff] [blame] | 147 | def test_fstrings(self): |
| 148 | # See issue 25180 |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 149 | self.check_ast_roundtrip(r"""f'{f"{0}"*3}'""") |
| 150 | self.check_ast_roundtrip(r"""f'{f"{y}"*3}'""") |
Eric V. Smith | 608adf9 | 2015-09-20 15:09:15 -0400 | [diff] [blame] | 151 | |
Chih-Hsuan Yen | aaf47ca | 2019-05-27 01:08:20 +0800 | [diff] [blame] | 152 | def test_strings(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 153 | self.check_ast_roundtrip("u'foo'") |
| 154 | self.check_ast_roundtrip("r'foo'") |
| 155 | self.check_ast_roundtrip("b'foo'") |
Chih-Hsuan Yen | aaf47ca | 2019-05-27 01:08:20 +0800 | [diff] [blame] | 156 | |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 157 | def test_del_statement(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 158 | self.check_ast_roundtrip("del x, y, z") |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 159 | |
| 160 | def test_shifts(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 161 | self.check_ast_roundtrip("45 << 2") |
| 162 | self.check_ast_roundtrip("13 >> 7") |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 163 | |
| 164 | def test_for_else(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 165 | self.check_ast_roundtrip(for_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 166 | |
| 167 | def test_while_else(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 168 | self.check_ast_roundtrip(while_else) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 169 | |
| 170 | def test_unary_parens(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 171 | self.check_ast_roundtrip("(-1)**7") |
| 172 | self.check_ast_roundtrip("(-1.)**8") |
| 173 | self.check_ast_roundtrip("(-1j)**6") |
| 174 | self.check_ast_roundtrip("not True or False") |
| 175 | self.check_ast_roundtrip("True or not False") |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 176 | |
Mark Dickinson | 3eb0290 | 2010-06-29 08:52:36 +0000 | [diff] [blame] | 177 | def test_integer_parens(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 178 | self.check_ast_roundtrip("3 .__abs__()") |
Mark Dickinson | 3eb0290 | 2010-06-29 08:52:36 +0000 | [diff] [blame] | 179 | |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 180 | def test_huge_float(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 181 | self.check_ast_roundtrip("1e1000") |
| 182 | self.check_ast_roundtrip("-1e1000") |
| 183 | self.check_ast_roundtrip("1e1000j") |
| 184 | self.check_ast_roundtrip("-1e1000j") |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 185 | |
| 186 | def test_min_int(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 187 | self.check_ast_roundtrip(str(-(2 ** 31))) |
| 188 | self.check_ast_roundtrip(str(-(2 ** 63))) |
Mark Dickinson | cba8c10 | 2010-06-30 11:45:53 +0000 | [diff] [blame] | 189 | |
| 190 | def test_imaginary_literals(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 191 | self.check_ast_roundtrip("7j") |
| 192 | self.check_ast_roundtrip("-7j") |
| 193 | self.check_ast_roundtrip("0j") |
| 194 | self.check_ast_roundtrip("-0j") |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 195 | |
| 196 | def test_lambda_parentheses(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 197 | self.check_ast_roundtrip("(lambda: int)()") |
Mark Dickinson | 8042e28 | 2010-06-29 10:01:48 +0000 | [diff] [blame] | 198 | |
Mark Dickinson | f5451e5 | 2010-06-28 20:09:18 +0000 | [diff] [blame] | 199 | def test_chained_comparisons(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 200 | self.check_ast_roundtrip("1 < 4 <= 5") |
| 201 | self.check_ast_roundtrip("a is b is c is not d") |
Mark Dickinson | f5451e5 | 2010-06-28 20:09:18 +0000 | [diff] [blame] | 202 | |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 203 | def test_function_arguments(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 204 | self.check_ast_roundtrip("def f(): pass") |
| 205 | self.check_ast_roundtrip("def f(a): pass") |
| 206 | self.check_ast_roundtrip("def f(b = 2): pass") |
| 207 | self.check_ast_roundtrip("def f(a, b): pass") |
| 208 | self.check_ast_roundtrip("def f(a, b = 2): pass") |
| 209 | self.check_ast_roundtrip("def f(a = 5, b = 2): pass") |
| 210 | self.check_ast_roundtrip("def f(*, a = 1, b = 2): pass") |
| 211 | self.check_ast_roundtrip("def f(*, a = 1, b): pass") |
| 212 | self.check_ast_roundtrip("def f(*, a, b = 2): pass") |
| 213 | self.check_ast_roundtrip("def f(a, b = None, *, c, **kwds): pass") |
| 214 | self.check_ast_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass") |
| 215 | self.check_ast_roundtrip("def f(*args, **kwargs): pass") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 216 | |
| 217 | def test_relative_import(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 218 | self.check_ast_roundtrip(relative_import) |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 219 | |
| 220 | def test_nonlocal(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 221 | self.check_ast_roundtrip(nonlocal_ex) |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 222 | |
| 223 | def test_raise_from(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 224 | self.check_ast_roundtrip(raise_from) |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 225 | |
| 226 | def test_bytes(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 227 | self.check_ast_roundtrip("b'123'") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 228 | |
| 229 | def test_annotations(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 230 | self.check_ast_roundtrip("def f(a : int): pass") |
| 231 | self.check_ast_roundtrip("def f(a: int = 5): pass") |
| 232 | self.check_ast_roundtrip("def f(*args: [int]): pass") |
| 233 | self.check_ast_roundtrip("def f(**kwargs: dict): pass") |
| 234 | self.check_ast_roundtrip("def f() -> None: pass") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 235 | |
| 236 | def test_set_literal(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 237 | self.check_ast_roundtrip("{'a', 'b', 'c'}") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 238 | |
| 239 | def test_set_comprehension(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 240 | self.check_ast_roundtrip("{x for x in range(5)}") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 241 | |
| 242 | def test_dict_comprehension(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 243 | self.check_ast_roundtrip("{x: x*x for x in range(10)}") |
Mark Dickinson | fa2e4e9 | 2010-06-28 21:14:17 +0000 | [diff] [blame] | 244 | |
| 245 | def test_class_decorators(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 246 | self.check_ast_roundtrip(class_decorator) |
Mark Dickinson | ae10005 | 2010-06-28 19:44:20 +0000 | [diff] [blame] | 247 | |
Mark Dickinson | 578aa56 | 2010-06-29 18:38:59 +0000 | [diff] [blame] | 248 | def test_class_definition(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 249 | self.check_ast_roundtrip("class A(metaclass=type, *[], **{}): pass") |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 250 | |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 251 | def test_elifs(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 252 | self.check_ast_roundtrip(elif1) |
| 253 | self.check_ast_roundtrip(elif2) |
Mark Dickinson | 8d6d760 | 2010-06-30 08:32:11 +0000 | [diff] [blame] | 254 | |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 255 | def test_try_except_finally(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 256 | self.check_ast_roundtrip(try_except_finally) |
Mark Dickinson | 81ad8cc | 2010-06-30 08:46:53 +0000 | [diff] [blame] | 257 | |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 258 | def test_starred_assignment(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 259 | self.check_ast_roundtrip("a, *b, c = seq") |
| 260 | self.check_ast_roundtrip("a, (*b, c) = seq") |
| 261 | self.check_ast_roundtrip("a, *b[0], c = seq") |
| 262 | self.check_ast_roundtrip("a, *(b, c) = seq") |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 263 | |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 264 | def test_with_simple(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 265 | self.check_ast_roundtrip(with_simple) |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 266 | |
| 267 | def test_with_as(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 268 | self.check_ast_roundtrip(with_as) |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 269 | |
| 270 | def test_with_two_items(self): |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 271 | self.check_ast_roundtrip(with_two_items) |
Mark Dickinson | fe8440a | 2012-05-06 17:35:19 +0100 | [diff] [blame] | 272 | |
Berker Peksag | d66dd5c | 2016-03-06 16:50:15 +0200 | [diff] [blame] | 273 | def test_dict_unpacking_in_dict(self): |
| 274 | # See issue 26489 |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 275 | self.check_ast_roundtrip(r"""{**{'y': 2}, 'x': 1}""") |
| 276 | self.check_ast_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""") |
Berker Peksag | d66dd5c | 2016-03-06 16:50:15 +0200 | [diff] [blame] | 277 | |
Batuhan Taşkaya | e7cab7f | 2020-03-09 23:27:03 +0300 | [diff] [blame] | 278 | def test_ext_slices(self): |
| 279 | self.check_ast_roundtrip("a[i]") |
| 280 | self.check_ast_roundtrip("a[i,]") |
| 281 | self.check_ast_roundtrip("a[i, j]") |
| 282 | self.check_ast_roundtrip("a[()]") |
| 283 | self.check_ast_roundtrip("a[i:j]") |
| 284 | self.check_ast_roundtrip("a[:j]") |
| 285 | self.check_ast_roundtrip("a[i:]") |
| 286 | self.check_ast_roundtrip("a[i:j:k]") |
| 287 | self.check_ast_roundtrip("a[:j:k]") |
| 288 | self.check_ast_roundtrip("a[i::k]") |
| 289 | self.check_ast_roundtrip("a[i:j,]") |
| 290 | self.check_ast_roundtrip("a[i:j, k]") |
| 291 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 292 | def test_invalid_raise(self): |
| 293 | self.check_invalid(ast.Raise(exc=None, cause=ast.Name(id="X"))) |
| 294 | |
| 295 | def test_invalid_fstring_constant(self): |
| 296 | self.check_invalid(ast.JoinedStr(values=[ast.Constant(value=100)])) |
| 297 | |
| 298 | def test_invalid_fstring_conversion(self): |
| 299 | self.check_invalid( |
| 300 | ast.FormattedValue( |
| 301 | value=ast.Constant(value="a", kind=None), |
| 302 | conversion=ord("Y"), # random character |
| 303 | format_spec=None, |
| 304 | ) |
| 305 | ) |
| 306 | |
| 307 | def test_invalid_set(self): |
| 308 | self.check_invalid(ast.Set(elts=[])) |
| 309 | |
Batuhan Taşkaya | 7b35bef | 2020-01-02 21:20:04 +0300 | [diff] [blame] | 310 | def test_invalid_yield_from(self): |
| 311 | self.check_invalid(ast.YieldFrom(value=None)) |
Mark Dickinson | 1b2e944 | 2012-05-06 17:27:39 +0100 | [diff] [blame] | 312 | |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 313 | def test_docstrings(self): |
| 314 | docstrings = ( |
| 315 | 'this ends with double quote"', |
| 316 | 'this includes a """triple quote"""' |
| 317 | ) |
| 318 | for docstring in docstrings: |
| 319 | # check as Module docstrings for easy testing |
| 320 | self.check_ast_roundtrip(f"'{docstring}'") |
| 321 | |
Batuhan Taşkaya | e7cab7f | 2020-03-09 23:27:03 +0300 | [diff] [blame] | 322 | def test_constant_tuples(self): |
| 323 | self.check_src_roundtrip(ast.Constant(value=(1,), kind=None), "(1,)") |
| 324 | self.check_src_roundtrip( |
| 325 | ast.Constant(value=(1, 2, 3), kind=None), "(1, 2, 3)" |
| 326 | ) |
| 327 | |
Batuhan Taşkaya | 5b66ec1 | 2020-03-15 22:56:57 +0300 | [diff] [blame] | 328 | def test_function_type(self): |
| 329 | for function_type in ( |
| 330 | "() -> int", |
| 331 | "(int, int) -> int", |
| 332 | "(Callable[complex], More[Complex(call.to_typevar())]) -> None" |
| 333 | ): |
| 334 | self.check_ast_roundtrip(function_type, mode="func_type") |
| 335 | |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 336 | |
Batuhan Taşkaya | 397b96f | 2020-03-01 23:12:17 +0300 | [diff] [blame] | 337 | class CosmeticTestCase(ASTTestCase): |
| 338 | """Test if there are cosmetic issues caused by unnecesary additions""" |
| 339 | |
| 340 | def test_simple_expressions_parens(self): |
| 341 | self.check_src_roundtrip("(a := b)") |
| 342 | self.check_src_roundtrip("await x") |
| 343 | self.check_src_roundtrip("x if x else y") |
| 344 | self.check_src_roundtrip("lambda x: x") |
| 345 | self.check_src_roundtrip("1 + 1") |
| 346 | self.check_src_roundtrip("1 + 2 / 3") |
| 347 | self.check_src_roundtrip("(1 + 2) / 3") |
| 348 | self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2)") |
| 349 | self.check_src_roundtrip("(1 + 2) * 3 + 4 * (5 + 2) ** 2") |
Batuhan Taskaya | ce4a753 | 2020-05-17 00:46:11 +0300 | [diff] [blame] | 350 | self.check_src_roundtrip("~x") |
Batuhan Taşkaya | 397b96f | 2020-03-01 23:12:17 +0300 | [diff] [blame] | 351 | self.check_src_roundtrip("x and y") |
| 352 | self.check_src_roundtrip("x and y and z") |
| 353 | self.check_src_roundtrip("x and (y and x)") |
| 354 | self.check_src_roundtrip("(x and y) and z") |
| 355 | self.check_src_roundtrip("(x ** y) ** z ** q") |
| 356 | self.check_src_roundtrip("x >> y") |
| 357 | self.check_src_roundtrip("x << y") |
| 358 | self.check_src_roundtrip("x >> y and x >> z") |
| 359 | self.check_src_roundtrip("x + y - z * q ^ t ** k") |
| 360 | self.check_src_roundtrip("P * V if P and V else n * R * T") |
| 361 | self.check_src_roundtrip("lambda P, V, n: P * V == n * R * T") |
| 362 | self.check_src_roundtrip("flag & (other | foo)") |
| 363 | self.check_src_roundtrip("not x == y") |
| 364 | self.check_src_roundtrip("x == (not y)") |
| 365 | self.check_src_roundtrip("yield x") |
| 366 | self.check_src_roundtrip("yield from x") |
| 367 | self.check_src_roundtrip("call((yield x))") |
| 368 | self.check_src_roundtrip("return x + (yield x)") |
| 369 | |
Batuhan Taskaya | 25160cd | 2020-05-17 00:53:25 +0300 | [diff] [blame] | 370 | |
| 371 | def test_class_bases_and_keywords(self): |
| 372 | self.check_src_roundtrip("class X:\n pass") |
| 373 | self.check_src_roundtrip("class X(A):\n pass") |
| 374 | self.check_src_roundtrip("class X(A, B, C, D):\n pass") |
| 375 | self.check_src_roundtrip("class X(x=y):\n pass") |
| 376 | self.check_src_roundtrip("class X(metaclass=z):\n pass") |
| 377 | self.check_src_roundtrip("class X(x=y, z=d):\n pass") |
| 378 | self.check_src_roundtrip("class X(A, x=y):\n pass") |
| 379 | self.check_src_roundtrip("class X(A, **kw):\n pass") |
| 380 | self.check_src_roundtrip("class X(*args):\n pass") |
| 381 | self.check_src_roundtrip("class X(*args, **kwargs):\n pass") |
| 382 | |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 383 | def test_docstrings(self): |
| 384 | docstrings = ( |
| 385 | '"""simple doc string"""', |
| 386 | '''"""A more complex one |
| 387 | with some newlines"""''', |
| 388 | '''"""Foo bar baz |
| 389 | |
| 390 | empty newline"""''', |
| 391 | '"""With some \t"""', |
| 392 | '"""Foo "bar" baz """', |
| 393 | ) |
| 394 | |
| 395 | for prefix in docstring_prefixes: |
| 396 | for docstring in docstrings: |
| 397 | self.check_src_roundtrip(f"{prefix}{docstring}") |
| 398 | |
| 399 | def test_docstrings_negative_cases(self): |
| 400 | # Test some cases that involve strings in the children of the |
| 401 | # first node but aren't docstrings to make sure we don't have |
| 402 | # False positives. |
| 403 | docstrings_negative = ( |
| 404 | 'a = """false"""', |
| 405 | '"""false""" + """unless its optimized"""', |
| 406 | '1 + 1\n"""false"""', |
| 407 | 'f"""no, top level but f-fstring"""' |
| 408 | ) |
| 409 | for prefix in docstring_prefixes: |
| 410 | for negative in docstrings_negative: |
| 411 | # this cases should be result with single quote |
| 412 | # rather then triple quoted docstring |
| 413 | src = f"{prefix}{negative}" |
| 414 | self.check_ast_roundtrip(src) |
| 415 | self.check_src_dont_roundtrip(src) |
Batuhan Taşkaya | 397b96f | 2020-03-01 23:12:17 +0300 | [diff] [blame] | 416 | |
Batuhan Taskaya | ce4a753 | 2020-05-17 00:46:11 +0300 | [diff] [blame] | 417 | def test_unary_op_factor(self): |
| 418 | for prefix in ("+", "-", "~"): |
| 419 | self.check_src_roundtrip(f"{prefix}1") |
| 420 | for prefix in ("not",): |
| 421 | self.check_src_roundtrip(f"{prefix} 1") |
| 422 | |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 423 | class DirectoryTestCase(ASTTestCase): |
| 424 | """Test roundtrip behaviour on all files in Lib and Lib/test.""" |
| 425 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 426 | lib_dir = pathlib.Path(__file__).parent / ".." |
| 427 | test_directories = (lib_dir, lib_dir / "test") |
| 428 | skip_files = {"test_fstring.py"} |
Pablo Galindo | 23a226b | 2019-12-29 19:20:55 +0000 | [diff] [blame] | 429 | run_always_files = {"test_grammar.py", "test_syntax.py", "test_compile.py", |
| 430 | "test_ast.py", "test_asdl_parser.py"} |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 431 | |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 432 | _files_to_test = None |
| 433 | |
| 434 | @classmethod |
| 435 | def files_to_test(cls): |
| 436 | |
| 437 | if cls._files_to_test is not None: |
| 438 | return cls._files_to_test |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 439 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 440 | items = [ |
| 441 | item.resolve() |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 442 | for directory in cls.test_directories |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 443 | for item in directory.glob("*.py") |
| 444 | if not item.name.startswith("bad") |
| 445 | ] |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 446 | |
Mark Dickinson | be4fb69 | 2012-06-23 09:27:47 +0100 | [diff] [blame] | 447 | # Test limited subset of files unless the 'cpu' resource is specified. |
| 448 | if not test.support.is_resource_enabled("cpu"): |
Pablo Galindo | be287c3 | 2019-12-29 20:18:36 +0000 | [diff] [blame] | 449 | |
| 450 | tests_to_run_always = {item for item in items if |
| 451 | item.name in cls.run_always_files} |
| 452 | |
Pablo Galindo | 23a226b | 2019-12-29 19:20:55 +0000 | [diff] [blame] | 453 | items = set(random.sample(items, 10)) |
| 454 | |
Pablo Galindo | be287c3 | 2019-12-29 20:18:36 +0000 | [diff] [blame] | 455 | # Make sure that at least tests that heavily use grammar features are |
| 456 | # always considered in order to reduce the chance of missing something. |
| 457 | items = list(items | tests_to_run_always) |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 458 | |
| 459 | # bpo-31174: Store the names sample to always test the same files. |
| 460 | # It prevents false alarms when hunting reference leaks. |
| 461 | cls._files_to_test = items |
| 462 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 463 | return items |
Victor Stinner | 8e482be | 2017-10-24 03:33:36 -0700 | [diff] [blame] | 464 | |
| 465 | def test_files(self): |
Pablo Galindo | ac22911 | 2019-12-09 17:57:50 +0000 | [diff] [blame] | 466 | for item in self.files_to_test(): |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 467 | if test.support.verbose: |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 468 | print(f"Testing {item.absolute()}") |
Eric V. Smith | 06cf601 | 2016-09-03 12:33:38 -0400 | [diff] [blame] | 469 | |
Eric V. Smith | 451d0e3 | 2016-09-09 21:56:20 -0400 | [diff] [blame] | 470 | # Some f-strings are not correctly round-tripped by |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 471 | # Tools/parser/unparse.py. See issue 28002 for details. |
| 472 | # We need to skip files that contain such f-strings. |
| 473 | if item.name in self.skip_files: |
Eric V. Smith | 06cf601 | 2016-09-03 12:33:38 -0400 | [diff] [blame] | 474 | if test.support.verbose: |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 475 | print(f"Skipping {item.absolute()}: see issue 28002") |
Eric V. Smith | 06cf601 | 2016-09-03 12:33:38 -0400 | [diff] [blame] | 476 | continue |
| 477 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 478 | with self.subTest(filename=item): |
| 479 | source = read_pyfile(item) |
Batuhan Taşkaya | 89aa469 | 2020-03-02 21:59:01 +0300 | [diff] [blame] | 480 | self.check_ast_roundtrip(source) |
Mark Dickinson | d751c2e | 2010-06-29 14:08:23 +0000 | [diff] [blame] | 481 | |
| 482 | |
Pablo Galindo | 27fc3b6 | 2019-11-24 23:02:40 +0000 | [diff] [blame] | 483 | if __name__ == "__main__": |
Zachary Ware | 2b0a610 | 2014-07-16 14:26:09 -0500 | [diff] [blame] | 484 | unittest.main() |