test_calling.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Python test set -- built-in functions
  2. import unittest
  3. def f1(x, y, *args, **kwargs):
  4. return (x, y, args, kwargs)
  5. def f2(x=None, y=None, *args, **kwargs):
  6. return (x, y, args, kwargs)
  7. class C:
  8. def f1(self, x, y, *args, **kwargs):
  9. return (self, x, y, args, kwargs)
  10. def f2(self, x=None, y=None, *args, **kwargs):
  11. return (self, x, y, args, kwargs)
  12. class BuiltinTest(unittest.TestCase):
  13. def test_simple_call(self):
  14. self.assertEqual(f1(1, 2), (1, 2, (), {}))
  15. self.assertRaises(TypeError, lambda: f1(1))
  16. self.assertEqual(f2(1), (1, None, (), {}))
  17. self.assertEqual(f1(1, 2, 3, 4), (1, 2, (3, 4), {}))
  18. self.assertEqual(f1(1, y=2), (1, 2, (), {}))
  19. self.assertEqual(f1(1, z=3, y=2), (1, 2, (), {'z': 3}))
  20. self.assertRaises(TypeError, lambda: f1(1, 2, y=3))
  21. self.assertEqual(f1(1, *[2, 3]), (1, 2, (3,), {}))
  22. self.assertEqual(f1(**{'x': 1, 'y': 2}), (1, 2, (), {}))
  23. self.assertEqual(f1(**{'x': 1, 'y': 2, 'z': 3}), (1, 2, (), {'z': 3}))
  24. self.assertEqual(f1(*[1, 2, 3], **{'z': 4}), (1, 2, (3,), {'z': 4}))
  25. self.assertRaises(TypeError, lambda: f1(*[1, 2, 3], **{'y', 4}))
  26. def test_method_call(self):
  27. o = C()
  28. self.assertEqual(o.f1(1, 2), (o, 1, 2, (), {}))
  29. self.assertRaises(TypeError, lambda: o.f1(1))
  30. self.assertEqual(o.f2(1), (o, 1, None, (), {}))
  31. self.assertEqual(o.f1(1, 2, 3, 4), (o, 1, 2, (3, 4), {}))
  32. self.assertEqual(o.f1(1, y=2), (o, 1, 2, (), {}))
  33. self.assertEqual(o.f1(1, z=3, y=2), (o, 1, 2, (), {'z': 3}))
  34. self.assertRaises(TypeError, lambda: o.f1(1, 2, y=3))
  35. self.assertRaises(TypeError, lambda: o.f1(1, 2, self=3))
  36. self.assertEqual(o.f1(1, *[2, 3]), (o, 1, 2, (3,), {}))
  37. self.assertEqual(o.f1(**{'x': 1, 'y': 2}), (o, 1, 2, (), {}))
  38. self.assertEqual(o.f1(**{'x': 1, 'y': 2, 'z': 3}), (o, 1, 2, (), {'z': 3}))
  39. self.assertEqual(o.f1(*[1, 2, 3], **{'z': 4}), (o, 1, 2, (3,), {'z': 4}))
  40. self.assertRaises(TypeError, lambda: o.f1(*[1, 2, 3], **{'y', 4}))
  41. if __name__ == "__main__":
  42. unittest.main()