/Lib/test/test_complex_args.py

http://unladen-swallow.googlecode.com/ · Python · 91 lines · 63 code · 22 blank · 6 comment · 1 complexity · 524eecd8f02a6a02916614e4bf4a23be MD5 · raw file

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