1 |
|
---|
2 | import unittest
|
---|
3 | from test import test_support
|
---|
4 | import textwrap
|
---|
5 |
|
---|
6 | class ComplexArgsTestCase(unittest.TestCase):
|
---|
7 |
|
---|
8 | def check(self, func, expected, *args):
|
---|
9 | self.assertEqual(func(*args), expected)
|
---|
10 |
|
---|
11 | # These functions are tested below as lambdas too. If you add a
|
---|
12 | # function test, also add a similar lambda test.
|
---|
13 |
|
---|
14 | # Functions are wrapped in "exec" statements in order to
|
---|
15 | # silence Py3k warnings.
|
---|
16 |
|
---|
17 | def test_func_parens_no_unpacking(self):
|
---|
18 | exec textwrap.dedent("""
|
---|
19 | def f(((((x))))): return x
|
---|
20 | self.check(f, 1, 1)
|
---|
21 | # Inner parens are elided, same as: f(x,)
|
---|
22 | def f(((x)),): return x
|
---|
23 | self.check(f, 2, 2)
|
---|
24 | """)
|
---|
25 |
|
---|
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,))
|
---|
36 | """)
|
---|
37 |
|
---|
38 | def test_func_2(self):
|
---|
39 | exec textwrap.dedent("""
|
---|
40 | def f(((((x)),),)): return x
|
---|
41 | self.check(f, 2, ((2,),))
|
---|
42 | """)
|
---|
43 |
|
---|
44 | def test_func_3(self):
|
---|
45 | exec textwrap.dedent("""
|
---|
46 | def f((((((x)),),),)): return x
|
---|
47 | self.check(f, 3, (((3,),),))
|
---|
48 | """)
|
---|
49 |
|
---|
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)
|
---|
54 |
|
---|
55 | def f(((((((x)),)),),), a, b, c): return x, a, b, c
|
---|
56 | self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
|
---|
57 |
|
---|
58 | def f(a, b, c, ((((((x)),)),),)): return a, b, c, x
|
---|
59 | self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),))
|
---|
60 | """)
|
---|
61 |
|
---|
62 | # Duplicate the tests above, but for lambda. If you add a lambda test,
|
---|
63 | # also add a similar function test above.
|
---|
64 |
|
---|
65 | def test_lambda_parens_no_unpacking(self):
|
---|
66 | exec textwrap.dedent("""
|
---|
67 | f = lambda (((((x))))): x
|
---|
68 | self.check(f, 1, 1)
|
---|
69 | # Inner parens are elided, same as: f(x,)
|
---|
70 | f = lambda ((x)),: x
|
---|
71 | self.check(f, 2, 2)
|
---|
72 | """)
|
---|
73 |
|
---|
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,))
|
---|
84 | """)
|
---|
85 |
|
---|
86 | def test_lambda_2(self):
|
---|
87 | exec textwrap.dedent("""
|
---|
88 | f = lambda (((((x)),),)): x
|
---|
89 | self.check(f, 2, ((2,),))
|
---|
90 | """)
|
---|
91 |
|
---|
92 | def test_lambda_3(self):
|
---|
93 | exec textwrap.dedent("""
|
---|
94 | f = lambda ((((((x)),),),)): x
|
---|
95 | self.check(f, 3, (((3,),),))
|
---|
96 | """)
|
---|
97 |
|
---|
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)
|
---|
102 |
|
---|
103 | f = lambda ((((((x)),)),),), a, b, c: (x, a, b, c)
|
---|
104 | self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
|
---|
105 |
|
---|
106 | f = lambda a, b, c, ((((((x)),)),),): (a, b, c, x)
|
---|
107 | self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),))
|
---|
108 | """)
|
---|
109 |
|
---|
110 |
|
---|
111 | def test_main():
|
---|
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)
|
---|
116 |
|
---|
117 | if __name__ == "__main__":
|
---|
118 | test_main()
|
---|