def testBlackBoxReleasingUnknownObjectType(self): '''Asks BlackBox to release an unknown ObjectType.''' o1 = ObjectType() o2 = ObjectType() bb = BlackBox() o1_ticket = bb.keepObjectType(o1) o3 = bb.retrieveObjectType(-5) self.assertEqual(o3, None)
def testEventLoop(self): '''Calling virtuals in a event loop''' objs = [ObjectType(), NoOverride(), Override()] evaluated = ObjectType.processEvent(objs, Event(Event.BASIC_EVENT)) self.assertEqual(evaluated, 3) self.assert_(objs[2].called)
def testReparentWithTheSameParent(self): '''Set the same parent twice to check if the ref continue the same''' obj = ObjectType() parent = ObjectType() self.assertEqual(sys.getrefcount(obj), 2) obj.setParent(parent) self.assertEqual(sys.getrefcount(obj), 3) obj.setParent(parent) self.assertEqual(sys.getrefcount(obj), 3)
def testObjectTypeReferenceAndPointer(self): '''In C++ we have "function(const ObjectType&, int)", in Python we add "function(ValueType)".''' obj = ObjectType() obj.setObjectName('sbrubbles') multiplier = 3.0 control = len(obj.objectName()) * multiplier self.assertEqual(SampleNamespace.passReferenceToObjectType(obj, multiplier), control) control = len(obj.objectName()) self.assertEqual(SampleNamespace.passReferenceToObjectType(obj), control)
def testDeleteChild(self): '''Delete child in python should not invalidate child''' parent = ObjectType() child = ObjectType(parent) name = ''.join(random.sample(string.letters, 5)) child.setObjectName(name) del child new_child = parent.children()[0] self.assertEqual(new_child.objectName(), name)
def testParentDestructor(self): '''Delete parent object should invalidate child''' parent = ObjectType() child = ObjectType() child.setParent(parent) refcount_before = sys.getrefcount(child) del parent self.assertRaises(RuntimeError, child.objectName) self.assertEqual(sys.getrefcount(child), refcount_before-1)
def testRecursiveParentDelete(self): '''Delete parent should invalidate grandchildren''' parent = ObjectType() child = ObjectType(parent) grandchild = ObjectType(child) del parent self.assertRaises(RuntimeError, child.objectName) self.assertEqual(sys.getrefcount(child), 2) self.assertRaises(RuntimeError, grandchild.objectName) self.assertEqual(sys.getrefcount(grandchild), 2)
def testParentDestructorMultipleChildren(self): '''Delete parent object should invalidate all children''' parent = ObjectType() children = [ObjectType() for _ in range(10)] for child in children: child.setParent(parent) del parent for i, child in enumerate(children): self.assertRaises(RuntimeError, child.objectName) self.assertEqual(sys.getrefcount(child), 4)
def scope(): class ExtObj1(ObjectType): def __init__(self): ObjectType.__init__(self) obj = ExtObj1() child = ObjectType(parent=obj) self.assertEqual(obj.takeChild(child), child) class ExtObj2(ObjectType): def __init__(self): ObjectType.__init__(self) obj = ExtObj2() child = ObjectType(parent=obj) self.assertEqual(obj.takeChild(child), child)
def testReparentedObjectTypeIdentity(self): '''Reparent children from one parent to another.''' object_list = [] old_parent = ObjectType() new_parent = ObjectType() for i in range(3): obj = ObjectType() object_list.append(obj) obj.setParent(old_parent) for obj in object_list: obj.setParent(new_parent) for child in new_parent.children(): self.assertTrue(child in object_list)
def testReparentedObjectTypeIdentityWithChildrenCreatedInCpp(self): '''Reparent children created in C++ from one parent to another.''' object_list = [] old_parent = ObjectType() new_parent = ObjectType() for i in range(3): obj = ObjectType.create() object_list.append(obj) obj.setParent(old_parent) for obj in object_list: obj.setParent(new_parent) for child in new_parent.children(): self.assert_(child in object_list)
def testDeleteChild(self): '''Delete child in C++ should invalidate child - using C++ wrapper''' parent = ObjectType() parent.setObjectName('parent') child = ObjectType(parent) child.setObjectName('child') parent.killChild('child') self.assertRaises(RuntimeError, child.objectName) self.assertEqual(parent.objectName(), 'parent')
def testNextInFocusChainCycleList(self): '''As above but in for a list of objects''' parents = [] children = [] focus_chains = [] for i in range(10): parent = ObjectType() child = ObjectType(parent) next_focus = child.nextInFocusChain() parents.append(parent) children.append(child) focus_chains.append(next_focus) shiboken.invalidate(parents)
def testInvalidProperty(self): o = ObjectType() try: o.typo self.assertFail() except AttributeError as error: self.assertEqual(error.args[0], "'sample.ObjectType' object has no attribute 'typo'")
def testObjectInvalidatedAfterUseAsParameter(self): '''Tries to use wrapper invalidated after use as a parameter to another method.''' eot = ExtObjectType() ot = ObjectType() eot.causeEvent(Event.ANY_EVENT) self.assertEqual(eot.type_of_last_event, Event.ANY_EVENT) self.assertRaises(RuntimeError, ot.event, eot.last_event)
def testEvent(self): '''C++ calling Python reimplementation of virtual in multiple inheritance''' obj = ImplementsBoth() self.assertTrue(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT))) self.assertTrue(obj.event_processed) self.assertEqual(obj.callSum1(1, 2, 3), 12)
def testCpp(self): '''C++ calling C++ virtual method in multiple inheritance scenario''' obj = ImplementsNone() self.assertTrue(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT))) self.assertRaises(AttributeError, getattr, obj, 'event_processed') self.assertEqual(obj.callSum0(1, 2, 3), 6)
def testCpp(self): '''C++ calling C++ virtual method in multiple inheritance scenario''' obj = ImplementsNone() self.assert_(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT))) self.assertRaises(AttributeError, getattr, obj, 'event_processed') self.assertEqual(obj.callSum0(1, 2, 3), 6)
def testEvent(self): '''C++ calling Python reimplementation of virtual in multiple inheritance''' obj = ImplementsBoth() self.assert_(ObjectType.processEvent([obj], Event(Event.BASIC_EVENT))) self.assert_(obj.event_processed) self.assertEqual(obj.callSum1(1, 2, 3), 12)
def testDecideCallWithInheritance(self): '''Call methods overloads that receive parent and inheritor classes' instances.''' objecttype = ObjectType() objectmodel = ObjectModel() self.assertEqual(ObjectModel.receivesObjectTypeFamily(objecttype), ObjectModel.ObjectTypeCalled) self.assertNotEqual(ObjectModel.receivesObjectTypeFamily(objecttype), ObjectModel.ObjectModelCalled) self.assertEqual(ObjectModel.receivesObjectTypeFamily(objectmodel), ObjectModel.ObjectModelCalled) self.assertNotEqual(ObjectModel.receivesObjectTypeFamily(objectmodel), ObjectModel.ObjectTypeCalled)
def testOwnershipTransferenceCppCreated(self): '''Ownership transference using a C++ created object.''' o1 = ObjectType.create() o1.setObjectName('object1') o1_refcnt = sys.getrefcount(o1) bb = BlackBox() o1_ticket = bb.keepObjectType(o1) self.assertRaises(RuntimeError, o1.objectName)
def testUseDefaultValues(self): o = ObjectType() o.setObjectNameWithSize(size=3) self.assertEqual(o.objectName(), "<un") # use name='unknown' default argument o.setObjectSplittedName("") self.assertEqual(o.objectName(), "<unknown>") # user prefix='<unk' and suffix='nown>'
def testRefCountingOfReferredObjectAfterDeletingReferrer(self): '''Deleting the object referring to other object should decrease the reference count of the referee.''' d = Derived() o = ObjectType() refcount = getrefcount(o) d.objectTypeField = o self.assertEqual(getrefcount(o), refcount + 1) del d self.assertEqual(getrefcount(o), refcount)
def testRefCountingAccessingObjectTypeField(self): '''Accessing a object type field should respect the reference counting rules.''' d = Derived() # attributing object to instance's field should increase its reference count o1 = ObjectType() refcount1 = getrefcount(o1) d.objectTypeField = o1 self.assertEqual(d.objectTypeField, o1) self.assertEqual(getrefcount(d.objectTypeField), refcount1 + 1) # attributing a new object to instance's field should decrease the previous object's reference count o2 = ObjectType() refcount2 = getrefcount(o2) d.objectTypeField = o2 self.assertEqual(d.objectTypeField, o2) self.assertEqual(getrefcount(o1), refcount1) self.assertEqual(getrefcount(d.objectTypeField), refcount2 + 1)
def testMethodWithAllParamers(self): '''Method with all parameters: Time.somethingCompletelyDifferent( int h, int m, ImplicitConv ic = ImplicitConv::CtorThree, ObjectType* type = 0 ); ''' time = Time() obj = ObjectType() result = time.somethingCompletelyDifferent(1, 2, ImplicitConv(2), obj) self.assertEqual(result, Time.FourArgs)
def testClassDecref(self): # Bug was that class PyTypeObject wasn't decrefed when instance died before = sys.getrefcount(ObjectType) for i in range(1000): obj = ObjectType() shiboken.delete(obj) after = sys.getrefcount(ObjectType) self.assertLess(abs(before - after), 5)
def testBasic(self): '''Allowing threads and calling virtuals from C++''' number = 10 objs = [Producer() for x in range(number)] thread = Collector(objs) thread.start() evaluated = ObjectType.processEvent(objs, Event(Event.BASIC_EVENT)) thread.join() producer_data = [x.data for x in objs] self.assertEqual(evaluated, number) self.assertEqual(producer_data, thread.data)
def testOwnershipTransference(self): '''Ownership transference from Python to C++ and back again.''' o1 = ObjectType() o1.setObjectName('object1') o1_refcnt = sys.getrefcount(o1) o2 = ObjectType() o2.setObjectName('object2') o2_refcnt = sys.getrefcount(o2) bb = BlackBox() o1_ticket = bb.keepObjectType(o1) o2_ticket = bb.keepObjectType(o2) self.assertEqual(set(bb.objects()), set([o1, o2])) self.assertEqual(str(o1.objectName()), 'object1') self.assertEqual(str(o2.objectName()), 'object2') self.assertEqual(sys.getrefcount(o1), o1_refcnt + 1) # PySide give +1 ref to object with c++ ownership self.assertEqual(sys.getrefcount(o2), o2_refcnt + 1) o2 = bb.retrieveObjectType(o2_ticket) self.assertEqual(sys.getrefcount(o2), o2_refcnt) del bb self.assertRaises(RuntimeError, o1.objectName) self.assertEqual(str(o2.objectName()), 'object2') self.assertEqual(sys.getrefcount(o2), o2_refcnt)
def testAccessingObjectTypeField(self): '''Reads and writes a object type (in this case an 'ObjectType') field.''' d = Derived() # attribution old_value = d.objectTypeField new_value = ObjectType() d.objectTypeField = new_value self.assertEqual(d.objectTypeField, new_value) self.assertNotEqual(d.objectTypeField, old_value) # attribution with a convertible type value = None d.objectTypeField = value self.assertEqual(d.objectTypeField, value) # attribution with invalid type self.assertRaises(TypeError, lambda : setattr(d, 'objectTypeField', 123))
def testImplicitConversions(self): '''Test if overloaded function call decisor takes implicit conversions into account.''' ic = ImplicitConv.implicitConvCommon(ImplicitConv()) self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorNone) ic = ImplicitConv.implicitConvCommon(3) self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorOne) self.assertEqual(ic.objId(), 3) ic = ImplicitConv.implicitConvCommon(ImplicitConv.CtorThree) self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorThree) obj = ObjectType() ic = ImplicitConv.implicitConvCommon(obj) self.assertEqual(ic.ctorEnum(), ImplicitConv.CtorObjectTypeReference) ic = ImplicitConv.implicitConvCommon(42.42) self.assertEqual(ic.value(), 42.42)
def testInvalidateChild(self): '''Invalidating method call should remove child from the care of a parent if it has one.''' parent = ObjectType() child1 = ObjectType(parent) child1.setObjectName('child1') child2 = ObjectType.create() child2.setParent(parent) child2.setObjectName('child2') self.assertEqual(parent.children(), [child1, child2]) bbox = BlackBox() # This method steals ownership from Python to C++. bbox.keepObjectType(child1) self.assertEqual(parent.children(), [child2]) bbox.keepObjectType(child2) self.assertEqual(parent.children(), []) del parent self.assertEqual(child1.objectName(), 'child1') self.assertRaises(RuntimeError, child2.objectName)
def testObjectTypeSetObjectNameWithStrVariable(self): '''ObjectType.setObjectName with Str variable as argument.''' s = Str('object name') o = ObjectType() o.setObjectName(s) self.assertEqual(str(o.objectName()), str(s))
def __init__(self): ObjectType.__init__(self) self.data = None self.read = False
def testObjectTypeSetObjectNameWithStrInstantiation(self): '''ObjectType.setObjectName with Str instantiation as argument.''' s = 'object name' o = ObjectType() o.setObjectName(Str(s)) self.assertEqual(str(o.objectName()), s)
def testObjectTypeSetObjectNameWithPythonString(self): '''ObjectType.setObjectName with Python string as argument.''' o = ObjectType() o.setObjectName('object name') self.assertEqual(str(o.objectName()), 'object name')
def __init__(self): ObjectType.__init__(self) self.type_of_last_event = None self.last_event = None
def testProtectedObjectTypeProperty(self): '''Writes and reads a protected object type property.''' obj = ObjectType() self.obj.protectedObjectTypeProperty = obj self.assertEqual(self.obj.protectedObjectTypeProperty, obj)
def testParentFromCpp(self): o = ObjectType() self.assertEqual(sys.getrefcount(o), 2) o.getCppParent().setObjectName('parent') self.assertEqual(sys.getrefcount(o), 3) o.getCppParent().setObjectName('parent') self.assertEqual(sys.getrefcount(o), 3) o.getCppParent().setObjectName('parent') self.assertEqual(sys.getrefcount(o), 3) o.getCppParent().setObjectName('parent') self.assertEqual(sys.getrefcount(o), 3) o.getCppParent().setObjectName('parent') self.assertEqual(sys.getrefcount(o), 3) o.destroyCppParent() self.assertEqual(sys.getrefcount(o), 2)
def testNextInFocusChainCycle(self): parent = ObjectType() child = ObjectType(parent) next_focus = child.nextInFocusChain() shiboken.invalidate(parent)
def __init__(self): ObjectType.__init__(self) VirtualMethods.__init__(self) self.event_processed = False
def __init__(self): ObjectType.__init__(self) VirtualMethods.__init__(self)
def __init__(self): ObjectType.__init__(self) self.called = False
def testLShiftWithExpectedType(self): '''Collector << ObjectType # libsample << operator''' collector = Collector() obj = ObjectType() collector << obj self.assertEqual(collector.items()[0], obj.identifier())
def __init__(self): ObjectType.__init__(self)
def testMoreArguments(self): o = ObjectType() o.setObjectSplittedName("", prefix="pys", suffix="ide") self.assertEqual(o.objectName(), "pyside") o.setObjectSplittedName("", suffix="ide", prefix="pys") self.assertEqual(o.objectName(), "pyside") o.setObjectNameWithSize(name="pyside", size=6) self.assertEqual(o.objectName(), "pyside") o.setObjectNameWithSize(size=6, name="pyside") self.assertEqual(o.objectName(), "pyside")
def testWithInt(self): '''Overload with Enums and ints with default value''' o = ObjectType() self.assertEqual(o.callWithEnum('', Event.ANY_EVENT, 9), 81) self.assertEqual(o.callWithEnum('', 9), 9)
def testNullOverload(self): o = ObjectType() o.setObject(None) self.assertEqual(o.callId(), 0) o.setNullObject(None) self.assertEqual(o.callId(), 1)