Coverage for tests/test_pySharedPtr.py: 51%
33 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-15 07:52 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-15 07:52 +0000
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#
22import gc
23import unittest
25import _inheritance
28class PySharedPtrTestSuite(unittest.TestCase):
29 """Test the ability of PySharedPtr to safely pass hybrid objects
30 between C++ and Python."""
32 class PyDerived(_inheritance.CppBase):
33 def __init__(self):
34 super().__init__()
36 # Don't override overridable()
38 def abstract(self):
39 return "py-abstract"
41 class PyCppDerived(_inheritance.CppDerived):
42 def __init__(self):
43 super().__init__()
45 def nonOverridable(self):
46 return "error -- should never be called!"
48 def overridable(self):
49 return "py-override"
51 def abstract(self):
52 return "py-abstract"
54 def checkGarbageCollection(self, concreteClass, returns):
55 """Generic test for whether a C++/Python class survives garbage collection.
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())
68 gc.collect()
70 retrieved = _inheritance.getFromStorage(storage)
71 self.assertIsInstance(retrieved, _inheritance.CppBase)
72 self.assertIsInstance(retrieved, concreteClass)
73 self.assertEqual(_inheritance.printFromCpp(retrieved), " ".join(returns))
75 def testPyDerivedGarbageCollection(self):
76 self.checkGarbageCollection(self.PyDerived, ("42", "", "py-abstract"))
78 def testCppDerivedGarbageCollection(self):
79 self.checkGarbageCollection(_inheritance.CppDerived, ("42", "overridden", "implemented"))
81 def testPyCppDerivedGarbageCollection(self):
82 self.checkGarbageCollection(self.PyCppDerived, ("42", "py-override", "py-abstract"))
85if __name__ == "__main__": 85 ↛ 86line 85 didn't jump to line 86, because the condition on line 85 was never true
86 unittest.main()