Back to index

python3.2  3.2.2
Public Member Functions | Static Public Attributes
test.test_functools.TestPartial Class Reference
Inheritance diagram for test.test_functools.TestPartial:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def test_basic_examples
def test_attributes
def test_argument_checking
def test_protection_of_callers_dict_argument
def test_arg_combinations
def test_kw_combinations
def test_positional
def test_keyword
def test_no_side_effects
def test_error_propagation
def test_weakref
def test_with_bound_and_unbound_methods
def test_repr
def test_pickle

Static Public Attributes

 thetype = functools.partial

Detailed Description

Definition at line 30 of file test_functools.py.


Member Function Documentation

Definition at line 82 of file test_functools.py.

00082 
00083     def test_arg_combinations(self):
00084         # exercise special code paths for zero args in either partial
00085         # object or the caller
00086         p = self.thetype(capture)
00087         self.assertEqual(p(), ((), {}))
00088         self.assertEqual(p(1,2), ((1,2), {}))
00089         p = self.thetype(capture, 1, 2)
00090         self.assertEqual(p(), ((1,2), {}))
00091         self.assertEqual(p(3,4), ((1,2,3,4), {}))

Here is the call graph for this function:

Definition at line 62 of file test_functools.py.

00062 
00063     def test_argument_checking(self):
00064         self.assertRaises(TypeError, self.thetype)     # need at least a func arg
00065         try:
00066             self.thetype(2)()
00067         except TypeError:
00068             pass
00069         else:
00070             self.fail('First arg not checked for callability')

Here is the call graph for this function:

Definition at line 41 of file test_functools.py.

00041 
00042     def test_attributes(self):
00043         p = self.thetype(capture, 1, 2, a=10, b=20)
00044         # attributes should be readable
00045         self.assertEqual(p.func, capture)
00046         self.assertEqual(p.args, (1, 2))
00047         self.assertEqual(p.keywords, dict(a=10, b=20))
00048         # attributes should not be writable
00049         if not isinstance(self.thetype, type):
00050             return
00051         self.assertRaises(AttributeError, setattr, p, 'func', map)
00052         self.assertRaises(AttributeError, setattr, p, 'args', (1, 2))
00053         self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2))
00054 
00055         p = self.thetype(hex)
00056         try:
00057             del p.__dict__
00058         except TypeError:
00059             pass
00060         else:
00061             self.fail('partial object allowed __dict__ to be deleted')

Here is the call graph for this function:

Definition at line 34 of file test_functools.py.

00034 
00035     def test_basic_examples(self):
00036         p = self.thetype(capture, 1, 2, a=10, b=20)
00037         self.assertEqual(p(3, 4, b=30, c=40),
00038                          ((1, 2, 3, 4), dict(a=10, b=30, c=40)))
00039         p = self.thetype(map, lambda x: x*10)
00040         self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40])

Here is the call graph for this function:

Definition at line 128 of file test_functools.py.

00128 
00129     def test_error_propagation(self):
00130         def f(x, y):
00131             x / y
00132         self.assertRaises(ZeroDivisionError, self.thetype(f, 1, 0))
00133         self.assertRaises(ZeroDivisionError, self.thetype(f, 1), 0)
00134         self.assertRaises(ZeroDivisionError, self.thetype(f), 1, 0)
00135         self.assertRaises(ZeroDivisionError, self.thetype(f, y=0), 1)

Here is the call graph for this function:

Definition at line 112 of file test_functools.py.

00112 
00113     def test_keyword(self):
00114         # make sure keyword arguments are captured correctly
00115         for a in ['a', 0, None, 3.5]:
00116             p = self.thetype(capture, a=a)
00117             expected = {'a':a,'x':None}
00118             empty, got = p(x=None)
00119             self.assertTrue(expected == got and empty == ())

Here is the call graph for this function:

Definition at line 92 of file test_functools.py.

00092 
00093     def test_kw_combinations(self):
00094         # exercise special code paths for no keyword args in
00095         # either the partial object or the caller
00096         p = self.thetype(capture)
00097         self.assertEqual(p(), ((), {}))
00098         self.assertEqual(p(a=1), ((), {'a':1}))
00099         p = self.thetype(capture, a=1)
00100         self.assertEqual(p(), ((), {'a':1}))
00101         self.assertEqual(p(b=2), ((), {'a':1, 'b':2}))
00102         # keyword args in the call override those in the partial object
00103         self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2}))

Here is the call graph for this function:

Definition at line 120 of file test_functools.py.

00120 
00121     def test_no_side_effects(self):
00122         # make sure there are no side effects that affect subsequent calls
00123         p = self.thetype(capture, 0, a=1)
00124         args1, kw1 = p(1, b=2)
00125         self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2})
00126         args2, kw2 = p()
00127         self.assertTrue(args2 == (0,) and kw2 == {'a':1})

Here is the call graph for this function:

Reimplemented in test.test_functools.TestPythonPartial.

Definition at line 176 of file test_functools.py.

00176 
00177     def test_pickle(self):
00178         f = self.thetype(signature, 'asdf', bar=True)
00179         f.add_something_to__dict__ = True
00180         f_copy = pickle.loads(pickle.dumps(f))
00181         self.assertEqual(signature(f), signature(f_copy))

Here is the call graph for this function:

Definition at line 104 of file test_functools.py.

00104 
00105     def test_positional(self):
00106         # make sure positional arguments are captured correctly
00107         for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:
00108             p = self.thetype(capture, *args)
00109             expected = args + ('x',)
00110             got, empty = p('x')
00111             self.assertTrue(expected == got and empty == {})

Here is the call graph for this function:

Definition at line 71 of file test_functools.py.

00071 
00072     def test_protection_of_callers_dict_argument(self):
00073         # a caller's dictionary should not be altered by partial
00074         def func(a=10, b=20):
00075             return a
00076         d = {'a':3}
00077         p = self.thetype(func, a=5)
00078         self.assertEqual(p(**d), 3)
00079         self.assertEqual(d, {'a':3})
00080         p(b=7)
00081         self.assertEqual(d, {'a':3})

Here is the call graph for this function:

Reimplemented in test.test_functools.TestPythonPartial.

Definition at line 150 of file test_functools.py.

00150 
00151     def test_repr(self):
00152         args = (object(), object())
00153         args_repr = ', '.join(repr(a) for a in args)
00154         kwargs = {'a': object(), 'b': object()}
00155         kwargs_repr = ', '.join("%s=%r" % (k, v) for k, v in kwargs.items())
00156         if self.thetype is functools.partial:
00157             name = 'functools.partial'
00158         else:
00159             name = self.thetype.__name__
00160 
00161         f = self.thetype(capture)
00162         self.assertEqual('{}({!r})'.format(name, capture),
00163                          repr(f))
00164 
00165         f = self.thetype(capture, *args)
00166         self.assertEqual('{}({!r}, {})'.format(name, capture, args_repr),
00167                          repr(f))
00168 
00169         f = self.thetype(capture, **kwargs)
00170         self.assertEqual('{}({!r}, {})'.format(name, capture, kwargs_repr),
00171                          repr(f))
00172 
00173         f = self.thetype(capture, *args, **kwargs)
00174         self.assertEqual('{}({!r}, {}, {})'.format(name, capture, args_repr, kwargs_repr),
00175                          repr(f))

Here is the call graph for this function:

Definition at line 136 of file test_functools.py.

00136 
00137     def test_weakref(self):
00138         f = self.thetype(int, base=16)
00139         p = proxy(f)
00140         self.assertEqual(f.func, p.func)
00141         f = None
00142         self.assertRaises(ReferenceError, getattr, p, 'func')

Here is the call graph for this function:

Definition at line 143 of file test_functools.py.

00143 
00144     def test_with_bound_and_unbound_methods(self):
00145         data = list(map(str, range(10)))
00146         join = self.thetype(str.join, '')
00147         self.assertEqual(join(data), '0123456789')
00148         join = self.thetype(''.join)
00149         self.assertEqual(join(data), '0123456789')

Here is the call graph for this function:


Member Data Documentation

test.test_functools.TestPartial.thetype = functools.partial [static]

The documentation for this class was generated from the following file: