@@ -21,20 +21,20 @@ def __eq__(self, other):
2121
2222
2323def test ():
24- '''Test basic workings of `monkeypatch_method `.'''
24+ '''Test basic workings of `monkeypatch `.'''
2525
2626 class A (EqualByIdentity ):
2727 pass
2828
29- @monkeypatching_tools .monkeypatch_method (A )
29+ @monkeypatching_tools .monkeypatch (A )
3030 def meow (a ):
3131 return (a , 1 )
3232
3333 a = A ()
3434
3535 assert a .meow () == meow (a ) == (a , 1 )
3636
37- @monkeypatching_tools .monkeypatch_method (A , 'roar' )
37+ @monkeypatching_tools .monkeypatch (A , 'roar' )
3838 def woof (a ):
3939 return (a , 2 )
4040
@@ -51,7 +51,7 @@ class A(EqualByIdentity):
5151 def booga (self ):
5252 return 'Old method'
5353
54- @monkeypatching_tools .monkeypatch_method (A , override_if_exists = False )
54+ @monkeypatching_tools .monkeypatch (A , override_if_exists = False )
5555 def meow (a ):
5656 return (a , 1 )
5757
@@ -60,7 +60,7 @@ def meow(a):
6060 assert a .meow () == meow (a ) == (a , 1 )
6161
6262
63- @monkeypatching_tools .monkeypatch_method (A , override_if_exists = False )
63+ @monkeypatching_tools .monkeypatch (A , override_if_exists = False )
6464 def booga ():
6565 raise RuntimeError ('Should never be called.' )
6666
@@ -75,7 +75,7 @@ def test_monkeypatch_property():
7575 class A (EqualByIdentity ):
7676 pass
7777
78- @monkeypatching_tools .monkeypatch_method (A )
78+ @monkeypatching_tools .monkeypatch (A )
7979 @property
8080 def meow (a ):
8181 return (type (a ), 'bark' )
@@ -90,7 +90,7 @@ def test_monkeypatch_cached_property():
9090 class A (EqualByIdentity ):
9191 pass
9292
93- @monkeypatching_tools .monkeypatch_method (A )
93+ @monkeypatching_tools .monkeypatch (A )
9494 @caching .CachedProperty
9595 def meow (a ):
9696 return (type (a ), uuid .uuid4 ().hex )
@@ -110,13 +110,13 @@ def test_helpful_message_when_forgetting_parentheses():
110110 '''Test user gets a helpful exception when when forgetting parentheses.'''
111111
112112 def confusedly_forget_parentheses ():
113- @monkeypatching_tools .monkeypatch_method
113+ @monkeypatching_tools .monkeypatch
114114 def f (): pass
115115
116116 with cute_testing .RaiseAssertor (
117117 TypeError ,
118118 'It seems that you forgot to add parentheses after '
119- '`@monkeypatch_method ` when decorating the `f` function.'
119+ '`@monkeypatch ` when decorating the `f` function.'
120120 ):
121121
122122 confusedly_forget_parentheses ()
@@ -129,7 +129,7 @@ class A(EqualByIdentity):
129129 def my_static_method (x ):
130130 raise 'Flow should never reach here.'
131131
132- @monkeypatching_tools .monkeypatch_method (A )
132+ @monkeypatching_tools .monkeypatch (A )
133133 @staticmethod
134134 def my_static_method (x ):
135135 return (x , 'Success' )
@@ -151,7 +151,7 @@ class A(EqualByIdentity):
151151 def my_class_method (cls ):
152152 raise 'Flow should never reach here.'
153153
154- @monkeypatching_tools .monkeypatch_method (A )
154+ @monkeypatching_tools .monkeypatch (A )
155155 @classmethod
156156 def my_class_method (cls ):
157157 return cls
@@ -169,7 +169,7 @@ def my_class_method(cls):
169169
170170def test_monkeypatch_classmethod_subclass ():
171171 '''
172- Test `monkeypatch_method ` on a subclass of `classmethod`.
172+ Test `monkeypatch ` on a subclass of `classmethod`.
173173
174174 This is useful in Django, that uses its own `classmethod` subclass.
175175 '''
@@ -181,7 +181,7 @@ class A(EqualByIdentity):
181181 def my_funky_class_method (cls ):
182182 raise 'Flow should never reach here.'
183183
184- @monkeypatching_tools .monkeypatch_method (A )
184+ @monkeypatching_tools .monkeypatch (A )
185185 @FunkyClassMethod
186186 def my_funky_class_method (cls ):
187187 return cls
@@ -206,11 +206,11 @@ def woof(self):
206206 a0 = A ()
207207 a1 = A ()
208208
209- @monkeypatching_tools .monkeypatch_method (a0 )
209+ @monkeypatching_tools .monkeypatch (a0 )
210210 def meow (a ):
211211 return 'not meow'
212212
213- @monkeypatching_tools .monkeypatch_method (a0 )
213+ @monkeypatching_tools .monkeypatch (a0 )
214214 def woof (a ):
215215 return 'not woof'
216216
@@ -226,3 +226,21 @@ def woof(a):
226226
227227 assert A .woof (a0 ) == (a0 , 'woof' )
228228
229+
230+ def test_monkeypatch_module ():
231+ module = types .ModuleType ('module' )
232+ assert not hasattr (module , 'meow' )
233+ @monkeypatching_tools .monkeypatch (module )
234+ def meow ():
235+ return 'First meow'
236+ assert module .meow () == 'First meow'
237+
238+ @monkeypatching_tools .monkeypatch (module , override_if_exists = False )
239+ def meow ():
240+ return 'Second meow'
241+ assert module .meow () == 'First meow'
242+
243+ @monkeypatching_tools .monkeypatch (module , name = 'woof' , override_if_exists = False )
244+ def meow ():
245+ return 'Third meow'
246+ assert module .woof () == 'Third meow'
0 commit comments