polymorphism.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. import unittest
  5. from polymorphism_ext import *
  6. class PolymorphTest(unittest.TestCase):
  7. def testReturnCpp(self):
  8. # Python Created Object With Same Id As
  9. # Cpp Created B Object
  10. # b = B(872)
  11. # Get Reference To Cpp Created B Object
  12. a = getBCppObj()
  13. # Python Created B Object and Cpp B Object
  14. # Should have same result by calling f()
  15. self.assertEqual ('B::f()', a.f())
  16. self.assertEqual ('B::f()', call_f(a))
  17. self.assertEqual ('A::f()', call_f(A()))
  18. def test_references(self):
  19. # B is not exposed to Python
  20. a = getBCppObj()
  21. self.assertEqual(type(a), A)
  22. # C is exposed to Python
  23. c = getCCppObj()
  24. self.assertEqual(type(c), C)
  25. def test_factory(self):
  26. self.assertEqual(type(factory(0)), A)
  27. self.assertEqual(type(factory(1)), A)
  28. self.assertEqual(type(factory(2)), C)
  29. def test_return_py(self):
  30. class X(A):
  31. def f(self):
  32. return 'X.f'
  33. x = X()
  34. self.assertEqual ('X.f', x.f())
  35. self.assertEqual ('X.f', call_f(x))
  36. def test_wrapper_downcast(self):
  37. a = pass_a(D())
  38. self.assertEqual('D::g()', a.g())
  39. def test_pure_virtual(self):
  40. p = P()
  41. self.assertRaises(RuntimeError, p.f)
  42. q = Q()
  43. self.assertEqual ('Q::f()', q.f())
  44. class R(P):
  45. def f(self):
  46. return 'R.f'
  47. r = R()
  48. self.assertEqual ('R.f', r.f())
  49. if __name__ == "__main__":
  50. # remove the option which upsets unittest
  51. import sys
  52. sys.argv = [ x for x in sys.argv if x != '--broken-auto-ptr' ]
  53. unittest.main()