Coverage for tests/test_pySharedPtr.py: 59%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

33 statements  

1# 

2# Developed for the LSST Data Management System. 

3# This product includes software developed by the LSST Project 

4# (https://www.lsst.org). 

5# See the COPYRIGHT file at the top-level directory of this distribution 

6# for details of code ownership. 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the GNU General Public License 

19# along with this program. If not, see <https://www.gnu.org/licenses/>. 

20# 

21 

22import gc 

23import unittest 

24 

25import _inheritance 

26 

27 

28class PySharedPtrTestSuite(unittest.TestCase): 

29 """Test the ability of PySharedPtr to safely pass hybrid objects 

30 between C++ and Python.""" 

31 

32 class PyDerived(_inheritance.CppBase): 

33 def __init__(self): 

34 super().__init__() 

35 

36 # Don't override overridable() 

37 

38 def abstract(self): 

39 return "py-abstract" 

40 

41 class PyCppDerived(_inheritance.CppDerived): 

42 def __init__(self): 

43 super().__init__() 

44 

45 def nonOverridable(self): 

46 return "error -- should never be called!" 

47 

48 def overridable(self): 

49 return "py-override" 

50 

51 def abstract(self): 

52 return "py-abstract" 

53 

54 def checkGarbageCollection(self, concreteClass, returns): 

55 """Generic test for whether a C++/Python class survives garbage collection. 

56 

57 Parameters 

58 ---------- 

59 concreteClass : `_inheritance.CppBase`-type 

60 The class to test. Must be default-constructible. 

61 returns : `tuple` 

62 A tuple of the return values from ``concreteClass``'s 

63 ``nonOverridable``, ``overridable``, and ``abstract`` methods, in 

64 that order. 

65 """ 

66 storage = _inheritance.CppStorage(concreteClass()) 

67 

68 gc.collect() 

69 

70 retrieved = _inheritance.getFromStorage(storage) 

71 self.assertIsInstance(retrieved, _inheritance.CppBase) 

72 self.assertIsInstance(retrieved, concreteClass) 

73 self.assertEqual(_inheritance.printFromCpp(retrieved), " ".join(returns)) 

74 

75 def testPyDerivedGarbageCollection(self): 

76 self.checkGarbageCollection(self.PyDerived, ("42", "", "py-abstract")) 

77 

78 def testCppDerivedGarbageCollection(self): 

79 self.checkGarbageCollection(_inheritance.CppDerived, ("42", "overridden", "implemented")) 

80 

81 def testPyCppDerivedGarbageCollection(self): 

82 self.checkGarbageCollection(self.PyCppDerived, ("42", "py-override", "py-abstract")) 

83 

84 

85if __name__ == "__main__": 85 ↛ 86line 85 didn't jump to line 86, because the condition on line 85 was never true

86 unittest.main()