aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2011-09-23 18:11:27 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:18:17 -0300
commit2d47b4ec2e4f8119b11cdabe0e56ca55152c120d (patch)
treea3d9a9568e4526575efb2420cabdfb0763c66ea2 /tests
parent3534d8d0a7dc65f6fb8110abbaf570b70d729584 (diff)
Fixed duck punch unti test to work with Python3.
Diffstat (limited to 'tests')
-rw-r--r--tests/samplebinding/duck_punching_test.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/tests/samplebinding/duck_punching_test.py b/tests/samplebinding/duck_punching_test.py
index 2870b8c4..b48693f3 100644
--- a/tests/samplebinding/duck_punching_test.py
+++ b/tests/samplebinding/duck_punching_test.py
@@ -31,6 +31,12 @@ import unittest
from sample import VirtualMethods, SimpleFile, Point
+def MethodTypeCompat(func, instance):
+ if IS_PY3K:
+ return types.MethodType(func, instance)
+ else:
+ return types.MethodType(func, instance, type(instance))
+
class Duck(VirtualMethods):
def __init__(self):
VirtualMethods.__init__(self)
@@ -60,7 +66,7 @@ class DuckPunchingTest(unittest.TestCase):
def myVirtualMethod0(obj, pt, val, cpx, b):
self.duck_method_called = True
return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier
- vm.virtualMethod0 = types.MethodType(myVirtualMethod0, vm, VirtualMethods)
+ vm.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, vm)
result1 = vm.callVirtualMethod0(pt, val, cpx, b)
self.assert_(self.duck_method_called)
@@ -89,7 +95,7 @@ class DuckPunchingTest(unittest.TestCase):
def myVirtualMethod0(obj, pt, val, cpx, b):
self.duck_method_called = True
return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b) * self.multiplier
- duck.virtualMethod0 = types.MethodType(myVirtualMethod0, duck, Duck)
+ duck.virtualMethod0 = MethodTypeCompat(myVirtualMethod0, duck)
result1 = duck.callVirtualMethod0(pt, val, cpx, b)
self.assert_(self.duck_method_called)
@@ -114,7 +120,7 @@ class DuckPunchingTest(unittest.TestCase):
def myExists(obj):
self.duck_method_called = True
return False
- simplefile.exists = types.MethodType(myExists, simplefile, SimpleFile)
+ simplefile.exists = MethodTypeCompat(myExists, simplefile)
# Static 'exists' was overridden by the monkey patch, which accepts 0 arguments
self.assertRaises(TypeError, simplefile.exists, 'sbrubbles')
@@ -138,7 +144,7 @@ class DuckPunchingTest(unittest.TestCase):
def myExists(obj):
self.duck_method_called = True
return False
- monkey.exists = types.MethodType(myExists, monkey, SimpleFile)
+ monkey.exists = MethodTypeCompat(myExists, monkey)
# Static 'exists' was overridden by the monkey patch, which accepts 0 arguments
self.assertRaises(TypeError, monkey.exists, 'sbrubbles')
@@ -153,7 +159,7 @@ class DuckPunchingTest(unittest.TestCase):
self.call_counter += 1
return VirtualMethods.virtualMethod0(obj, pt, val, cpx, b)
vm = VirtualMethods()
- vm.virtualMethod0 = types.MethodType(myVirtualMethod0, 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)