summaryrefslogtreecommitdiffstats
path: root/Demo/parser/test_unparse.py
diff options
context:
space:
mode:
Diffstat (limited to 'Demo/parser/test_unparse.py')
-rw-r--r--Demo/parser/test_unparse.py83
1 files changed, 79 insertions, 4 deletions
diff --git a/Demo/parser/test_unparse.py b/Demo/parser/test_unparse.py
index edc9ee1..5832b8f 100644
--- a/Demo/parser/test_unparse.py
+++ b/Demo/parser/test_unparse.py
@@ -6,7 +6,7 @@ import ast
import _ast
import unparse
-forelse = """\
+for_else = """\
def f():
for x in range(10):
break
@@ -15,7 +15,7 @@ def f():
z = 3
"""
-whileelse = """\
+while_else = """\
def g():
while True:
break
@@ -24,6 +24,37 @@ def g():
z = 3
"""
+relative_import = """\
+from . import fred
+from .. import barney
+from .australia import shrimp as prawns
+"""
+
+nonlocal_ex = """\
+def f():
+ x = 1
+ def g():
+ nonlocal x
+ x = 2
+ y = 7
+ def h():
+ nonlocal x, y
+"""
+
+# also acts as test for 'except ... as ...'
+raise_from = """\
+try:
+ 1 / 0
+except ZeroDivisionError as e:
+ raise ArithmeticError from e
+"""
+
+class_decorator = """\
+@f1(arg)
+@f2
+class Foo: pass
+"""
+
class UnparseTestCase(unittest.TestCase):
# Tests for specific bugs found in earlier versions of unparse
@@ -43,10 +74,10 @@ class UnparseTestCase(unittest.TestCase):
self.check_roundtrip("13 >> 7")
def test_for_else(self):
- self.check_roundtrip(forelse)
+ self.check_roundtrip(for_else)
def test_while_else(self):
- self.check_roundtrip(whileelse)
+ self.check_roundtrip(while_else)
def test_unary_parens(self):
self.check_roundtrip("(-1)**7")
@@ -57,6 +88,50 @@ class UnparseTestCase(unittest.TestCase):
self.check_roundtrip("1 < 4 <= 5")
self.check_roundtrip("a is b is c is not d")
+ def test_function_arguments(self):
+ self.check_roundtrip("def f(): pass")
+ self.check_roundtrip("def f(a): pass")
+ self.check_roundtrip("def f(b = 2): pass")
+ self.check_roundtrip("def f(a, b): pass")
+ self.check_roundtrip("def f(a, b = 2): pass")
+ self.check_roundtrip("def f(a = 5, b = 2): pass")
+ self.check_roundtrip("def f(*, a = 1, b = 2): pass")
+ self.check_roundtrip("def f(*, a = 1, b): pass")
+ self.check_roundtrip("def f(*, a, b = 2): pass")
+ self.check_roundtrip("def f(a, b = None, *, c, **kwds): pass")
+ self.check_roundtrip("def f(a=2, *args, c=5, d, **kwds): pass")
+ self.check_roundtrip("def f(*args, **kwargs): pass")
+
+ def test_relative_import(self):
+ self.check_roundtrip(relative_import)
+
+ def test_nonlocal(self):
+ self.check_roundtrip(nonlocal_ex)
+
+ def test_raise_from(self):
+ self.check_roundtrip(raise_from)
+
+ def test_bytes(self):
+ self.check_roundtrip("b'123'")
+
+ def test_annotations(self):
+ self.check_roundtrip("def f(a : int): pass")
+ self.check_roundtrip("def f(a: int = 5): pass")
+ self.check_roundtrip("def f(*args: [int]): pass")
+ self.check_roundtrip("def f(**kwargs: dict): pass")
+ self.check_roundtrip("def f() -> None: pass")
+
+ def test_set_literal(self):
+ self.check_roundtrip("{'a', 'b', 'c'}")
+
+ def test_set_comprehension(self):
+ self.check_roundtrip("{x for x in range(5)}")
+
+ def test_dict_comprehension(self):
+ self.check_roundtrip("{x: x*x for x in range(10)}")
+
+ def test_class_decorators(self):
+ self.check_roundtrip(class_decorator)
def test_main():
test.support.run_unittest(UnparseTestCase)