1
|
|
|
from injectify.api import inject |
2
|
|
|
from injectify.injectors import ( |
3
|
|
|
HeadInjector, |
4
|
|
|
TailInjector, |
5
|
|
|
ReturnInjector, |
6
|
|
|
FieldInjector, |
7
|
|
|
NestedInjector, |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def test_head_injector_correctly_injects_function(): |
12
|
|
|
def target(x): |
13
|
|
|
a = 10 |
14
|
|
|
if x > a: |
15
|
|
|
a = x |
16
|
|
|
return a |
17
|
|
|
|
18
|
|
|
@inject(target=target, injector=HeadInjector()) |
19
|
|
|
def handler(): |
20
|
|
|
x = 11 |
21
|
|
|
|
22
|
|
|
assert target(0) == 11 |
23
|
|
|
assert target(10) == 11 |
24
|
|
|
assert target(101) == 11 |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def test_tail_injector_correctly_injects_function(): |
28
|
|
|
def target(x): |
29
|
|
|
if x > 100: |
30
|
|
|
return x |
31
|
|
|
|
32
|
|
|
@inject(target=target, injector=TailInjector()) |
33
|
|
|
def handler(): |
34
|
|
|
return -1 |
35
|
|
|
|
36
|
|
|
assert target(13) == -1 |
37
|
|
|
assert target(101) == 101 |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def test_return_injector_correctly_injects_function_all_returns(): |
41
|
|
|
def target(x): |
42
|
|
|
if x > 100: |
43
|
|
|
y = x * 2 |
44
|
|
|
return y |
45
|
|
|
else: |
46
|
|
|
y = x + 2 |
47
|
|
|
return y |
48
|
|
|
|
49
|
|
|
@inject(target=target, injector=ReturnInjector()) |
50
|
|
|
def handler(): |
51
|
|
|
return '{} :)'.format(y) |
|
|
|
|
52
|
|
|
|
53
|
|
|
assert target(13) == '15 :)' |
54
|
|
|
assert target(101) == '202 :)' |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_return_injector_correctly_injects_function_ordinal_returns(): |
58
|
|
|
def target(x): |
59
|
|
|
if x > 100: |
60
|
|
|
y = x * 2 |
61
|
|
|
return y |
62
|
|
|
else: |
63
|
|
|
y = x + 2 |
64
|
|
|
return y |
65
|
|
|
|
66
|
|
|
@inject(target=target, injector=ReturnInjector(ordinal=1)) |
67
|
|
|
def handler(): |
68
|
|
|
return '{} :)'.format(y) |
|
|
|
|
69
|
|
|
|
70
|
|
|
assert target(13) == '15 :)' |
71
|
|
|
assert target(101) == 202 |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def test_field_injector_correctly_injects_function_before_all_fields(): |
75
|
|
|
def target(x): |
76
|
|
|
if x > 100: |
77
|
|
|
y = x * 2 |
78
|
|
|
else: |
79
|
|
|
y = x + 2 |
80
|
|
|
return y |
81
|
|
|
|
82
|
|
|
@inject(target=target, injector=FieldInjector('y', insert='before')) |
83
|
|
|
def handler(): |
84
|
|
|
x += 1 |
|
|
|
|
85
|
|
|
|
86
|
|
|
assert target(13) == 16 |
87
|
|
|
assert target(101) == 204 |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def test_field_injector_correctly_injects_function_after_all_fields(): |
91
|
|
|
def target(x): |
92
|
|
|
if x > 100: |
93
|
|
|
y = x * 2 |
94
|
|
|
else: |
95
|
|
|
y = x + 2 |
96
|
|
|
return y |
97
|
|
|
|
98
|
|
|
@inject(target=target, injector=FieldInjector('y', insert='after')) |
99
|
|
|
def handler(): |
100
|
|
|
y -= 1 |
|
|
|
|
101
|
|
|
|
102
|
|
|
assert target(13) == 14 |
103
|
|
|
assert target(101) == 201 |
104
|
|
|
|
105
|
|
|
|
106
|
|
View Code Duplication |
def test_field_injector_correctly_injects_function_before_ordinal_field(): |
107
|
|
|
def target(x): |
108
|
|
|
if x > 100: |
109
|
|
|
y = x * 2 |
110
|
|
|
else: |
111
|
|
|
y = x + 2 |
112
|
|
|
return y |
113
|
|
|
|
114
|
|
|
@inject( |
115
|
|
|
target=target, injector=FieldInjector('y', ordinal=1, insert='before'), |
116
|
|
|
) |
117
|
|
|
def handler(): |
118
|
|
|
x += 1 |
|
|
|
|
119
|
|
|
|
120
|
|
|
assert target(13) == 16 |
121
|
|
|
assert target(101) == 202 |
122
|
|
|
|
123
|
|
|
|
124
|
|
View Code Duplication |
def test_field_injector_correctly_injects_function_after_ordinal_field(): |
125
|
|
|
def target(x): |
126
|
|
|
if x > 100: |
127
|
|
|
y = x * 2 |
128
|
|
|
else: |
129
|
|
|
y = x + 2 |
130
|
|
|
return y |
131
|
|
|
|
132
|
|
|
@inject(target=target, injector=FieldInjector('y', ordinal=0, insert='after')) |
133
|
|
|
def handler(): |
134
|
|
|
y -= 1 |
|
|
|
|
135
|
|
|
|
136
|
|
|
assert target(13) == 15 |
137
|
|
|
assert target(101) == 201 |
138
|
|
|
|
139
|
|
|
|
140
|
|
View Code Duplication |
def test_nested_injector_correctly_injects_function(): |
|
|
|
|
141
|
|
|
def target(x): |
142
|
|
|
def nested(y): |
143
|
|
|
if y > 100: |
144
|
|
|
return y |
145
|
|
|
|
146
|
|
|
if x < 200: |
147
|
|
|
return nested(x) |
148
|
|
|
|
149
|
|
|
@inject(target=target, injector=NestedInjector('nested', TailInjector())) |
150
|
|
|
def handler(): |
151
|
|
|
return -1 |
152
|
|
|
|
153
|
|
|
assert target(13) == -1 |
154
|
|
|
assert target(101) == 101 |
155
|
|
|
assert target(200) is None |
156
|
|
|
|