1717from python_toolbox import caching
1818
1919
20+ class EqualByIdentity (object ):
21+ def __eq__ (self , other ):
22+ return self is other
23+
24+
2025def 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
4550def 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
8287def 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
106109def 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
160162def 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
0 commit comments