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