3 from test
import test_support
6 class ComplexArgsTestCase(unittest
.TestCase
):
8 def check(self
, func
, expected
, *args
):
9 self
.assertEqual(func(*args
), expected
)
11 # These functions are tested below as lambdas too. If you add a
12 # function test, also add a similar lambda test.
14 # Functions are wrapped in "exec" statements in order to
15 # silence Py3k warnings.
17 def test_func_parens_no_unpacking(self
):
18 exec textwrap
.dedent("""
19 def f(((((x))))): return x
21 # Inner parens are elided, same as: f(x,)
22 def f(((x)),): return x
26 def test_func_1(self
):
27 exec textwrap
.dedent("""
28 def f(((((x),)))): return x
29 self.check(f, 3, (3,))
30 def f(((((x)),))): return x
31 self.check(f, 4, (4,))
32 def f(((((x))),)): return x
33 self.check(f, 5, (5,))
34 def f(((x),)): return x
35 self.check(f, 6, (6,))
38 def test_func_2(self
):
39 exec textwrap
.dedent("""
40 def f(((((x)),),)): return x
41 self.check(f, 2, ((2,),))
44 def test_func_3(self
):
45 exec textwrap
.dedent("""
46 def f((((((x)),),),)): return x
47 self.check(f, 3, (((3,),),))
50 def test_func_complex(self
):
51 exec textwrap
.dedent("""
52 def f((((((x)),),),), a, b, c): return x, a, b, c
53 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
55 def f(((((((x)),)),),), a, b, c): return x, a, b, c
56 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
58 def f(a, b, c, ((((((x)),)),),)): return a, b, c, x
59 self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),))
62 # Duplicate the tests above, but for lambda. If you add a lambda test,
63 # also add a similar function test above.
65 def test_lambda_parens_no_unpacking(self
):
66 exec textwrap
.dedent("""
67 f = lambda (((((x))))): x
69 # Inner parens are elided, same as: f(x,)
74 def test_lambda_1(self
):
75 exec textwrap
.dedent("""
76 f = lambda (((((x),)))): x
77 self.check(f, 3, (3,))
78 f = lambda (((((x)),))): x
79 self.check(f, 4, (4,))
80 f = lambda (((((x))),)): x
81 self.check(f, 5, (5,))
82 f = lambda (((x),)): x
83 self.check(f, 6, (6,))
86 def test_lambda_2(self
):
87 exec textwrap
.dedent("""
88 f = lambda (((((x)),),)): x
89 self.check(f, 2, ((2,),))
92 def test_lambda_3(self
):
93 exec textwrap
.dedent("""
94 f = lambda ((((((x)),),),)): x
95 self.check(f, 3, (((3,),),))
98 def test_lambda_complex(self
):
99 exec textwrap
.dedent("""
100 f = lambda (((((x)),),),), a, b, c: (x, a, b, c)
101 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
103 f = lambda ((((((x)),)),),), a, b, c: (x, a, b, c)
104 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
106 f = lambda a, b, c, ((((((x)),)),),): (a, b, c, x)
107 self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),))
112 with test_support
.check_py3k_warnings(
113 ("tuple parameter unpacking has been removed", SyntaxWarning),
114 ("parenthesized argument names are invalid", SyntaxWarning)):
115 test_support
.run_unittest(ComplexArgsTestCase
)
117 if __name__
== "__main__":