blob: 5832b8ffb7444d31b336da8b4f155f2af9c636ca [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 Dickinsonf5451e52010-06-28 20:09:18 +000087 def test_chained_comparisons(self):
88 self.check_roundtrip("1 < 4 <= 5")
89 self.check_roundtrip("a is b is c is not d")
90
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000091 def test_function_arguments(self):
92 self.check_roundtrip("def f(): pass")
93 self.check_roundtrip("def f(a): pass")
94 self.check_roundtrip("def f(b = 2): pass")
95 self.check_roundtrip("def f(a, b): pass")
96 self.check_roundtrip("def f(a, b = 2): pass")
97 self.check_roundtrip("def f(a = 5, b = 2): pass")
98 self.check_roundtrip("def f(*, a = 1, b = 2): pass")
99 self.check_roundtrip("def f(*, a = 1, b): pass")
100 self.check_roundtrip("def f(*, a, b = 2): pass")
101 self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
102 self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
103 self.check_roundtrip("def f(*args, **kwargs): pass")
104
105 def test_relative_import(self):
106 self.check_roundtrip(relative_import)
107
108 def test_nonlocal(self):
109 self.check_roundtrip(nonlocal_ex)
110
111 def test_raise_from(self):
112 self.check_roundtrip(raise_from)
113
114 def test_bytes(self):
115 self.check_roundtrip("b'123'")
116
117 def test_annotations(self):
118 self.check_roundtrip("def f(a : int): pass")
119 self.check_roundtrip("def f(a: int = 5): pass")
120 self.check_roundtrip("def f(*args: [int]): pass")
121 self.check_roundtrip("def f(**kwargs: dict): pass")
122 self.check_roundtrip("def f() -> None: pass")
123
124 def test_set_literal(self):
125 self.check_roundtrip("{'a', 'b', 'c'}")
126
127 def test_set_comprehension(self):
128 self.check_roundtrip("{x for x in range(5)}")
129
130 def test_dict_comprehension(self):
131 self.check_roundtrip("{x: x*x for x in range(10)}")
132
133 def test_class_decorators(self):
134 self.check_roundtrip(class_decorator)
Mark Dickinsonae100052010-06-28 19:44:20 +0000135
136def test_main():
137 test.support.run_unittest(UnparseTestCase)
138
139if __name__ == '__main__':
140 test_main()