|
1 |
|
2 import unittest |
|
3 from test import test_support |
|
4 |
|
5 class ComplexArgsTestCase(unittest.TestCase): |
|
6 |
|
7 def check(self, func, expected, *args): |
|
8 self.assertEqual(func(*args), expected) |
|
9 |
|
10 # These functions are tested below as lambdas too. If you add a function test, |
|
11 # also add a similar lambda test. |
|
12 |
|
13 def test_func_parens_no_unpacking(self): |
|
14 def f(((((x))))): return x |
|
15 self.check(f, 1, 1) |
|
16 # Inner parens are elided, same as: f(x,) |
|
17 def f(((x)),): return x |
|
18 self.check(f, 2, 2) |
|
19 |
|
20 def test_func_1(self): |
|
21 def f(((((x),)))): return x |
|
22 self.check(f, 3, (3,)) |
|
23 def f(((((x)),))): return x |
|
24 self.check(f, 4, (4,)) |
|
25 def f(((((x))),)): return x |
|
26 self.check(f, 5, (5,)) |
|
27 def f(((x),)): return x |
|
28 self.check(f, 6, (6,)) |
|
29 |
|
30 def test_func_2(self): |
|
31 def f(((((x)),),)): return x |
|
32 self.check(f, 2, ((2,),)) |
|
33 |
|
34 def test_func_3(self): |
|
35 def f((((((x)),),),)): return x |
|
36 self.check(f, 3, (((3,),),)) |
|
37 |
|
38 def test_func_complex(self): |
|
39 def f((((((x)),),),), a, b, c): return x, a, b, c |
|
40 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7) |
|
41 |
|
42 def f(((((((x)),)),),), a, b, c): return x, a, b, c |
|
43 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7) |
|
44 |
|
45 def f(a, b, c, ((((((x)),)),),)): return a, b, c, x |
|
46 self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),)) |
|
47 |
|
48 # Duplicate the tests above, but for lambda. If you add a lambda test, |
|
49 # also add a similar function test above. |
|
50 |
|
51 def test_lambda_parens_no_unpacking(self): |
|
52 f = lambda (((((x))))): x |
|
53 self.check(f, 1, 1) |
|
54 # Inner parens are elided, same as: f(x,) |
|
55 f = lambda ((x)),: x |
|
56 self.check(f, 2, 2) |
|
57 |
|
58 def test_lambda_1(self): |
|
59 f = lambda (((((x),)))): x |
|
60 self.check(f, 3, (3,)) |
|
61 f = lambda (((((x)),))): x |
|
62 self.check(f, 4, (4,)) |
|
63 f = lambda (((((x))),)): x |
|
64 self.check(f, 5, (5,)) |
|
65 f = lambda (((x),)): x |
|
66 self.check(f, 6, (6,)) |
|
67 |
|
68 def test_lambda_2(self): |
|
69 f = lambda (((((x)),),)): x |
|
70 self.check(f, 2, ((2,),)) |
|
71 |
|
72 def test_lambda_3(self): |
|
73 f = lambda ((((((x)),),),)): x |
|
74 self.check(f, 3, (((3,),),)) |
|
75 |
|
76 def test_lambda_complex(self): |
|
77 f = lambda (((((x)),),),), a, b, c: (x, a, b, c) |
|
78 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7) |
|
79 |
|
80 f = lambda ((((((x)),)),),), a, b, c: (x, a, b, c) |
|
81 self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7) |
|
82 |
|
83 f = lambda a, b, c, ((((((x)),)),),): (a, b, c, x) |
|
84 self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),)) |
|
85 |
|
86 |
|
87 def test_main(): |
|
88 test_support.run_unittest(ComplexArgsTestCase) |
|
89 |
|
90 if __name__ == "__main__": |
|
91 test_main() |