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
|
|
View Code Duplication |
def test_head_injector_correctly_injects_static_method(): |
|
|
|
|
12
|
|
|
class Target: |
13
|
|
|
@staticmethod |
14
|
|
|
def target(x): |
15
|
|
|
a = 10 |
16
|
|
|
if x > a: |
17
|
|
|
a = x |
18
|
|
|
return a |
19
|
|
|
|
20
|
|
|
@inject(target=Target.target, injector=HeadInjector()) |
21
|
|
|
def handler(): |
22
|
|
|
x = 11 |
23
|
|
|
|
24
|
|
|
target = Target() |
25
|
|
|
assert target.target(0) == 11 |
26
|
|
|
assert target.target(10) == 11 |
27
|
|
|
assert target.target(101) == 11 |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def test_tail_injector_correctly_injects_static_method(): |
31
|
|
|
class Target: |
32
|
|
|
@staticmethod |
33
|
|
|
def target(x): |
34
|
|
|
if x > 100: |
35
|
|
|
return x |
36
|
|
|
|
37
|
|
|
@inject(target=Target.target, injector=TailInjector()) |
38
|
|
|
def handler(): |
39
|
|
|
return -1 |
40
|
|
|
|
41
|
|
|
target = Target() |
42
|
|
|
assert target.target(13) == -1 |
43
|
|
|
assert target.target(101) == 101 |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_return_injector_correctly_injects_static_method_all_returns(): |
47
|
|
|
class Target: |
48
|
|
|
@staticmethod |
49
|
|
|
def target(x): |
50
|
|
|
if x > 100: |
51
|
|
|
y = x * 2 |
52
|
|
|
return y |
53
|
|
|
else: |
54
|
|
|
y = x + 2 |
55
|
|
|
return y |
56
|
|
|
|
57
|
|
|
@inject(target=Target.target, injector=ReturnInjector()) |
58
|
|
|
def handler(): |
59
|
|
|
return '{} :)'.format(y) |
|
|
|
|
60
|
|
|
|
61
|
|
|
target = Target() |
62
|
|
|
assert target.target(13) == '15 :)' |
63
|
|
|
assert target.target(101) == '202 :)' |
64
|
|
|
|
65
|
|
|
|
66
|
|
View Code Duplication |
def test_return_injector_correctly_injects_static_method_ordinal_returns(): |
|
|
|
|
67
|
|
|
class Target: |
68
|
|
|
@staticmethod |
69
|
|
|
def target(x): |
70
|
|
|
if x > 100: |
71
|
|
|
y = x * 2 |
72
|
|
|
return y |
73
|
|
|
else: |
74
|
|
|
y = x + 2 |
75
|
|
|
return y |
76
|
|
|
|
77
|
|
|
@inject(target=Target.target, injector=ReturnInjector(ordinal=1)) |
78
|
|
|
def handler(): |
79
|
|
|
return '{} :)'.format(y) |
|
|
|
|
80
|
|
|
|
81
|
|
|
target = Target() |
82
|
|
|
assert target.target(13) == '15 :)' |
83
|
|
|
assert target.target(101) == 202 |
84
|
|
|
|
85
|
|
|
|
86
|
|
View Code Duplication |
def test_field_injector_correctly_injects_static_method_before_all_fields(): |
|
|
|
|
87
|
|
|
class Target: |
88
|
|
|
@staticmethod |
89
|
|
|
def target(x): |
90
|
|
|
if x > 100: |
91
|
|
|
y = x * 2 |
92
|
|
|
else: |
93
|
|
|
y = x + 2 |
94
|
|
|
return y |
95
|
|
|
|
96
|
|
|
@inject(target=Target.target, injector=FieldInjector('y', insert='before')) |
97
|
|
|
def handler(): |
98
|
|
|
x += 1 |
|
|
|
|
99
|
|
|
|
100
|
|
|
target = Target() |
101
|
|
|
assert target.target(13) == 16 |
102
|
|
|
assert target.target(101) == 204 |
103
|
|
|
|
104
|
|
|
|
105
|
|
View Code Duplication |
def test_field_injector_correctly_injects_static_method_after_all_fields(): |
|
|
|
|
106
|
|
|
class Target: |
107
|
|
|
@staticmethod |
108
|
|
|
def target(x): |
109
|
|
|
if x > 100: |
110
|
|
|
y = x * 2 |
111
|
|
|
else: |
112
|
|
|
y = x + 2 |
113
|
|
|
return y |
114
|
|
|
|
115
|
|
|
@inject(target=Target.target, injector=FieldInjector('y', insert='after')) |
116
|
|
|
def handler(): |
117
|
|
|
y -= 1 |
|
|
|
|
118
|
|
|
|
119
|
|
|
target = Target() |
120
|
|
|
assert target.target(13) == 14 |
121
|
|
|
assert target.target(101) == 201 |
122
|
|
|
|
123
|
|
|
|
124
|
|
View Code Duplication |
def test_field_injector_correctly_injects_static_method_before_ordinal_field(): |
|
|
|
|
125
|
|
|
class Target: |
126
|
|
|
@staticmethod |
127
|
|
|
def target(x): |
128
|
|
|
if x > 100: |
129
|
|
|
y = x * 2 |
130
|
|
|
else: |
131
|
|
|
y = x + 2 |
132
|
|
|
return y |
133
|
|
|
|
134
|
|
|
@inject( |
135
|
|
|
target=Target.target, injector=FieldInjector('y', ordinal=1, insert='before'), |
136
|
|
|
) |
137
|
|
|
def handler(): |
138
|
|
|
x += 1 |
|
|
|
|
139
|
|
|
|
140
|
|
|
target = Target() |
141
|
|
|
assert target.target(13) == 16 |
142
|
|
|
assert target.target(101) == 202 |
143
|
|
|
|
144
|
|
|
|
145
|
|
View Code Duplication |
def test_field_injector_correctly_injects_static_method_after_ordinal_field(): |
|
|
|
|
146
|
|
|
class Target: |
147
|
|
|
@staticmethod |
148
|
|
|
def target(x): |
149
|
|
|
if x > 100: |
150
|
|
|
y = x * 2 |
151
|
|
|
else: |
152
|
|
|
y = x + 2 |
153
|
|
|
return y |
154
|
|
|
|
155
|
|
|
@inject( |
156
|
|
|
target=Target.target, injector=FieldInjector('y', ordinal=0, insert='after') |
157
|
|
|
) |
158
|
|
|
def handler(): |
159
|
|
|
y -= 1 |
|
|
|
|
160
|
|
|
|
161
|
|
|
target = Target() |
162
|
|
|
assert target.target(13) == 15 |
163
|
|
|
assert target.target(101) == 201 |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
def test_nested_injector_correctly_injects_static_method(): |
167
|
|
|
class Target: |
168
|
|
|
@staticmethod |
169
|
|
|
def target(x): |
170
|
|
|
def nested(y): |
171
|
|
|
if y > 100: |
172
|
|
|
return y |
173
|
|
|
|
174
|
|
|
if x < 200: |
175
|
|
|
return nested(x) |
176
|
|
|
|
177
|
|
|
@inject(target=Target.target, injector=NestedInjector('nested', TailInjector())) |
178
|
|
|
def handler(): |
179
|
|
|
return -1 |
180
|
|
|
|
181
|
|
|
target = Target() |
182
|
|
|
assert target.target(13) == -1 |
183
|
|
|
assert target.target(101) == 101 |
184
|
|
|
assert target.target(200) is None |
185
|
|
|
|