blob: 3a795e9974428e1e6b60cc105e0c286210e01c90 [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")
126 self.check_roundtrip("not True or False")
127 self.check_roundtrip("True or not False")
128
Mark Dickinson3eb02902010-06-29 08:52:36 +0000129 def test_integer_parens(self):
130 self.check_roundtrip("3 .__abs__()")
131
Mark Dickinson8042e282010-06-29 10:01:48 +0000132 def test_huge_float(self):
133 self.check_roundtrip("1e1000")
134 self.check_roundtrip("-1e1000")
135
136 def test_lambda_parentheses(self):
137 self.check_roundtrip("(lambda: int)()")
138
Mark Dickinsonf5451e52010-06-28 20:09:18 +0000139 def test_chained_comparisons(self):
140 self.check_roundtrip("1 < 4 <= 5")
141 self.check_roundtrip("a is b is c is not d")
142
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000143 def test_function_arguments(self):
144 self.check_roundtrip("def f(): pass")
145 self.check_roundtrip("def f(a): pass")
146 self.check_roundtrip("def f(b = 2): pass")
147 self.check_roundtrip("def f(a, b): pass")
148 self.check_roundtrip("def f(a, b = 2): pass")
149 self.check_roundtrip("def f(a = 5, b = 2): pass")
150 self.check_roundtrip("def f(*, a = 1, b = 2): pass")
151 self.check_roundtrip("def f(*, a = 1, b): pass")
152 self.check_roundtrip("def f(*, a, b = 2): pass")
153 self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
154 self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
155 self.check_roundtrip("def f(*args, **kwargs): pass")
156
157 def test_relative_import(self):
158 self.check_roundtrip(relative_import)
159
160 def test_nonlocal(self):
161 self.check_roundtrip(nonlocal_ex)
162
163 def test_raise_from(self):
164 self.check_roundtrip(raise_from)
165
166 def test_bytes(self):
167 self.check_roundtrip("b'123'")
168
169 def test_annotations(self):
170 self.check_roundtrip("def f(a : int): pass")
171 self.check_roundtrip("def f(a: int = 5): pass")
172 self.check_roundtrip("def f(*args: [int]): pass")
173 self.check_roundtrip("def f(**kwargs: dict): pass")
174 self.check_roundtrip("def f() -> None: pass")
175
176 def test_set_literal(self):
177 self.check_roundtrip("{'a', 'b', 'c'}")
178
179 def test_set_comprehension(self):
180 self.check_roundtrip("{x for x in range(5)}")
181
182 def test_dict_comprehension(self):
183 self.check_roundtrip("{x: x*x for x in range(10)}")
184
185 def test_class_decorators(self):
186 self.check_roundtrip(class_decorator)
Mark Dickinsonae100052010-06-28 19:44:20 +0000187
Mark Dickinson578aa562010-06-29 18:38:59 +0000188 def test_class_definition(self):
189 self.check_roundtrip("class A(metaclass=type, *[], **{}): pass")
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000190
Mark Dickinson8d6d7602010-06-30 08:32:11 +0000191 def test_elifs(self):
192 self.check_roundtrip(elif1)
193 self.check_roundtrip(elif2)
194
Mark Dickinson81ad8cc2010-06-30 08:46:53 +0000195 def test_try_except_finally(self):
196 self.check_roundtrip(try_except_finally)
197
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000198class DirectoryTestCase(ASTTestCase):
199 """Test roundtrip behaviour on all files in Lib and Lib/test."""
200
201 # test directories, relative to the root of the distribution
202 test_directories = 'Lib', os.path.join('Lib', 'test')
203
204 def test_files(self):
205 # get names of files to test
206 dist_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
207
208 names = []
209 for d in self.test_directories:
210 test_dir = os.path.join(dist_dir, d)
211 for n in os.listdir(test_dir):
212 if n.endswith('.py') and not n.startswith('bad'):
213 names.append(os.path.join(test_dir, n))
214
215 for filename in names:
216 if test.support.verbose:
217 print('Testing %s' % filename)
218 source = read_pyfile(filename)
219 self.check_roundtrip(source)
220
221
Mark Dickinsonae100052010-06-28 19:44:20 +0000222def test_main():
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000223 test.support.run_unittest(UnparseTestCase, DirectoryTestCase)
Mark Dickinsonae100052010-06-28 19:44:20 +0000224
225if __name__ == '__main__':
226 test_main()