summaryrefslogtreecommitdiff
path: root/mock/tests/testpatch.py
diff options
context:
space:
mode:
authorSenthil Kumaran <senthil@uthcode.com>2016-01-08 23:43:29 -0800
committerRobert Collins <rbtcollins@hp.com>2016-03-23 18:55:24 +1300
commit4deb2c11bb98d0d1dfb472b17107879f4e506df5 (patch)
treebba432df72e8f9db0a858f3f05471cba54f71380 /mock/tests/testpatch.py
parentf40c660f75f6a25b6ff78dc4b71086cf17805f5d (diff)
downloadmock-4deb2c11bb98d0d1dfb472b17107879f4e506df5.tar.gz
Issue #22138: Fix mock.patch behavior when patching descriptors. Restore
original values after patching. Patch contributed by Sean McCully.
Diffstat (limited to 'mock/tests/testpatch.py')
-rw-r--r--mock/tests/testpatch.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/mock/tests/testpatch.py b/mock/tests/testpatch.py
index 761ce99..32a6c27 100644
--- a/mock/tests/testpatch.py
+++ b/mock/tests/testpatch.py
@@ -1852,5 +1852,32 @@ class PatchTest(unittest.TestCase):
self.assertEqual(stopped, ["three", "two", "one"])
+ def test_special_attrs(self):
+ def foo(x=0):
+ """TEST"""
+ return x
+ with patch.object(foo, '__defaults__', (1, )):
+ self.assertEqual(foo(), 1)
+ self.assertEqual(foo(), 0)
+
+ with patch.object(foo, '__doc__', "FUN"):
+ self.assertEqual(foo.__doc__, "FUN")
+ self.assertEqual(foo.__doc__, "TEST")
+
+ with patch.object(foo, '__module__', "testpatch2"):
+ self.assertEqual(foo.__module__, "testpatch2")
+ self.assertEqual(foo.__module__, __name__)
+
+ if hasattr(self.test_special_attrs, '__annotations__'):
+ with patch.object(foo, '__annotations__', dict([('s', 1, )])):
+ self.assertEqual(foo.__annotations__, dict([('s', 1, )]))
+ self.assertEqual(foo.__annotations__, dict())
+
+ if hasattr(self.test_special_attrs, '__kwdefaults__'):
+ foo = eval("lambda *a, x=0: x")
+ with patch.object(foo, '__kwdefaults__', dict([('x', 1, )])):
+ self.assertEqual(foo(), 1)
+ self.assertEqual(foo(), 0)
+
if __name__ == '__main__':
unittest.main()