blob: be84400c38ff3630f42628527dcf8a7094dff360 [file] [log] [blame]
Mark Dickinsonae100052010-06-28 19:44:20 +00001import unittest
2import test.support
Mark Dickinsonae100052010-06-28 19:44:20 +00003import io
Mark Dickinsond751c2e2010-06-29 14:08:23 +00004import os
Mark Dickinsonbe4fb692012-06-23 09:27:47 +01005import random
Mark Dickinsond751c2e2010-06-29 14:08:23 +00006import tokenize
Mark Dickinsonae100052010-06-28 19:44:20 +00007import unparse
Mark Dickinsonbe4fb692012-06-23 09:27:47 +01008import ast
Mark Dickinsonae100052010-06-28 19:44:20 +00009
Mark Dickinsond751c2e2010-06-29 14:08:23 +000010def read_pyfile(filename):
11 """Read and return the contents of a Python source file (as a
12 string), taking into account the file encoding."""
13 with open(filename, "rb") as pyfile:
14 encoding = tokenize.detect_encoding(pyfile.readline)[0]
15 with open(filename, "r", encoding=encoding) as pyfile:
16 source = pyfile.read()
17 return source
18
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000019for_else = """\
Mark Dickinsonae100052010-06-28 19:44:20 +000020def f():
21 for x in range(10):
22 break
23 else:
24 y = 2
25 z = 3
26"""
27
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000028while_else = """\
Mark Dickinsonae100052010-06-28 19:44:20 +000029def g():
30 while True:
31 break
32 else:
33 y = 2
34 z = 3
35"""
36
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000037relative_import = """\
38from . import fred
39from .. import barney
40from .australia import shrimp as prawns
41"""
42
43nonlocal_ex = """\
44def f():
45 x = 1
46 def g():
47 nonlocal x
48 x = 2
49 y = 7
50 def h():
51 nonlocal x, y
52"""
53
54# also acts as test for 'except ... as ...'
55raise_from = """\
56try:
57 1 / 0
58except ZeroDivisionError as e:
59 raise ArithmeticError from e
60"""
61
62class_decorator = """\
63@f1(arg)
64@f2
65class Foo: pass
66"""
67
Mark Dickinson8d6d7602010-06-30 08:32:11 +000068elif1 = """\
69if cond1:
70 suite1
71elif cond2:
72 suite2
73else:
74 suite3
75"""
76
77elif2 = """\
78if cond1:
79 suite1
80elif cond2:
81 suite2
82"""
83
Mark Dickinson81ad8cc2010-06-30 08:46:53 +000084try_except_finally = """\
85try:
86 suite1
87except ex1:
88 suite2
89except ex2:
90 suite3
91else:
92 suite4
93finally:
94 suite5
95"""
Mark Dickinson8d6d7602010-06-30 08:32:11 +000096
Mark Dickinsonfe8440a2012-05-06 17:35:19 +010097with_simple = """\
98with f():
99 suite1
100"""
101
102with_as = """\
103with f() as x:
104 suite1
105"""
106
107with_two_items = """\
108with f() as x, g() as y:
109 suite1
110"""
111
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000112class ASTTestCase(unittest.TestCase):
113 def assertASTEqual(self, ast1, ast2):
114 self.assertEqual(ast.dump(ast1), ast.dump(ast2))
Mark Dickinsonae100052010-06-28 19:44:20 +0000115
116 def check_roundtrip(self, code1, filename="internal"):
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000117 ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST)
Mark Dickinsonae100052010-06-28 19:44:20 +0000118 unparse_buffer = io.StringIO()
119 unparse.Unparser(ast1, unparse_buffer)
120 code2 = unparse_buffer.getvalue()
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000121 ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST)
122 self.assertASTEqual(ast1, ast2)
123
124class UnparseTestCase(ASTTestCase):
125 # Tests for specific bugs found in earlier versions of unparse
Mark Dickinsonae100052010-06-28 19:44:20 +0000126
127 def test_del_statement(self):
128 self.check_roundtrip("del x, y, z")
129
130 def test_shifts(self):
131 self.check_roundtrip("45 << 2")
132 self.check_roundtrip("13 >> 7")
133
134 def test_for_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000135 self.check_roundtrip(for_else)
Mark Dickinsonae100052010-06-28 19:44:20 +0000136
137 def test_while_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000138 self.check_roundtrip(while_else)
Mark Dickinsonae100052010-06-28 19:44:20 +0000139
140 def test_unary_parens(self):
141 self.check_roundtrip("(-1)**7")
Mark Dickinsoncba8c102010-06-30 11:45:53 +0000142 self.check_roundtrip("(-1.)**8")
143 self.check_roundtrip("(-1j)**6")
Mark Dickinsonae100052010-06-28 19:44:20 +0000144 self.check_roundtrip("not True or False")
145 self.check_roundtrip("True or not False")
146
Mark Dickinson3eb02902010-06-29 08:52:36 +0000147 def test_integer_parens(self):
148 self.check_roundtrip("3 .__abs__()")
149
Mark Dickinson8042e282010-06-29 10:01:48 +0000150 def test_huge_float(self):
151 self.check_roundtrip("1e1000")
152 self.check_roundtrip("-1e1000")
Mark Dickinsoncba8c102010-06-30 11:45:53 +0000153 self.check_roundtrip("1e1000j")
154 self.check_roundtrip("-1e1000j")
155
156 def test_min_int(self):
157 self.check_roundtrip(str(-2**31))
158 self.check_roundtrip(str(-2**63))
159
160 def test_imaginary_literals(self):
161 self.check_roundtrip("7j")
162 self.check_roundtrip("-7j")
163 self.check_roundtrip("0j")
164 self.check_roundtrip("-0j")
Mark Dickinson8042e282010-06-29 10:01:48 +0000165
166 def test_lambda_parentheses(self):
167 self.check_roundtrip("(lambda: int)()")
168
Mark Dickinsonf5451e52010-06-28 20:09:18 +0000169 def test_chained_comparisons(self):
170 self.check_roundtrip("1 < 4 <= 5")
171 self.check_roundtrip("a is b is c is not d")
172
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000173 def test_function_arguments(self):
174 self.check_roundtrip("def f(): pass")
175 self.check_roundtrip("def f(a): pass")
176 self.check_roundtrip("def f(b = 2): pass")
177 self.check_roundtrip("def f(a, b): pass")
178 self.check_roundtrip("def f(a, b = 2): pass")
179 self.check_roundtrip("def f(a = 5, b = 2): pass")
180 self.check_roundtrip("def f(*, a = 1, b = 2): pass")
181 self.check_roundtrip("def f(*, a = 1, b): pass")
182 self.check_roundtrip("def f(*, a, b = 2): pass")
183 self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
184 self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
185 self.check_roundtrip("def f(*args, **kwargs): pass")
186
187 def test_relative_import(self):
188 self.check_roundtrip(relative_import)
189
190 def test_nonlocal(self):
191 self.check_roundtrip(nonlocal_ex)
192
193 def test_raise_from(self):
194 self.check_roundtrip(raise_from)
195
196 def test_bytes(self):
197 self.check_roundtrip("b'123'")
198
199 def test_annotations(self):
200 self.check_roundtrip("def f(a : int): pass")
201 self.check_roundtrip("def f(a: int = 5): pass")
202 self.check_roundtrip("def f(*args: [int]): pass")
203 self.check_roundtrip("def f(**kwargs: dict): pass")
204 self.check_roundtrip("def f() -> None: pass")
205
206 def test_set_literal(self):
207 self.check_roundtrip("{'a', 'b', 'c'}")
208
209 def test_set_comprehension(self):
210 self.check_roundtrip("{x for x in range(5)}")
211
212 def test_dict_comprehension(self):
213 self.check_roundtrip("{x: x*x for x in range(10)}")
214
215 def test_class_decorators(self):
216 self.check_roundtrip(class_decorator)
Mark Dickinsonae100052010-06-28 19:44:20 +0000217
Mark Dickinson578aa562010-06-29 18:38:59 +0000218 def test_class_definition(self):
219 self.check_roundtrip("class A(metaclass=type, *[], **{}): pass")
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000220
Mark Dickinson8d6d7602010-06-30 08:32:11 +0000221 def test_elifs(self):
222 self.check_roundtrip(elif1)
223 self.check_roundtrip(elif2)
224
Mark Dickinson81ad8cc2010-06-30 08:46:53 +0000225 def test_try_except_finally(self):
226 self.check_roundtrip(try_except_finally)
227
Mark Dickinson1b2e9442012-05-06 17:27:39 +0100228 def test_starred_assignment(self):
229 self.check_roundtrip("a, *b, c = seq")
230 self.check_roundtrip("a, (*b, c) = seq")
231 self.check_roundtrip("a, *b[0], c = seq")
232 self.check_roundtrip("a, *(b, c) = seq")
233
Mark Dickinsonfe8440a2012-05-06 17:35:19 +0100234 def test_with_simple(self):
235 self.check_roundtrip(with_simple)
236
237 def test_with_as(self):
238 self.check_roundtrip(with_as)
239
240 def test_with_two_items(self):
241 self.check_roundtrip(with_two_items)
242
Mark Dickinson1b2e9442012-05-06 17:27:39 +0100243
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000244class DirectoryTestCase(ASTTestCase):
245 """Test roundtrip behaviour on all files in Lib and Lib/test."""
246
247 # test directories, relative to the root of the distribution
248 test_directories = 'Lib', os.path.join('Lib', 'test')
249
Mark Dickinsonabe52d72010-06-30 11:14:30 +0000250 def test_files(self):
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000251 # get names of files to test
252 dist_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
253
254 names = []
255 for d in self.test_directories:
256 test_dir = os.path.join(dist_dir, d)
257 for n in os.listdir(test_dir):
258 if n.endswith('.py') and not n.startswith('bad'):
259 names.append(os.path.join(test_dir, n))
260
Mark Dickinsonbe4fb692012-06-23 09:27:47 +0100261 # Test limited subset of files unless the 'cpu' resource is specified.
262 if not test.support.is_resource_enabled("cpu"):
263 names = random.sample(names, 10)
264
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000265 for filename in names:
266 if test.support.verbose:
267 print('Testing %s' % filename)
268 source = read_pyfile(filename)
269 self.check_roundtrip(source)
270
271
Mark Dickinsonae100052010-06-28 19:44:20 +0000272def test_main():
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000273 test.support.run_unittest(UnparseTestCase, DirectoryTestCase)
Mark Dickinsonae100052010-06-28 19:44:20 +0000274
275if __name__ == '__main__':
276 test_main()