File size: 1,519 Bytes
065fee7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from __future__ import print_function
import unittest
import copy
import wrapt
class CustomObjectProxy(wrapt.ObjectProxy):
def __copy__(self):
return CustomObjectProxy(copy.copy(self.__wrapped__))
def __deepcopy__(self, memo):
return CustomObjectProxy(copy.deepcopy(self.__wrapped__, memo))
class TestObjectCopy(unittest.TestCase):
def test_copy(self):
proxy1 = wrapt.ObjectProxy([1])
with self.assertRaises(NotImplementedError) as context:
proxy2 = copy.copy(proxy1)
self.assertTrue(str(context.exception) ==
'object proxy must define __copy__()')
def test_deepcopy(self):
proxy1 = wrapt.ObjectProxy([1])
with self.assertRaises(NotImplementedError) as context:
proxy2 = copy.deepcopy(proxy1)
self.assertTrue(str(context.exception) ==
'object proxy must define __deepcopy__()')
def test_copy_proxy(self):
proxy1 = CustomObjectProxy([1])
proxy2 = copy.copy(proxy1)
self.assertTrue(type(proxy1) == type(proxy2))
self.assertEqual(proxy1, proxy2)
self.assertEqual(proxy1.__wrapped__, proxy2.__wrapped__)
def test_deepcopy_proxy(self):
proxy1 = CustomObjectProxy([1])
proxy2 = copy.deepcopy(proxy1)
self.assertTrue(type(proxy1) == type(proxy2))
self.assertEqual(proxy1, proxy2)
self.assertEqual(proxy1.__wrapped__, proxy2.__wrapped__)
if __name__ == '__main__':
unittest.main()
|