import functional import unittest from test import test_support def capture(*args,**kw): """capture all positional and keyword arguments""" return args,kw class PartialTestCase(unittest.TestCase): 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.partial(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.partial(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.partial(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}) class FnPartialTestCase(PartialTestCase): def setUp(self): self.partial = functional.partial # use function partial class ClassPartialTestCase(PartialTestCase): def setUp(self): self.partial = functional.Partial # use class Partial def test_main(): test_support.run_unittest(FnPartialTestCase) test_support.run_unittest(ClassPartialTestCase) if __name__ == '__main__': test_main()