13a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
23a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitzimport unittest
33a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitzfrom test import test_support
4af61719ec354015566028250a2912efa30f4cda4Florent Xiclunaimport textwrap
53a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
63a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitzclass ComplexArgsTestCase(unittest.TestCase):
73a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
83a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def check(self, func, expected, *args):
93a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.assertEqual(func(*args), expected)
103a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
11af61719ec354015566028250a2912efa30f4cda4Florent Xicluna    # These functions are tested below as lambdas too.  If you add a
12af61719ec354015566028250a2912efa30f4cda4Florent Xicluna    # function test, also add a similar lambda test.
13af61719ec354015566028250a2912efa30f4cda4Florent Xicluna
14af61719ec354015566028250a2912efa30f4cda4Florent Xicluna    # Functions are wrapped in "exec" statements in order to
15af61719ec354015566028250a2912efa30f4cda4Florent Xicluna    # silence Py3k warnings.
163a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
173a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_func_parens_no_unpacking(self):
18af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
193a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((((x))))): return x
203a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 1, 1)
213a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        # Inner parens are elided, same as: f(x,)
223a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((x)),): return x
233a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 2, 2)
24af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
253a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
263a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_func_1(self):
27af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
283a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((((x),)))): return x
293a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 3, (3,))
303a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((((x)),))): return x
313a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 4, (4,))
323a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((((x))),)): return x
333a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 5, (5,))
343a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((x),)): return x
353a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 6, (6,))
36af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
373a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
383a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_func_2(self):
39af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
403a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((((x)),),)): return x
413a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 2, ((2,),))
42af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
433a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
443a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_func_3(self):
45af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
463a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f((((((x)),),),)): return x
473a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 3, (((3,),),))
48af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
493a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
503a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_func_complex(self):
51af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
523a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f((((((x)),),),), a, b, c): return x, a, b, c
533a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
543a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
553a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(((((((x)),)),),), a, b, c): return x, a, b, c
563a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
573a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
583a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        def f(a, b, c, ((((((x)),)),),)): return a, b, c, x
593a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),))
60af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
613a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
623a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    # Duplicate the tests above, but for lambda.  If you add a lambda test,
633a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    # also add a similar function test above.
643a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
653a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_lambda_parens_no_unpacking(self):
66af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
673a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda (((((x))))): x
683a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 1, 1)
693a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        # Inner parens are elided, same as: f(x,)
703a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda ((x)),: x
713a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 2, 2)
72af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
733a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
743a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_lambda_1(self):
75af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
763a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda (((((x),)))): x
773a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 3, (3,))
783a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda (((((x)),))): x
793a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 4, (4,))
803a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda (((((x))),)): x
813a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 5, (5,))
823a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda (((x),)): x
833a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 6, (6,))
84af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
853a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
863a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_lambda_2(self):
87af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
883a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda (((((x)),),)): x
893a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 2, ((2,),))
90af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
913a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
923a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_lambda_3(self):
93af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
943a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda ((((((x)),),),)): x
953a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, 3, (((3,),),))
96af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
973a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
983a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    def test_lambda_complex(self):
99af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        exec textwrap.dedent("""
1003a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda (((((x)),),),), a, b, c: (x, a, b, c)
1013a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
1023a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
1033a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda ((((((x)),)),),), a, b, c: (x, a, b, c)
1043a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, (3, 9, 8, 7), (((3,),),), 9, 8, 7)
1053a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
1063a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        f = lambda a, b, c, ((((((x)),)),),): (a, b, c, x)
1073a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz        self.check(f, (9, 8, 7, 3), 9, 8, 7, (((3,),),))
108af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        """)
1093a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
1103a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
1113a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitzdef test_main():
112af61719ec354015566028250a2912efa30f4cda4Florent Xicluna    with test_support.check_py3k_warnings(
113af61719ec354015566028250a2912efa30f4cda4Florent Xicluna            ("tuple parameter unpacking has been removed", SyntaxWarning),
114af61719ec354015566028250a2912efa30f4cda4Florent Xicluna            ("parenthesized argument names are invalid", SyntaxWarning)):
115af61719ec354015566028250a2912efa30f4cda4Florent Xicluna        test_support.run_unittest(ComplexArgsTestCase)
1163a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz
1173a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitzif __name__ == "__main__":
1183a23017bb2309f6ba44090fb322f9681bf49c4c3Neal Norwitz    test_main()
119