MCPcopy Index your code
hub / github.com/python/cpython / test_replace_method

Method test_replace_method

Lib/test/test_copy.py:950–971  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

948 self.assertRaises(TypeError, copy.replace, A())
949
950 def test_replace_method(self):
951 class A:
952 def __new__(cls, x, y=0):
953 self = object.__new__(cls)
954 self.x = x
955 self.y = y
956 return self
957
958 def __init__(self, *args, **kwargs):
959 self.z = self.x + self.y
960
961 def __replace__(self, **changes):
962 x = changes.get('x', self.x)
963 y = changes.get('y', self.y)
964 return type(self)(x, y)
965
966 attrs = attrgetter('x', 'y', 'z')
967 a = A(11, 22)
968 self.assertEqual(attrs(copy.replace(a)), (11, 22, 33))
969 self.assertEqual(attrs(copy.replace(a, x=1)), (1, 22, 23))
970 self.assertEqual(attrs(copy.replace(a, y=2)), (11, 2, 13))
971 self.assertEqual(attrs(copy.replace(a, x=1, y=2)), (1, 2, 3))
972
973 def test_namedtuple(self):
974 from collections import namedtuple

Callers

nothing calls this directly

Calls 4

attrgetterClass · 0.90
AClass · 0.70
assertEqualMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected