Total Complexity | 7 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from typing import Generator, AsyncGenerator |
||
2 | |||
3 | import pytest |
||
4 | |||
5 | from lagom import Container, bind_to_container |
||
6 | from lagom.exceptions import UnresolvableType |
||
7 | |||
8 | |||
9 | class MyDep: |
||
10 | value: str = "testing" |
||
11 | |||
12 | |||
13 | container = Container() |
||
14 | |||
15 | |||
16 | def example_function(resolved: MyDep, message: str) -> str: |
||
17 | return resolved.value + message |
||
18 | |||
19 | |||
20 | def example_generator(resolved: MyDep, message: str) -> Generator: |
||
21 | yield resolved.value + message |
||
22 | |||
23 | |||
24 | @bind_to_container(container) |
||
25 | def another_example_function(resolved: MyDep, message: str) -> str: |
||
26 | return resolved.value + message |
||
27 | |||
28 | |||
29 | def test_partial_application_can_be_applied_to_functions(): |
||
30 | partial = container.partial(example_function) |
||
31 | assert partial(message=" world") == "testing world" |
||
32 | |||
33 | |||
34 | def test_a_decorator_can_be_used_to_bind_as_well(): |
||
35 | assert another_example_function(message=" hello") == "testing hello" |
||
36 | |||
37 | |||
38 | def test_partial_application_can_be_applied_to_generators(): |
||
39 | partial = container.partial(example_generator) |
||
40 | results = [] |
||
41 | for result in partial(message=" world"): |
||
42 | results.append(result) |
||
43 | assert results == ["testing world"] |
||
44 |