summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_functional.py154
1 files changed, 154 insertions, 0 deletions
diff --git a/Lib/test/test_functional.py b/Lib/test/test_functional.py
new file mode 100644
index 0000000..db3a289
--- /dev/null
+++ b/Lib/test/test_functional.py
@@ -0,0 +1,154 @@
+import functional
+import unittest
+from test import test_support
+
+@staticmethod
+def PythonPartial(func, *args, **keywords):
+ 'Pure Python approximation of partial()'
+ def newfunc(*fargs, **fkeywords):
+ newkeywords = keywords.copy()
+ newkeywords.update(fkeywords)
+ return func(*(args + fargs), **newkeywords)
+ newfunc.func = func
+ newfunc.args = args
+ newfunc.keywords = keywords
+ return newfunc
+
+def capture(*args, **kw):
+ """capture all positional and keyword arguments"""
+ return args, kw
+
+class TestPartial(unittest.TestCase):
+
+ thetype = functional.partial
+
+ def test_basic_examples(self):
+ p = self.thetype(capture, 1, 2, a=10, b=20)
+ self.assertEqual(p(3, 4, b=30, c=40),
+ ((1, 2, 3, 4), dict(a=10, b=30, c=40)))
+ p = self.thetype(map, lambda x: x*10)
+ self.assertEqual(p([1,2,3,4]), [10, 20, 30, 40])
+
+ def test_attributes(self):
+ p = self.thetype(capture, 1, 2, a=10, b=20)
+ # attributes should be readable
+ self.assertEqual(p.func, capture)
+ self.assertEqual(p.args, (1, 2))
+ self.assertEqual(p.keywords, dict(a=10, b=20))
+ # attributes should not be writable
+ if not isinstance(self.thetype, type):
+ return
+ self.assertRaises(TypeError, setattr, p, 'func', map)
+ self.assertRaises(TypeError, setattr, p, 'args', (1, 2))
+ self.assertRaises(TypeError, setattr, p, 'keywords', dict(a=1, b=2))
+
+ def test_argument_checking(self):
+ self.assertRaises(TypeError, self.thetype) # need at least a func arg
+ try:
+ self.thetype(2)()
+ except TypeError:
+ pass
+ else:
+ self.fail('First arg not checked for callability')
+
+ def test_protection_of_callers_dict_argument(self):
+ # a caller's dictionary should not be altered by partial
+ def func(a=10, b=20):
+ return a
+ d = {'a':3}
+ p = self.thetype(func, a=5)
+ self.assertEqual(p(**d), 3)
+ self.assertEqual(d, {'a':3})
+ p(b=7)
+ self.assertEqual(d, {'a':3})
+
+ def test_arg_combinations(self):
+ # exercise special code paths for zero args in either partial
+ # object or the caller
+ p = self.thetype(capture)
+ self.assertEqual(p(), ((), {}))
+ self.assertEqual(p(1,2), ((1,2), {}))
+ p = self.thetype(capture, 1, 2)
+ self.assertEqual(p(), ((1,2), {}))
+ self.assertEqual(p(3,4), ((1,2,3,4), {}))
+
+ def test_kw_combinations(self):
+ # exercise special code paths for no keyword args in
+ # either the partial object or the caller
+ p = self.thetype(capture)
+ self.assertEqual(p(), ((), {}))
+ self.assertEqual(p(a=1), ((), {'a':1}))
+ p = self.thetype(capture, a=1)
+ self.assertEqual(p(), ((), {'a':1}))
+ self.assertEqual(p(b=2), ((), {'a':1, 'b':2}))
+ # keyword args in the call override those in the partial object
+ self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2}))
+
+ def test_positional(self):
+ # make sure positional arguments are captured correctly
+ for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:
+ p = self.thetype(capture, *args)
+ expected = args + ('x',)
+ got, empty = p('x')
+ self.failUnless(expected == got and empty == {})
+
+ def test_keyword(self):
+ # make sure keyword arguments are captured correctly
+ for a in ['a', 0, None, 3.5]:
+ p = self.thetype(capture, a=a)
+ expected = {'a':a,'x':None}
+ empty, got = p(x=None)
+ self.failUnless(expected == got and empty == ())
+
+ def test_no_side_effects(self):
+ # make sure there are no side effects that affect subsequent calls
+ p = self.thetype(capture, 0, a=1)
+ args1, kw1 = p(1, b=2)
+ self.failUnless(args1 == (0,1) and kw1 == {'a':1,'b':2})
+ args2, kw2 = p()
+ self.failUnless(args2 == (0,) and kw2 == {'a':1})
+
+ def test_error_propagation(self):
+ def f(x, y):
+ x / y
+ self.assertRaises(ZeroDivisionError, self.thetype(f, 1, 0))
+ self.assertRaises(ZeroDivisionError, self.thetype(f, 1), 0)
+ self.assertRaises(ZeroDivisionError, self.thetype(f), 1, 0)
+ self.assertRaises(ZeroDivisionError, self.thetype(f, y=0), 1)
+
+
+class PartialSubclass(functional.partial):
+ pass
+
+class TestPartialSubclass(TestPartial):
+
+ thetype = PartialSubclass
+
+
+class TestPythonPartial(TestPartial):
+
+ thetype = PythonPartial
+
+
+
+def test_main(verbose=None):
+ import sys
+ test_classes = (
+ TestPartial,
+ TestPartialSubclass,
+ TestPythonPartial,
+ )
+ test_support.run_unittest(*test_classes)
+
+ # verify reference counting
+ if verbose and hasattr(sys, "gettotalrefcount"):
+ import gc
+ counts = [None] * 5
+ for i in xrange(len(counts)):
+ test_support.run_unittest(*test_classes)
+ gc.collect()
+ counts[i] = sys.gettotalrefcount()
+ print counts
+
+if __name__ == '__main__':
+ test_main(verbose=True)
True when finding the empty string and the indexes are completely out of range. | * | | Issue #24284: The startswith and endswith methods of the str class no longerSerhiy Storchaka2015-05-311-3/+3 | | | | | | | | | | | | | | | | | | | | return True when finding the empty string and the indexes are completely out of range. * | | | Merge from 3.5.Eric Snow2015-05-301-12/+14 |\ \ \ \ | |/ / / | * | | Issue #16991: Fix a few leaks and other memory-related concerns in OrderedDict.Eric Snow2015-05-301-12/+14 | | | | * | | | Merge from 3.5.Eric Snow2015-05-301-19/+43 |\ \ \ \ | |/ / / | * | | Issue #16991: Do not return None from OrderedDict.__reversed__.Eric Snow2015-05-301-3/+0 | | | | | * | | Issue #16991: Properly handle return values in several places.Eric Snow2015-05-301-16/+43 | | | | | * | | Reverting my previous commit.Yury Selivanov2015-05-301-104/+41 |/ / / | | | | | | | | | Something went horribly wrong when I was doing `hg rebase`. * | | Issue #24115: Update uses of PyObject_IsTrue(), PyObject_Not(),Serhiy Storchaka2015-05-304-19/+46 |\ \ \ | | | | | | | | | | | | | | | | PyObject_IsInstance(), PyObject_RichCompareBool() and _PyDict_Contains() to check for and handle errors correctly. | * \ \ Issue #24115: Update uses of PyObject_IsTrue(), PyObject_Not(),Serhiy Storchaka2015-05-304-19/+46 | |\ \ \ | | |/ / | | | | | | | | | | | | PyObject_IsInstance(), PyObject_RichCompareBool() and _PyDict_Contains() to check for and handle errors correctly. | | * | Issue #24115: Update uses of PyObject_IsTrue(), PyObject_Not(),Serhiy Storchaka2015-05-30