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 5import sys 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_self_default(self): 50 51 class X(A): 52 def f(self): 53 return 'X.f() -> ' + A.f(self) 54 55 x = X() 56 57 self.assertEqual ('X.f() -> A::f()', x.f()) 58 59 # This one properly raises the "dangling reference" exception 60 # self.failUnlessEqual ('X.f() -> A::f()', call_f(x)) 61 62 def test_wrapper_downcast(self): 63 a = pass_a(D()) 64 self.assertEqual('D::g()', a.g()) 65 66 def test_pure_virtual(self): 67 p = P() 68 self.assertRaises(RuntimeError, p.f) 69 70 q = Q() 71 self.assertEqual ('Q::f()', q.f()) 72 73 class R(P): 74 def f(self): 75 return 'R.f' 76 77 r = R() 78 self.assertEqual ('R.f', r.f()) 79 80 81def test(): 82 # remove the option that upsets unittest 83 import sys 84 sys.argv = [ x for x in sys.argv if x != '--broken-auto-ptr' ] 85 unittest.main() 86 87# This nasty hack basically says that if we're loaded by another module, we'll 88# be testing polymorphism2_auto_ptr_ext instead of polymorphism2_ext. 89if __name__ == "__main__": 90 from polymorphism2_ext import * 91 test() 92else: 93 from polymorphism2_auto_ptr_ext import * 94 95