blob: 2ac1ea670fe79e552d9c0aad6fdbf3e723e1bf7d [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
5import tokenize
Mark Dickinsonae100052010-06-28 19:44:20 +00006import ast
Mark Dickinsonae100052010-06-28 19:44:20 +00007import unparse
8
Mark Dickinsond751c2e2010-06-29 14:08:23 +00009def 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 Dickinsonfa2e4e92010-06-28 21:14:17 +000018for_else = """\
Mark Dickinsonae100052010-06-28 19:44:20 +000019def f():
20 for x in range(10):
21 break
22 else:
23 y = 2
24 z = 3
25"""
26
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000027while_else = """\
Mark Dickinsonae100052010-06-28 19:44:20 +000028def g():
29 while True:
30 break
31 else:
32 y = 2
33 z = 3
34"""
35
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000036relative_import = """\
37from . import fred
38from .. import barney
39from .australia import shrimp as prawns
40"""
41
42nonlocal_ex = """\
43def 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 ...'
54raise_from = """\
55try:
56 1 / 0
57except ZeroDivisionError as e:
58 raise ArithmeticError from e
59"""
60
61class_decorator = """\
62@f1(arg)
63@f2
64class Foo: pass
65"""
66
Mark Dickinson8d6d7602010-06-30 08:32:11 +000067elif1 = """\
68if cond1:
69 suite1
70elif cond2:
71 suite2
72else:
73 suite3
74"""
75
76elif2 = """\
77if cond1:
78 suite1
79elif cond2:
80 suite2
81"""
82
Mark Dickinson81ad8cc2010-06-30 08:46:53 +000083try_except_finally = """\
84try:
85 suite1
86except ex1:
87 suite2
88except ex2:
89 suite3
90else:
91 suite4
92finally:
93 suite5
94"""
Mark Dickinson8d6d7602010-06-30 08:32:11 +000095
Mark Dickinsond751c2e2010-06-29 14:08:23 +000096class ASTTestCase(unittest.TestCase):
97 def assertASTEqual(self, ast1, ast2):
98 self.assertEqual(ast.dump(ast1), ast.dump(ast2))
Mark Dickinsonae100052010-06-28 19:44:20 +000099
100 def check_roundtrip(self, code1, filename="internal"):
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000101 ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST)
Mark Dickinsonae100052010-06-28 19:44:20 +0000102 unparse_buffer = io.StringIO()
103 unparse.Unparser(ast1, unparse_buffer)
104 code2 = unparse_buffer.getvalue()
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000105 ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST)
106 self.assertASTEqual(ast1, ast2)
107
108class UnparseTestCase(ASTTestCase):
109 # Tests for specific bugs found in earlier versions of unparse
Mark Dickinsonae100052010-06-28 19:44:20 +0000110
111 def test_del_statement(self):
112 self.check_roundtrip("del x, y, z")
113
114 def test_shifts(self):
115 self.check_roundtrip("45 << 2")
116 self.check_roundtrip("13 >> 7")
117
118 def test_for_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000119 self.check_roundtrip(for_else)
Mark Dickinsonae100052010-06-28 19:44:20 +0000120
121 def test_while_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000122 self.check_roundtrip(while_else)
Mark Dickinsonae100052010-06-28 19:44:20 +0000123
124 def test_unary_parens(self):
125 self.check_roundtrip("(-1)**7")
Mark Dickinsoncba8c102010-06-30 11:45:53 +0000126 self.check_roundtrip("(-1.)**8")
127 self.check_roundtrip("(-1j)**6")
Mark Dickinsonae100052010-06-28 19:44:20 +0000128 self.check_roundtrip("not True or False")
129 self.check_roundtrip("True or not False")
130
Mark Dickinson3eb02902010-06-29 08:52:36 +0000131 def test_integer_parens(self):
132 self.check_roundtrip("3 .__abs__()")
133
Mark Dickinson8042e282010-06-29 10:01:48 +0000134 def test_huge_float(self):
135 self.check_roundtrip("1e1000")
136 self.check_roundtrip("-1e1000")
Mark Dickinsoncba8c102010-06-30 11:45:53 +0000137 self.check_roundtrip("1e1000j")
138 self.check_roundtrip("-1e1000j")
139
140 def test_min_int(self):
141 self.check_roundtrip(str(-2**31))
142 self.check_roundtrip(str(-2**63))
143
144 def test_imaginary_literals(self):
145 self.check_roundtrip("7j")
146 self.check_roundtrip("-7j")
147 self.check_roundtrip("0j")
148 self.check_roundtrip("-0j")
Mark Dickinson8042e282010-06-29 10:01:48 +0000149
150 def test_lambda_parentheses(self):
151 self.check_roundtrip("(lambda: int)()")
152
Mark Dickinsonf5451e52010-06-28 20:09:18 +0000153 def test_chained_comparisons(self):
154 self.check_roundtrip("1 < 4 <= 5")
155 self.check_roundtrip("a is b is c is not d")
156
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000157 def test_function_arguments(self):
158 self.check_roundtrip("def f(): pass")
159 self.check_roundtrip("def f(a): pass")
160 self.check_roundtrip("def f(b = 2): pass")
161 self.check_roundtrip("def f(a, b): pass")
162 self.check_roundtrip("def f(a, b = 2): pass")
163 self.check_roundtrip("def f(a = 5, b = 2): pass")
164 self.check_roundtrip("def f(*, a = 1, b = 2): pass")
165 self.check_roundtrip("def f(*, a = 1, b): pass")
166 self.check_roundtrip("def f(*, a, b = 2): pass")
167 self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
168 self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
169 self.check_roundtrip("def f(*args, **kwargs): pass")
170
171 def test_relative_import(self):
172 self.check_roundtrip(relative_import)
173
174 def test_nonlocal(self):
175 self.check_roundtrip(nonlocal_ex)
176
177 def test_raise_from(self):
178 self.check_roundtrip(raise_from)
179
180 def test_bytes(self):
181 self.check_roundtrip("b'123'")
182
183 def test_annotations(self):
184 self.check_roundtrip("def f(a : int): pass")
185 self.check_roundtrip("def f(a: int = 5): pass")
186 self.check_roundtrip("def f(*args: [int]): pass")
187 self.check_roundtrip("def f(**kwargs: dict): pass")
188 self.check_roundtrip("def f() -> None: pass")
189
190 def test_set_literal(self):
191 self.check_roundtrip("{'a', 'b', 'c'}")
192
193 def test_set_comprehension(self):
194 self.check_roundtrip("{x for x in range(5)}")
195
196 def test_dict_comprehension(self):
197 self.check_roundtrip("{x: x*x for x in range(10)}")
198
199 def test_class_decorators(self):
200 self.check_roundtrip(class_decorator)
Mark Dickinsonae100052010-06-28 19:44:20 +0000201
Mark Dickinson578aa562010-06-29 18:38:59 +0000202 def test_class_definition(self):
203 self.check_roundtrip("class A(metaclass=type, *[], **{}): pass")
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000204
Mark Dickinson8d6d7602010-06-30 08:32:11 +0000205 def test_elifs(self):
206 self.check_roundtrip(elif1)
207 self.check_roundtrip(elif2)
208
Mark Dickinson81ad8cc2010-06-30 08:46:53 +0000209 def test_try_except_finally(self):
210 self.check_roundtrip(try_except_finally)
211
Mark Dickinson1b2e9442012-05-06 17:27:39 +0100212 def test_starred_assignment(self):
213 self.check_roundtrip("a, *b, c = seq")
214 self.check_roundtrip("a, (*b, c) = seq")
215 self.check_roundtrip("a, *b[0], c = seq")
216 self.check_roundtrip("a, *(b, c) = seq")
217
218
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000219class DirectoryTestCase(ASTTestCase):
220 """Test roundtrip behaviour on all files in Lib and Lib/test."""
221
222 # test directories, relative to the root of the distribution
223 test_directories = 'Lib', os.path.join('Lib', 'test')
224
Mark Dickinsonabe52d72010-06-30 11:14:30 +0000225 def test_files(self):
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000226 # get names of files to test
227 dist_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
228
229 names = []
230 for d in self.test_directories:
231 test_dir = os.path.join(dist_dir, d)
232 for n in os.listdir(test_dir):
233 if n.endswith('.py') and not n.startswith('bad'):
234 names.append(os.path.join(test_dir, n))
235
236 for filename in names:
237 if test.support.verbose:
238 print('Testing %s' % filename)
239 source = read_pyfile(filename)
240 self.check_roundtrip(source)
241
242
Mark Dickinsonae100052010-06-28 19:44:20 +0000243def test_main():
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000244 test.support.run_unittest(UnparseTestCase, DirectoryTestCase)
Mark Dickinsonae100052010-06-28 19:44:20 +0000245
246if __name__ == '__main__':
247 test_main()