| Total Complexity | 9 |
| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from lagom import Container, injectable |
||
| 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 = injectable) -> 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 | class MethodBasedBar: |
||
| 25 | def greet(self, message: str, foo: Foo = injectable) -> str: |
||
| 26 | return foo.greet() + message |
||
| 27 | |||
| 28 | |||
| 29 | def test_partial_application_can_be_applied_to_class(): |
||
| 30 | bar = container.partial(Bar)(not_injected="!") |
||
| 31 | assert bar.greet() == "Hello Foo!" |
||
| 32 | |||
| 33 | |||
| 34 | def test_passed_in_arguments_are_used_over_container_generated_ones_when_positional(): |
||
| 35 | partial_bar = container.partial(Bar) |
||
| 36 | assert partial_bar("!", Foo(name="Local")).greet() == "Hello Local!"
|
||
| 37 | |||
| 38 | |||
| 39 | def test_passed_in_arguments_are_used_over_container_generated_ones_when_named(): |
||
| 40 | partial_bar = container.partial(Bar) |
||
| 41 | assert ( |
||
| 42 | partial_bar(not_injected="!", foo=Foo(name="Local")).greet() == "Hello Local!" |
||
| 43 | ) |
||
| 44 | |||
| 45 | |||
| 46 | def test_partial_application_can_be_applied_to_instance_method(): |
||
| 47 | bar = MethodBasedBar() |
||
| 48 | partial = container.partial(bar.greet) |
||
| 49 | assert partial(", hello?") == "Hello Foo, hello?"
|
||
| 50 |