# HG changeset patch # Parent 023e23a400ece6597b22a53a1e0d5abadad2fb2e diff -r 023e23a400ec Lib/test/test_functools.py --- a/Lib/test/test_functools.py Thu Jul 19 19:39:58 2012 +0300 +++ b/Lib/test/test_functools.py Thu Jul 19 21:59:13 2012 +0300 @@ -756,6 +756,61 @@ self.assertEqual(square.cache_info().hits, 4) self.assertEqual(square.cache_info().misses, 4) +class TestUnbind(unittest.TestCase): + unbind = lambda self, obj: functools.unbind(obj) + + class _C: + @classmethod + def cm(klass, dummy): + if dummy == 42: + return klass.__qualname__ + return '' + + @staticmethod + def sm(): + pass + + def m(self): + pass + + def test_bound(self): + self.assertIs(list.append, self.unbind([].append)) + self.assertIs(list.__add__, self.unbind([].__add__)) + + inst = TestUnbind._C() + #self.assertIs(TestUnbind._C.cm, self.unbind(inst.cm)) + self.assertEqual('TestUnbind._C', self.unbind(inst.cm)(42)) # see below + self.assertIs(TestUnbind._C.sm, self.unbind(inst.sm)) + self.assertIs(TestUnbind._C.m, self.unbind(inst.m)) + # dict.fromkeys is a classmethod + # XXX id(dict.fromkeys) is not a constant, so we can't use assertIs. + # we'll just make sure the returned function behaves like fromkeys. + self.assertEqual({1 : 2}, self.unbind({3 : 4}.fromkeys)([1], 2)) + + def test_unbound(self): + self.assertIs(len, self.unbind(len)) + self.assertIs(list.append, self.unbind(list.append)) + self.assertIs(list.__add__, self.unbind(list.__add__)) + + #self.assertIs(TestUnbind._C.cm, self.unbind(TestUnbind._C.cm)) + self.assertEqual('TestUnbind._C', self.unbind(TestUnbind._C.cm)(42)) + self.assertIs(TestUnbind._C.sm, self.unbind(TestUnbind._C.sm)) + self.assertIs(TestUnbind._C.m, self.unbind(TestUnbind._C.m)) + # see above + self.assertEqual({1 : 2}, self.unbind(dict.fromkeys)([1], 2)) + + def test_idempotence(self): + self.assertIs(TestUnbind.test_idempotence, + self.unbind(self.unbind(TestUnbind.test_idempotence)) + ) + + def test_nonfuncs(self): + with self.assertRaises(TypeError): + self.unbind({}) + with self.assertRaises(TypeError): + self.unbind(functools) + + def test_main(verbose=None): test_classes = ( TestPartial, @@ -767,6 +822,7 @@ TestWraps, TestReduce, TestLRU, + TestUnbind ) support.run_unittest(*test_classes)