blob: 7d85f16cfda079ef1fa89697dcc9e9c92add59df [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
83
84
Mark Dickinsond751c2e2010-06-29 14:08:23 +000085class ASTTestCase(unittest.TestCase):
86 def assertASTEqual(self, ast1, ast2):
87 self.assertEqual(ast.dump(ast1), ast.dump(ast2))
Mark Dickinsonae100052010-06-28 19:44:20 +000088
89 def check_roundtrip(self, code1, filename="internal"):
Mark Dickinsond751c2e2010-06-29 14:08:23 +000090 ast1 = compile(code1, filename, "exec", ast.PyCF_ONLY_AST)
Mark Dickinsonae100052010-06-28 19:44:20 +000091 unparse_buffer = io.StringIO()
92 unparse.Unparser(ast1, unparse_buffer)
93 code2 = unparse_buffer.getvalue()
Mark Dickinsond751c2e2010-06-29 14:08:23 +000094 ast2 = compile(code2, filename, "exec", ast.PyCF_ONLY_AST)
95 self.assertASTEqual(ast1, ast2)
96
97class UnparseTestCase(ASTTestCase):
98 # Tests for specific bugs found in earlier versions of unparse
Mark Dickinsonae100052010-06-28 19:44:20 +000099
100 def test_del_statement(self):
101 self.check_roundtrip("del x, y, z")
102
103 def test_shifts(self):
104 self.check_roundtrip("45 << 2")
105 self.check_roundtrip("13 >> 7")
106
107 def test_for_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000108 self.check_roundtrip(for_else)
Mark Dickinsonae100052010-06-28 19:44:20 +0000109
110 def test_while_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000111 self.check_roundtrip(while_else)
Mark Dickinsonae100052010-06-28 19:44:20 +0000112
113 def test_unary_parens(self):
114 self.check_roundtrip("(-1)**7")
115 self.check_roundtrip("not True or False")
116 self.check_roundtrip("True or not False")
117
Mark Dickinson3eb02902010-06-29 08:52:36 +0000118 def test_integer_parens(self):
119 self.check_roundtrip("3 .__abs__()")
120
Mark Dickinson8042e282010-06-29 10:01:48 +0000121 def test_huge_float(self):
122 self.check_roundtrip("1e1000")
123 self.check_roundtrip("-1e1000")
124
125 def test_lambda_parentheses(self):
126 self.check_roundtrip("(lambda: int)()")
127
Mark Dickinsonf5451e52010-06-28 20:09:18 +0000128 def test_chained_comparisons(self):
129 self.check_roundtrip("1 < 4 <= 5")
130 self.check_roundtrip("a is b is c is not d")
131
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000132 def test_function_arguments(self):
133 self.check_roundtrip("def f(): pass")
134 self.check_roundtrip("def f(a): pass")
135 self.check_roundtrip("def f(b = 2): pass")
136 self.check_roundtrip("def f(a, b): pass")
137 self.check_roundtrip("def f(a, b = 2): pass")
138 self.check_roundtrip("def f(a = 5, b = 2): pass")
139 self.check_roundtrip("def f(*, a = 1, b = 2): pass")
140 self.check_roundtrip("def f(*, a = 1, b): pass")
141 self.check_roundtrip("def f(*, a, b = 2): pass")
142 self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
143 self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
144 self.check_roundtrip("def f(*args, **kwargs): pass")
145
146 def test_relative_import(self):
147 self.check_roundtrip(relative_import)
148
149 def test_nonlocal(self):
150 self.check_roundtrip(nonlocal_ex)
151
152 def test_raise_from(self):
153 self.check_roundtrip(raise_from)
154
155 def test_bytes(self):
156 self.check_roundtrip("b'123'")
157
158 def test_annotations(self):
159 self.check_roundtrip("def f(a : int): pass")
160 self.check_roundtrip("def f(a: int = 5): pass")
161 self.check_roundtrip("def f(*args: [int]): pass")
162 self.check_roundtrip("def f(**kwargs: dict): pass")
163 self.check_roundtrip("def f() -> None: pass")
164
165 def test_set_literal(self):
166 self.check_roundtrip("{'a', 'b', 'c'}")
167
168 def test_set_comprehension(self):
169 self.check_roundtrip("{x for x in range(5)}")
170
171 def test_dict_comprehension(self):
172 self.check_roundtrip("{x: x*x for x in range(10)}")
173
174 def test_class_decorators(self):
175 self.check_roundtrip(class_decorator)
Mark Dickinsonae100052010-06-28 19:44:20 +0000176
Mark Dickinson578aa562010-06-29 18:38:59 +0000177 def test_class_definition(self):
178 self.check_roundtrip("class A(metaclass=type, *[], **{}): pass")
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000179
Mark Dickinson8d6d7602010-06-30 08:32:11 +0000180 def test_elifs(self):
181 self.check_roundtrip(elif1)
182 self.check_roundtrip(elif2)
183
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000184class DirectoryTestCase(ASTTestCase):
185 """Test roundtrip behaviour on all files in Lib and Lib/test."""
186
187 # test directories, relative to the root of the distribution
188 test_directories = 'Lib', os.path.join('Lib', 'test')
189
190 def test_files(self):
191 # get names of files to test
192 dist_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
193
194 names = []
195 for d in self.test_directories:
196 test_dir = os.path.join(dist_dir, d)
197 for n in os.listdir(test_dir):
198 if n.endswith('.py') and not n.startswith('bad'):
199 names.append(os.path.join(test_dir, n))
200
201 for filename in names:
202 if test.support.verbose:
203 print('Testing %s' % filename)
204 source = read_pyfile(filename)
205 self.check_roundtrip(source)
206
207
Mark Dickinsonae100052010-06-28 19:44:20 +0000208def test_main():
Mark Dickinsond751c2e2010-06-29 14:08:23 +0000209 test.support.run_unittest(UnparseTestCase, DirectoryTestCase)
Mark Dickinsonae100052010-06-28 19:44:20 +0000210
211if __name__ == '__main__':
212 test_main()