Skip to content

Commit 8d3e6ed

Browse files
committed
-
1 parent 98adebb commit 8d3e6ed

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

source_py2/test_python_toolbox/test_monkeypatching_tools.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,30 @@
1717
from python_toolbox import caching
1818

1919

20+
class EqualByIdentity(object):
21+
def __eq__(self, other):
22+
return self is other
23+
24+
2025
def test():
2126
'''Test basic workings of `monkeypatch_method`.'''
2227

23-
class A(object):
28+
class A(EqualByIdentity):
2429
pass
2530

2631
@monkeypatching_tools.monkeypatch_method(A)
2732
def meow(a):
28-
return 1
33+
return (a, 1)
2934

3035
a = A()
3136

32-
assert a.meow() == meow(a) == 1
37+
assert a.meow() == meow(a) == (a, 1)
3338

3439
@monkeypatching_tools.monkeypatch_method(A, 'roar')
3540
def woof(a):
36-
return 2
41+
return (a, 2)
3742

38-
assert a.roar() == woof(a) == 2
43+
assert a.roar() == woof(a) == (a, 2)
3944

4045
assert not hasattr(a, 'woof')
4146

@@ -44,7 +49,7 @@ def woof(a):
4449

4550
def test_monkeypatch_cached_property():
4651

47-
class A(object):
52+
class A(EqualByIdentity):
4853
pass
4954

5055
@monkeypatching_tools.monkeypatch_method(A)
@@ -81,31 +86,29 @@ def f(): pass
8186

8287
def test_monkeypatch_staticmethod():
8388

84-
class A(object):
89+
class A(EqualByIdentity):
8590
@staticmethod
8691
def my_static_method(x):
8792
raise 'Flow should never reach here.'
8893

8994
@monkeypatching_tools.monkeypatch_method(A)
9095
@staticmethod
9196
def my_static_method(x):
92-
return 'Success'
97+
return (x, 'Success')
9398

9499
assert isinstance(cute_inspect.getattr_static(A, 'my_static_method'),
95100
staticmethod)
96101
assert isinstance(A.my_static_method, types.FunctionType)
97102

98-
assert A.my_static_method(3) == A.my_static_method('Whatever') == \
99-
'Success'
103+
assert A.my_static_method(3) == A.my_static_method(3) == (3, 'Success')
100104

101105
a0 = A()
102-
assert a0.my_static_method(3) == a0.my_static_method('Whatever') == \
103-
'Success'
106+
assert a0.my_static_method(3) == a0.my_static_method(3) == (3, 'Success')
104107

105108

106109
def test_monkeypatch_classmethod():
107110

108-
class A(object):
111+
class A(EqualByIdentity):
109112
@classmethod
110113
def my_class_method(cls):
111114
raise 'Flow should never reach here.'
@@ -132,11 +135,10 @@ def test_monkeypatch_classmethod_subclass():
132135
133136
This is useful in Django, that uses its own `classmethod` subclass.
134137
'''
135-
136138
class FunkyClassMethod(classmethod):
137139
is_funky = True
138140

139-
class A(object):
141+
class A(EqualByIdentity):
140142
@FunkyClassMethod
141143
def my_funky_class_method(cls):
142144
raise 'Flow should never reach here.'
@@ -159,9 +161,9 @@ def my_funky_class_method(cls):
159161

160162
def test_directly_on_object():
161163

162-
class A(object):
164+
class A(EqualByIdentity):
163165
def woof(self):
164-
return 'woof'
166+
return (self, 'woof')
165167

166168
a0 = A()
167169
a1 = A()
@@ -177,12 +179,12 @@ def woof(a):
177179
assert a0.meow() == 'not meow'
178180
assert a0.woof() == 'not woof'
179181

180-
assert a1.woof() == 'woof'
182+
assert a1.woof() == (a1, 'woof')
181183

182184
with cute_testing.RaiseAssertor(AttributeError):
183185
A.meow()
184186
with cute_testing.RaiseAssertor(AttributeError):
185187
a1.meow()
186188

187-
assert A.woof(a0) == 'woof'
189+
assert A.woof(a0) == (a0, 'woof')
188190

source_py3/test_python_toolbox/test_monkeypatching_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def my_funky_class_method(cls):
161161

162162
def test_directly_on_object():
163163

164-
class A:
164+
class A(EqualByIdentity):
165165
def woof(self):
166166
return (self, 'woof')
167167

0 commit comments

Comments
 (0)