class Offset(object): def __init__(self, value): self.value = value def add(self, a, b): return self.value + a + b add = inject.bind(add, a=2)
def test_bind_unbound_method(self): class Offset(object): def __init__(self, value): self.value = value def add(self, a, b): return self.value + a + b func = inject.bind(Offset.add, a=2) self.assertEquals(10, func(Offset(5), 3))
def test_bind_first_arg_with_keyword_second_arg(self): def func(a, b): return a + 2 * b func = inject.bind(func, a=1) self.assertEqual(7, func(b=3))
def test_bind_first_arg_with_default_second_arg(self): def func(a, b=5): return a + 2 * b func = inject.bind(func, a=1) self.assertEqual(11, func())
def test_we_cant_override_a_bound_argument(self): def func(a, b): return a + 2 * b func = inject.bind(func, a=1) self.assertRaises(TypeError, func, a=2, b=3)
def test_bind_with_keyword_args(self): def func(a, **kwargs): return a, kwargs func = inject.bind(func, a=1) self.assertEquals((1, dict(b=1, c=1)), func(b=1, c=1))
def test_bind_callable(self): def func(a, b): return a + 2 * b func = inject.bind(func, b=lambda: 1) self.assertEqual(4, func(2))
def test_bind_middle_arg(self): def func(a, b, c): return a + 2 * b + 3 * c func = inject.bind(func, b=1) self.assertEqual(13, func(2, 3))
def test_fail_when_passing_a_normal_argument_twice(self): def func(a, b): return a + 2 * b func = inject.bind(func, a=1) self.assertRaises(TypeError, func, 3, b=3)
def test_bind_out_of_order_args(self): def func(e, d, c, b, a): return (e, d, c, b, a) func = inject.bind(func, a=5, b=4, c=3, d=2) self.assertEqual((1, 2, 3, 4, 5), func(1))
def test_bind_last_arg(self): def func(a, b): return a + 2 * b func = inject.bind(func, b=1) self.assertEqual(4, func(2))
def test_fail_if_we_miss_an_argument(self): def func(a, b): return a + 2 * b func = inject.bind(func, a=1) self.assertRaises(TypeError, func)
def test_bind_with_varargs(self): def func(a, *args): return (a, args) func = inject.bind(func, a=0) self.assertEquals((0, (1, 2)), func(1, 2))
def test_bind_first_arg_with_positional_second_arg(self): def func(a, b): return a + 2 * b func = inject.bind(func, a=1) self.assertEqual(5, func(2))