def test_ctuple(self): from six.moves import range from xoutil.functools import ctuple def echo(*args): return args result = compose(echo, ctuple, list, range, math=False)(10) expected = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) self.assertEqual(expected, result) # Without ctuple prints the list result = compose(echo, list, range, math=False)(10) expected = ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ) self.assertEqual(expected, result)
def __init__(self, name, bases, attrs): from xoutil.names import nameof from xoutil.functools import compose OperatorType.operators.append(self) _PROVIDES = (' This {which} directly provides ' ':class:`xotl.ql.interfaces.{interface}`.\n\n') doc = '' for attr, trans in (('arity', lambda x: nameof(x, inner=True)), ('_method_name', repr), ('_format', compose(str, repr))): value = getattr(self, attr, None) if value: v = trans(value).replace('_', r'\_') v = v.replace("'", '"') doc += ('\n\n - **{attr}:** {v}'.format(attr=attr, v=v)) if doc: self.__doc__ = ((self.__doc__ if self.__doc__ else '') + '\n\n **Attributes**:' + doc) doc = '' interfaces = (IOperator, ) if getattr(self, '_rmethod_name', False): interfaces += (ISyntacticallyReversibleOperation, ) if 'ISyntacticallyReversibleOperation' not in self.__doc__: doc += _PROVIDES.format( which='class', interface='ISyntacticallyReversibleOperation') if getattr(self, 'equivalence_test', False): interfaces += (ISynctacticallyCommutativeOperation, ) if 'ISynctacticallyCommutativeOperation' not in self.__doc__: doc += _PROVIDES.format( which='class', interface='ISynctacticallyCommutativeOperation') if doc: self.__doc__ += '\n\n **Interface(s)**:\n\n' + doc directlyProvides(self, *interfaces)
def test_simple_case(self): incr = lambda x: x + 1 add_3 = compose(incr, incr, incr) self.assertEqual(3, add_3(0))
def test_only_callables(self): with self.assertRaises(TypeError): compose(1)
def test_single_argument_is_identitical(self): def anything(): pass self.assertIs(anything, compose(anything))
def test_needs_at_least_an_argument(self): with self.assertRaises(TypeError): compose()