Total Complexity | 8 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from lagom import Container |
||
2 | |||
3 | |||
4 | class Foo: |
||
5 | def __init__(self, name="Foo") -> None: |
||
6 | self._name = name |
||
7 | |||
8 | def greet(self) -> str: |
||
9 | return f"Hello {self._name}" |
||
10 | |||
11 | |||
12 | container = Container() |
||
13 | |||
14 | |||
15 | class Bar: |
||
16 | def __init__(self, not_injected: str, foo: Foo) -> None: |
||
17 | self.not_injected = not_injected |
||
18 | self.foo = foo |
||
19 | |||
20 | def greet(self) -> str: |
||
21 | return self.foo.greet() + self.not_injected |
||
22 | |||
23 | |||
24 | def test_partial_application_can_be_applied_to_class(): |
||
25 | bar = container.magic_partial(Bar)(not_injected="!") |
||
26 | assert bar.greet() == "Hello Foo!" |
||
27 | |||
28 | |||
29 | def test_one_class_can_be_bound_multiple_times(): |
||
30 | bar = container.magic_partial(Bar)(not_injected="!") |
||
31 | another_bar = container.magic_partial(Bar)(not_injected="?") |
||
32 | assert bar.greet() == "Hello Foo!" |
||
33 | assert another_bar.greet() == "Hello Foo?" |
||
34 | |||
35 | |||
36 | def test_passed_in_arguments_are_used_over_container_generated_ones_when_positional(): |
||
37 | partial_bar = container.magic_partial(Bar) |
||
38 | assert partial_bar("!", Foo(name="Local")).greet() == "Hello Local!" |
||
39 | |||
40 | |||
41 | def test_passed_in_arguments_are_used_over_container_generated_ones_when_named(): |
||
42 | partial_bar = container.magic_partial(Bar) |
||
43 | assert ( |
||
44 | partial_bar(not_injected="!", foo=Foo(name="Local")).greet() == "Hello Local!" |
||
45 | ) |
||
46 |