def test_getattr(self): def func(x): return x * 2 f = functions.composable(func) self.assertEqual((f . str)('blah'), 'blahblah') self.assertRaises(NameError, lambda: f . NonExistantName)
def test_compose(self): f = functions.composable(lambda x: x ** 3) h1 = f << (lambda x: x + 2) self.assertEqual(h1(2), 64) h2 = f >> (lambda x: x + 10) self.assertEqual(h2(4), 74)
def test_iterate(self): def func(x): return x * 2 f = functions.composable(func) self.assertEqual((f ** 2)(1), 4) self.assertEqual((f ** 3)(1), 8)
def test_ops(self): ops = operator.attrgetter(*self.ops)(operator) f = lambda x: x ** 2 g = lambda y: y > 10 and y - 5 or y * 3 f = functions.composable(f) g = functions.composable(g) for op in ops: h = op(f, g) self.assertTrue(callable(h)) start = random.randint(100, 200) for n in range(start, start + 100): self.assertEqual(h(n), op(f(n), g(n))) unary_ops = operator.attrgetter(*self.unary_ops)(operator) for op in unary_ops: h = op(f) n = random.randint(100, 1000) self.assertEqual(op(f(n)), h(n))
def test_repr(self): def func(x): return x + 100 f = functions.composable(func) self.assertEqual(repr(f), repr(func))