def testReturnNewCopy(self): '''Point returns a copy of itself.''' pt1 = Point(1.1, 2.3) pt2 = pt1.copy() self.assertEqual(pt1, pt2) pt2 += pt1 self.assert_(not pt1 == pt2)
def testReturnNewCopy(self): '''Point returns a copy of itself.''' pt1 = Point(1.1, 2.3) pt2 = pt1.copy() self.assertEqual(pt1, pt2) pt2 += pt1 self.assertFalse(pt1 == pt2)
def testArgRemoval2(self): '''Tests argument removal modifications on Modifications.argRemoval2.''' # void [-> PyObject*] argRemoval2(int, bool, Point = Point(1, 2) [removed], Point = Point(3, 4) [removed], int = 333) # code-injection: returns tuple with received parameters plus removed ones a0, a1, a2 = 1, True, 2 self.assertEqual(self.mods.argRemoval2(a0, a1), (a0, a1, Point(1, 2), Point(3, 4), 333)) self.assertEqual(self.mods.argRemoval2(a0, a1, a2), (a0, a1, Point(1, 2), Point(3, 4), a2))
def testDrawTextPointAndStr(self): overload = Overload() self.assertEqual(overload.drawText(Point(), Str()), Overload.Function0) self.assertEqual(overload.drawText(Point(), ''), Overload.Function0) self.assertEqual(overload.drawText(PointF(), Str()), Overload.Function1) self.assertEqual(overload.drawText(PointF(), ''), Overload.Function1)
def testOwnershipTransference(self): '''Ownership transference from Python to C++ and back again.''' p1 = Point(10, 20) bb = BlackBox() p1_ticket = bb.keepPoint(p1) self.assertRaises(RuntimeError, p1.x) p1_ret = bb.retrievePoint(p1_ticket) self.assertEqual(p1_ret, Point(10, 20))
def testReturnConstPointer(self): '''Point returns a const pointer for itself.''' pt1 = Point(5.0, 2.3) refcount1 = sys.getrefcount(pt1) pt2 = pt1.getSelf() self.assertEqual(pt1, pt2) self.assertEqual(sys.getrefcount(pt1), refcount1 + 1) self.assertEqual(sys.getrefcount(pt1), sys.getrefcount(pt2))
def testNotEqualOperator(self): '''Test Point class != operator.''' pt1 = Point(5.0, 2.3) pt2 = Point(5.0, 2.3) # This test no longer makes sense because we always supply default `==`, `!=`. #self.assertRaises(NotImplementedError, pt1.__ne__, pt2) # Since we use the default identity comparison, this results in `!=` . self.assertTrue(pt1 != pt2)
def testArgRemoval4(self): '''Tests argument removal modifications on Modifications.argRemoval4.''' # void [-> PyObject*] argRemoval4(int, Point [removed, new val = Point(6, 9)], bool, Point = Point(3, 4) [removed], int = 333) # code-injection: returns tuple with received parameters plus removed ones a0, a1, a2 = 1, True, 2 self.assertRaises(TypeError, self.mods.argRemoval4, a0) self.assertEqual(self.mods.argRemoval4(a0, a1), (a0, Point(6, 9), a1, Point(3, 4), 333)) self.assertEqual(self.mods.argRemoval4(a0, a1, a2), (a0, Point(6, 9), a1, Point(3, 4), a2))
def testProtectedValueTypePointerProperty(self): '''Writes and reads a protected value type pointer property.''' pt1 = Point(12, 34) pt2 = Point(12, 34) self.obj.protectedValueTypePointerProperty = pt1 self.assertEqual(self.obj.protectedValueTypePointerProperty, pt1) self.assertEqual(self.obj.protectedValueTypePointerProperty, pt2) self.assertTrue(self.obj.protectedValueTypePointerProperty is pt1) self.assertFalse(self.obj.protectedValueTypePointerProperty is pt2)
def testValueTypeReferenceAndValue(self): '''In C++ we have "function(const ValueType&, double)", in Python we add "function(ValueType)".''' point = Point(10, 20) multiplier = 4.0 control = (point.x() + point.y()) * multiplier self.assertEqual(SampleNamespace.passReferenceToValueType(point, multiplier), control) control = point.x() + point.y() self.assertEqual(SampleNamespace.passReferenceToValueType(point), control)
def testEqualOperator(self): '''Test Point class == operator.''' pt1 = Point(5.0, 2.3) pt2 = Point(5.0, 2.3) pt3 = Point(0.5, 3.2) self.assertTrue(pt1 == pt1) self.assertTrue(pt1 == pt2) self.assertFalse(pt1 == pt3) self.assertFalse(pt1 == object())
def testArgRemoval5(self): '''Tests argument removal modifications on Modifications.argRemoval5.''' # void [-> PyObject*] argRemoval5(int [removed, new val = 100], bool, # Point = Point(1, 2) [removed], # Point = Point(3, 4) [removed], int = 333) # code-injection: returns tuple with received parameters plus removed ones a0, a1, a2 = True, 2, True self.assertEqual(self.mods.argRemoval5(a0), (100, a0, Point(1, 2), Point(3, 4), 333)) self.assertEqual(self.mods.argRemoval5(a0, a1), (100, a0, Point(1, 2), Point(3, 4), a1)) # void [-> PyObject*] argRemoval5(int [removed, new val = 200], bool, int, bool) # code-injection: returns tuple with received parameters plus removed ones self.assertEqual(self.mods.argRemoval5(a0, a1, a2), (200, a0, a1, a2))
def testMonkeyPatchOnVirtualMethod(self): '''Injects new 'virtualMethod0' on a VirtualMethods instance and makes C++ call it.''' vm = VirtualMethods() pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True result1 = vm.virtualMethod0(pt, val, cpx, b) result2 = vm.callVirtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b)) def myVirtualMethod0(obj, pt, val, cpx, b): self.duck_method_called = True return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier vm.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, vm) result1 = vm.callVirtualMethod0(pt, val, cpx, b) self.assertTrue(self.duck_method_called) result2 = vm.virtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual( result1, VirtualMethods.virtualMethod0(vm, pt, val, cpx, b) * self.multiplier) # This is done to decrease the refcount of the vm object # allowing the object wrapper to be deleted before the # BindingManager. This is useful when compiling Shiboken # for debug, since the BindingManager destructor has an # assert that checks if the wrapper mapper is empty. vm.virtualMethod0 = None
def testConversionRuleForReturnTypeOnExtendedClass(self): x, y = 11, 2 diff = float(abs(x - y)) point = Point(x, y) em = ExtModifications() ok, res = em.differenceOfPointCoordinates(point) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, diff * em.multiplier + em.increment) ok, res = em.callDifferenceOfPointCoordinates(point) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, diff * em.multiplier + em.increment) ok, res = em.differenceOfPointCoordinates(None) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, em.increment) ok, res = em.callDifferenceOfPointCoordinates(None) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, em.increment)
def testDeepCopy(self): '''Deep copy of value types''' point = Point(3.1, 4.2) new_point = copy.deepcopy([point])[0] self.assert_(point is not new_point) self.assertEqual(point, new_point)
def testCallWithInvalidParametersSideB(self): '''Same as the previous test, but with an integer as first argument, just to complicate things for the overload method decisor.''' pt = Point() # This exception may move from a TypeError to a ValueError. self.assertRaises((TypeError, ValueError), SampleNamespace.forceDecisorSideB, 1, pt)
def testArgumentRemovalAndReturnTypeModificationWithTypesystemTemplates2( self): '''Test modifications to method signature and return value using type system templates (case 2).''' result, ok = self.mods.multiplyPointCoordsPlusValue(Point(2, 5), 4.1) self.assertEqual(type(ok), bool) self.assertEqual(type(result), float) self.assertEqual(result, 14.1)
def testTransform_ValidMatrix(self): '''Transform applies successfully.''' p = Point(3, 4) r = applyHomogeneousTransform(p, 0, 1, 0, -1, 0, 0, 0, 0, 1) self.assertTrue(type(r) is Point) self.assertEqual(r.x(), 4) self.assertEqual(r.y(), -3)
def testMonkeyPatchOnVirtualMethodWithInheritance(self): '''Injects new 'virtualMethod0' on an object that inherits from VirtualMethods and makes C++ call it.''' duck = Duck() pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True result1 = duck.virtualMethod0(pt, val, cpx, b) result2 = duck.callVirtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual(result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b)) def myVirtualMethod0(obj, pt, val, cpx, b): self.duck_method_called = True return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier duck.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, duck) result1 = duck.callVirtualMethod0(pt, val, cpx, b) self.assertTrue(self.duck_method_called) result2 = duck.virtualMethod0(pt, val, cpx, b) self.assertEqual(result1, result2) self.assertEqual( result1, VirtualMethods.virtualMethod0(duck, pt, val, cpx, b) * self.multiplier) duck.virtualMethod0 = None
def testOverloadedMethodModifications(self): '''Tests modifications to an overloaded method''' # overloaded(int, bool[removed], int, double) self.assertEqual(self.mods.overloaded(1, 2, 3.1), Modifications.Overloaded_ibid) # overloaded(int, bool, int[removed,default=321], int) self.assertEqual(self.mods.overloaded(1, True, 2), Modifications.Overloaded_ibii) # the others weren't modified self.assertEqual(self.mods.overloaded(1, True, 2, False), Modifications.Overloaded_ibib) self.assertEqual(self.mods.overloaded(1, False, 2, Point(3, 4)), Modifications.Overloaded_ibiP) self.assertRaises(TypeError, self.mods.overloaded, 1, True, Point(2, 3), Point(4, 5)) self.assertEqual(self.mods.over(1, True, Point(2, 3), Point(4, 5)), Modifications.Overloaded_ibPP)
def testCallWithInvalidParametersSideA(self): '''Call a method missing with the last argument missing. This can trigger the bug #262, which means using an argument not provided by the user.''' pt = Point() # This exception may move from a TypeError to a ValueError. self.assertRaises((TypeError, ValueError), SampleNamespace.forceDecisorSideA, pt)
def testAddedOperator(self): p = Point(0.0, 0.0) r = p - unicode('Hi') self.assertEqual(r, unicode('Hi')) # now the reverse op. r = unicode('Hi') - p self.assertEqual(r, unicode('Hi'))
def testProtectedValueTypeProperty(self): '''Writes and reads a protected value type property.''' point = Point(12, 34) self.obj.protectedValueTypeProperty = point self.assertEqual(self.obj.protectedValueTypeProperty, point) self.assertFalse(self.obj.protectedValueTypeProperty is point) pointProperty = self.obj.protectedValueTypeProperty self.assertTrue(self.obj.protectedValueTypeProperty is pointProperty)
def testForInfiniteRecursion(self): def myVirtualMethod0(obj, pt, val, cpx, b): self.call_counter += 1 return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) vm = VirtualMethods() vm.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, vm) pt, val, cpx, b = Point(1.1, 2.2), 4, complex(3.3, 4.4), True vm.virtualMethod0(pt, val, cpx, b) self.assertEqual(self.call_counter, 1)
def testSimple(self): '''Simple pickling and unpickling''' point = Point(10.2, 43.5) data = pickle.dumps(point) new_point = pickle.loads(data) self.assertEqual(point, new_point) self.assert_(point is not new_point)
def testArgumentRemovalAndReturnTypeModificationWithTypesystemTemplates1( self): '''Test modifications to method signature and return value using type system templates (case 1).''' result, ok = self.mods.pointToPair(Point(2, 5)) self.assertEqual(type(ok), bool) self.assertEqual(type(result), tuple) self.assertEqual(len(result), 2) self.assertEqual(type(result[0]), float) self.assertEqual(type(result[1]), float) self.assertEqual(result[0], 2.0) self.assertEqual(result[1], 5.0)
def testConversionOfListOfObjectsPassedAsArgument(self): '''Calls method with a Python list of wrapped objects to be converted to a C++ list.''' mult = 3 pts0 = (Point(1.0, 2.0), Point(3.3, 4.4), Point(5, 6)) pts1 = (Point(1.0, 2.0), Point(3.3, 4.4), Point(5, 6)) ListUser.multiplyPointList(pts1, mult) for pt0, pt1 in zip(pts0, pts1): self.assertEqual(pt0.x() * mult, pt1.x()) self.assertEqual(pt0.y() * mult, pt1.y())
def testProtectedValueTypePropertyWrapperRegistration(self): '''Access colocated protected value type property.''' cache_size = cacheSize() point = Point(12, 34) obj = createProtectedProperty() obj.protectedValueTypeProperty self.assertEqual(obj.protectedValueTypeProperty.copy(), obj.protectedValueTypeProperty) obj.protectedValueTypeProperty = point self.assertEqual(obj.protectedValueTypeProperty, point) self.assertFalse(obj.protectedValueTypeProperty is point) pointProperty = obj.protectedValueTypeProperty self.assertTrue(obj.protectedValueTypeProperty is pointProperty) del obj, point, pointProperty self.assertEqual(cacheSize(), cache_size)
def testAccessingValueTypeField(self): '''Reads and writes a value type (in this case a 'Point') field.''' d = Derived() self.assertEqual(type(d.valueTypeField), Point) # attribution old_value = d.valueTypeField new_value = Point(-10, 537) d.valueTypeField = new_value self.assertEqual(d.valueTypeField, new_value) #object modify d.valueTypeField.setX(10) d.valueTypeField.setY(20) self.assertEqual(d.valueTypeField.x(), 10) self.assertEqual(d.valueTypeField.y(), 20) # attribution with invalid type self.assertRaises(TypeError, lambda: setattr(d, 'valueTypeField', 123))
def testConversionRuleForReturnType(self): x, y = 11, 2 diff = float(abs(x - y)) point = Point(x, y) ok, res = self.mods.differenceOfPointCoordinates(point) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, diff) ok, res = self.mods.callDifferenceOfPointCoordinates(point) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, diff) ok, res = self.mods.differenceOfPointCoordinates(None) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, 0.0) ok, res = self.mods.callDifferenceOfPointCoordinates(None) self.assertTrue(isinstance(ok, bool)) self.assertTrue(isinstance(res, float)) self.assertEqual(res, 0.0)
def testCopy(self): point = Point(0.1, 2.4) new_point = copy.copy(point) self.assert_(point is not new_point) self.assertEqual(point, new_point)
def testPointTimesInt(self): '''sample.Point * int''' pt1 = Point(2, 7) num = 3 pt2 = Point(pt1.x() * num, pt1.y() * num) self.assertEqual(pt1 * num, pt2)
def testIntTimesPoint(self): '''int * sample.Point''' pt1 = Point(2, 7) num = 3 pt2 = Point(pt1.x() * num, pt1.y() * num) self.assertEqual(num * pt1, pt2)
def testModifiedMethod(self): pt1 = Point(0.0, 0.0) pt2 = Point(10.0, 10.0) expected = Point((pt1.x() + pt2.x()) / 2.0, (pt1.y() + pt2.y()) / 2.0) self.assertEqual(pt1.midpoint(pt2), expected)
def testUintOverflow(self): pt1 = Point(0.0, 0.0) self.assertRaises(OverflowError, pt1.setXAsUint, 840835495615213080) self.assertEqual(pt1.x(), 0.0)
def testConstructor(self): '''Test Point class constructor.''' pt = Point(5.0, 2.3) self.assertEqual(pt.x(), 5.0) self.assertEqual(pt.y(), 2.3)