def test_common_args(self): DEFAULT_CONTAINER.register_instance(2, int) DEFAULT_CONTAINER.register_instance(4.0, float) @Inject(int) @Inject(float, int) def sum_func(*args): self.assertIsInstance(args[0], float) self.assertIsInstance(args[1], int) self.assertIsInstance(args[1], int) self.assertGreaterEqual(len(args), 3) return sum(args) self.assertEqual(8, call(sum_func)) self.assertEqual(11, call(sum_func, 3))
def test_inheritance(self): test_case = self DEFAULT_CONTAINER.register_instance('inheritance', str) DEFAULT_CONTAINER.register_instance({}, dict) DEFAULT_CONTAINER.register_instance([], list) @Inject(str, some_object=object) class A(object): def __init__(self, some_str, some_object): test_case.assertIsInstance(some_str, str) test_case.assertIsInstance(some_object, object) @Inject(some_object=object, some_dict=dict) class B(A): def __init__(self, some_str, some_object, some_dict): test_case.assertIsInstance(some_str, str) test_case.assertIsInstance(some_object, object) test_case.assertIsInstance(some_dict, dict) @Inject(some_object=object, some_list=list) class C(A): def __init__(self, some_str, some_object, some_list): test_case.assertIsInstance(some_str, str) test_case.assertIsInstance(some_object, object) test_case.assertIsInstance(some_list, list) DEFAULT_CONTAINER.call(A) DEFAULT_CONTAINER.call(B) DEFAULT_CONTAINER.call(C)
def test_with_context_args(self): DEFAULT_CONTAINER.register_instance(1, int, context='1') DEFAULT_CONTAINER.register_instance(2, int, context='2') DEFAULT_CONTAINER.register_instance(3, int, context='3') @Inject(int, _context='3') @Inject(int, _context='2') @Inject(int, _context='1') def sum_func(*args): for i, arg in zip(range(1, len(args) + 1), args): self.assertEqual(i, arg) return sum(args) self.assertEqual(6, call(sum_func)) self.assertEqual(10, call(sum_func, 4))
def test_simple_func(self): @Inject(_name_as_context=True) def sub_func(a: int, b: int) -> int: return a - b DEFAULT_CONTAINER.register_instance(4, int, context='a') DEFAULT_CONTAINER.register_instance(2, int, context='b') self.assertEqual(2, call(sub_func)) DEFAULT_CONTAINER.register_instance(2, int, context='a') DEFAULT_CONTAINER.register_instance(2, int, context='b') self.assertEqual(0, call(sub_func)) DEFAULT_CONTAINER.register_instance(2, int, context='a') DEFAULT_CONTAINER.register_instance(4, int, context='b') self.assertEqual(-2, call(sub_func))
def test_simple_class(self): frc = WithForwardReferenceClass() DEFAULT_CONTAINER.register_instance(frc) self.assertIs(frc, call(frc.kwargs_inject))
def test_method_with_injection(self): DEFAULT_CONTAINER.register_instance(self) complex_class = new(ComplexClass) call(complex_class.method_with_injection)