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) 4import unittest 5from polymorphism_ext import * 6 7class PolymorphTest(unittest.TestCase): 8 9 def testReturnCpp(self): 10 11 # Python Created Object With Same Id As 12 # Cpp Created B Object 13 # b = B(872) 14 15 # Get Reference To Cpp Created B Object 16 a = getBCppObj() 17 18 # Python Created B Object and Cpp B Object 19 # Should have same result by calling f() 20 self.assertEqual ('B::f()', a.f()) 21 self.assertEqual ('B::f()', call_f(a)) 22 self.assertEqual ('A::f()', call_f(A())) 23 24 def test_references(self): 25 # B is not exposed to Python 26 a = getBCppObj() 27 self.assertEqual(type(a), A) 28 29 # C is exposed to Python 30 c = getCCppObj() 31 self.assertEqual(type(c), C) 32 33 def test_factory(self): 34 self.assertEqual(type(factory(0)), A) 35 self.assertEqual(type(factory(1)), A) 36 self.assertEqual(type(factory(2)), C) 37 38 def test_return_py(self): 39 40 class X(A): 41 def f(self): 42 return 'X.f' 43 44 x = X() 45 46 self.assertEqual ('X.f', x.f()) 47 self.assertEqual ('X.f', call_f(x)) 48 49 def test_wrapper_downcast(self): 50 a = pass_a(D()) 51 self.assertEqual('D::g()', a.g()) 52 53 def test_pure_virtual(self): 54 p = P() 55 self.assertRaises(RuntimeError, p.f) 56 57 q = Q() 58 self.assertEqual ('Q::f()', q.f()) 59 60 class R(P): 61 def f(self): 62 return 'R.f' 63 64 r = R() 65 self.assertEqual ('R.f', r.f()) 66 67 68if __name__ == "__main__": 69 70 # remove the option which upsets unittest 71 import sys 72 sys.argv = [ x for x in sys.argv if x != '--broken-auto-ptr' ] 73 74 unittest.main() 75