aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/tests/smartbinding/smart_pointer_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken2/tests/smartbinding/smart_pointer_test.py')
-rw-r--r--sources/shiboken2/tests/smartbinding/smart_pointer_test.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/sources/shiboken2/tests/smartbinding/smart_pointer_test.py b/sources/shiboken2/tests/smartbinding/smart_pointer_test.py
index e1883c7cc..d7ddf963c 100644
--- a/sources/shiboken2/tests/smartbinding/smart_pointer_test.py
+++ b/sources/shiboken2/tests/smartbinding/smart_pointer_test.py
@@ -29,7 +29,16 @@
##
#############################################################################
+from __future__ import print_function
+
+import gc
+import os
+import sys
import unittest
+
+sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+from shiboken_paths import init_paths
+init_paths()
from copy import copy
from smart import Obj, Registry, Integer
@@ -79,6 +88,9 @@ class SmartPointerTests(unittest.TestCase):
result = ptrToObj.takeInteger(ptrToObj.m_internalInteger)
self.assertEqual(integerCount(), 2)
result = None
+ if integerCount() > 1:
+ gc.collect()
+ print('Running garbage collector for reference test', file=sys.stderr)
self.assertEqual(integerCount(), 1)
# Make a copy of the shared pointer, object count should not change.
@@ -116,6 +128,10 @@ class SmartPointerTests(unittest.TestCase):
self.assertEqual(integer.m_int, 50)
# Set and get a member value via shared pointer (like operator->).
+ ptrToInteger.setValue(150)
+ self.assertEqual(ptrToInteger.value(), 150)
+
+ # Set and get a member field via shared pointer (like operator->).
ptrToInteger.m_int = 100
self.assertEqual(ptrToInteger.m_int, 100)
@@ -143,6 +159,18 @@ class SmartPointerTests(unittest.TestCase):
self.assertEqual(objCount(), 0)
self.assertEqual(integerCount(), 0)
+ def testConstIntegerSmartPointer(self):
+ # Uncomment to see more debug info about creation of objects and ref counts.
+ # Registry.getInstance().setShouldPrint(True)
+
+ # Create Obj.
+ o = Obj()
+ ptrToConstInteger = o.giveSharedPtrToConstInteger()
+ self.assertEqual(ptrToConstInteger.m_int, 456)
+ result = o.takeSharedPtrToConstInteger(ptrToConstInteger)
+ self.assertEqual(result, 456)
+ self.assertEqual(ptrToConstInteger.value(), 456)
+
def testSmartPointersWithNamespace(self):
# Create the main object
o = Obj()
@@ -171,7 +199,7 @@ class SmartPointerTests(unittest.TestCase):
self.assertEqual(objCount(), 10)
# clear and delete all objects in the list
- ptrToObjList.clear()
+ del ptrToObjList[:] # Python 2.7 lists have no clear method
self.assertEqual(len(ptrToObjList), 0)
self.assertEqual(objCount(), 1)
@@ -186,6 +214,19 @@ class SmartPointerTests(unittest.TestCase):
except AttributeError as error:
self.assertEqual(error.args[0], "'smart.SharedPtr_Obj' object has no attribute 'typo'")
+ def testSmartPointerConversions(self):
+ # Create Obj.
+ o = Obj()
+ self.assertEqual(objCount(), 1)
+ self.assertEqual(integerCount(), 1)
+
+ # Create a shared pointer to an Integer2
+ integer2 = o.giveSharedPtrToInteger2()
+ self.assertEqual(integer2.value(), 456)
+
+ # pass Smart<Integer2> to a function that accepts Smart<Integer>
+ r = o.takeSharedPtrToInteger(integer2)
+ self.assertEqual(r, integer2.value())
if __name__ == '__main__':
unittest.main()