blob: b0a773dc4953fd9fb5aab9ea6f2ec8d595cd4726 [file] [log] [blame]
Mark Dickinsonae100052010-06-28 19:44:20 +00001import unittest
2import test.support
3
4import io
5import ast
6import _ast
7import unparse
8
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +00009for_else = """\
Mark Dickinsonae100052010-06-28 19:44:20 +000010def f():
11 for x in range(10):
12 break
13 else:
14 y = 2
15 z = 3
16"""
17
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000018while_else = """\
Mark Dickinsonae100052010-06-28 19:44:20 +000019def g():
20 while True:
21 break
22 else:
23 y = 2
24 z = 3
25"""
26
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000027relative_import = """\
28from . import fred
29from .. import barney
30from .australia import shrimp as prawns
31"""
32
33nonlocal_ex = """\
34def f():
35 x = 1
36 def g():
37 nonlocal x
38 x = 2
39 y = 7
40 def h():
41 nonlocal x, y
42"""
43
44# also acts as test for 'except ... as ...'
45raise_from = """\
46try:
47 1 / 0
48except ZeroDivisionError as e:
49 raise ArithmeticError from e
50"""
51
52class_decorator = """\
53@f1(arg)
54@f2
55class Foo: pass
56"""
57
Mark Dickinsonae100052010-06-28 19:44:20 +000058class UnparseTestCase(unittest.TestCase):
59 # Tests for specific bugs found in earlier versions of unparse
60
61 def check_roundtrip(self, code1, filename="internal"):
62 ast1 = compile(code1, filename, "exec", _ast.PyCF_ONLY_AST)
63 unparse_buffer = io.StringIO()
64 unparse.Unparser(ast1, unparse_buffer)
65 code2 = unparse_buffer.getvalue()
66 ast2 = compile(code2, filename, "exec", _ast.PyCF_ONLY_AST)
67 self.assertEqual(ast.dump(ast1), ast.dump(ast2))
68
69 def test_del_statement(self):
70 self.check_roundtrip("del x, y, z")
71
72 def test_shifts(self):
73 self.check_roundtrip("45 << 2")
74 self.check_roundtrip("13 >> 7")
75
76 def test_for_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000077 self.check_roundtrip(for_else)
Mark Dickinsonae100052010-06-28 19:44:20 +000078
79 def test_while_else(self):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000080 self.check_roundtrip(while_else)
Mark Dickinsonae100052010-06-28 19:44:20 +000081
82 def test_unary_parens(self):
83 self.check_roundtrip("(-1)**7")
84 self.check_roundtrip("not True or False")
85 self.check_roundtrip("True or not False")
86
Mark Dickinson3eb02902010-06-29 08:52:36 +000087 def test_integer_parens(self):
88 self.check_roundtrip("3 .__abs__()")
89
Mark Dickinson8042e282010-06-29 10:01:48 +000090 def test_huge_float(self):
91 self.check_roundtrip("1e1000")
92 self.check_roundtrip("-1e1000")
93
94 def test_lambda_parentheses(self):
95 self.check_roundtrip("(lambda: int)()")
96
Mark Dickinsonf5451e52010-06-28 20:09:18 +000097 def test_chained_comparisons(self):
98 self.check_roundtrip("1 < 4 <= 5")
99 self.check_roundtrip("a is b is c is not d")
100
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000101 def test_function_arguments(self):
102 self.check_roundtrip("def f(): pass")
103 self.check_roundtrip("def f(a): pass")
104 self.check_roundtrip("def f(b = 2): pass")
105 self.check_roundtrip("def f(a, b): pass")
106 self.check_roundtrip("def f(a, b = 2): pass")
107 self.check_roundtrip("def f(a = 5, b = 2): pass")
108 self.check_roundtrip("def f(*, a = 1, b = 2): pass")
109 self.check_roundtrip("def f(*, a = 1, b): pass")
110 self.check_roundtrip("def f(*, a, b = 2): pass")
111 self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
112 self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
113 self.check_roundtrip("def f(*args, **kwargs): pass")
114
115 def test_relative_import(self):
116 self.check_roundtrip(relative_import)
117
118 def test_nonlocal(self):
119 self.check_roundtrip(nonlocal_ex)
120
121 def test_raise_from(self):
122 self.check_roundtrip(raise_from)
123
124 def test_bytes(self):
125 self.check_roundtrip("b'123'")
126
127 def test_annotations(self):
128 self.check_roundtrip("def f(a : int): pass")
129 self.check_roundtrip("def f(a: int = 5): pass")
130 self.check_roundtrip("def f(*args: [int]): pass")
131 self.check_roundtrip("def f(**kwargs: dict): pass")
132 self.check_roundtrip("def f() -> None: pass")
133
134 def test_set_literal(self):
135 self.check_roundtrip("{'a', 'b', 'c'}")
136
137 def test_set_comprehension(self):
138 self.check_roundtrip("{x for x in range(5)}")
139
140 def test_dict_comprehension(self):
141 self.check_roundtrip("{x: x*x for x in range(10)}")
142
143 def test_class_decorators(self):
144 self.check_roundtrip(class_decorator)
Mark Dickinsonae100052010-06-28 19:44:20 +0000145
146def test_main():
147 test.support.run_unittest(UnparseTestCase)
148
149if __name__ == '__main__':
150 test_main()