Passed
Push — master ( c1b59e...b3bb01 )
by Mitchell
03:13
created

test_return_injector_correctly_injects_method_all_returns()   A

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 14

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nop 0
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
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_method():
12
    class Target:
13
        def target(self, x):
14
            a = 10
15
            if x > a:
16
                a = x
17
            return a
18
19
    @inject(target=Target.target, injector=HeadInjector())
20
    def handler():
21
        x = 11
22
23
    target = Target()
24
    assert target.target(0) == 11
25
    assert target.target(10) == 11
26
    assert target.target(101) == 11
27
28
29
def test_tail_injector_correctly_injects_method():
30
    class Target:
31
        def target(self, x):
32
            if x > 100:
33
                return x
34
35
    @inject(target=Target.target, injector=TailInjector())
36
    def handler():
37
        return -1
38
39
    target = Target()
40
    assert target.target(13) == -1
41
    assert target.target(101) == 101
42
43
44 View Code Duplication
def test_return_injector_correctly_injects_method_all_returns():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
45
    class Target:
46
        def target(self, x):
47
            if x > 100:
48
                y = x * 2
49
                return y
50
            else:
51
                y = x + 2
52
                return y
53
54
    @inject(target=Target.target, injector=ReturnInjector())
55
    def handler():
56
        return '{} :)'.format(y)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
57
58
    target = Target()
59
    assert target.target(13) == '15 :)'
60
    assert target.target(101) == '202 :)'
61
62
63 View Code Duplication
def test_return_injector_correctly_injects_method_ordinal_returns():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
64
    class Target:
65
        def target(self, x):
66
            if x > 100:
67
                y = x * 2
68
                return y
69
            else:
70
                y = x + 2
71
                return y
72
73
    @inject(target=Target.target, injector=ReturnInjector(ordinal=1))
74
    def handler():
75
        return '{} :)'.format(y)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
76
77
    target = Target()
78
    assert target.target(13) == '15 :)'
79
    assert target.target(101) == 202
80
81
82 View Code Duplication
def test_field_injector_correctly_injects_method_before_all_fields():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
83
    class Target:
84
        def target(self, x):
85
            if x > 100:
86
                y = x * 2
87
            else:
88
                y = x + 2
89
            return y
90
91
    @inject(target=Target.target, injector=FieldInjector('y', insert='before'))
92
    def handler():
93
        x += 1
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
94
95
    target = Target()
96
    assert target.target(13) == 16
97
    assert target.target(101) == 204
98
99
100 View Code Duplication
def test_field_injector_correctly_injects_method_after_all_fields():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
101
    class Target:
102
        def target(self, x):
103
            if x > 100:
104
                y = x * 2
105
            else:
106
                y = x + 2
107
            return y
108
109
    @inject(target=Target.target, injector=FieldInjector('y', insert='after'))
110
    def handler():
111
        y -= 1
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
112
113
    target = Target()
114
    assert target.target(13) == 14
115
    assert target.target(101) == 201
116
117
118 View Code Duplication
def test_field_injector_correctly_injects_method_before_ordinal_field():
119
    class Target:
120
        def target(self, x):
121
            if x > 100:
122
                y = x * 2
123
            else:
124
                y = x + 2
125
            return y
126
127
    @inject(
128
        target=Target.target, injector=FieldInjector('y', ordinal=1, insert='before'),
129
    )
130
    def handler():
131
        x += 1
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
132
133
    target = Target()
134
    assert target.target(13) == 16
135
    assert target.target(101) == 202
136
137
138 View Code Duplication
def test_field_injector_correctly_injects_method_after_ordinal_field():
139
    class Target:
140
        def target(self, x):
141
            if x > 100:
142
                y = x * 2
143
            else:
144
                y = x + 2
145
            return y
146
147
    @inject(
148
        target=Target.target, injector=FieldInjector('y', ordinal=0, insert='after')
149
    )
150
    def handler():
151
        y -= 1
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
152
153
    target = Target()
154
    assert target.target(13) == 15
155
    assert target.target(101) == 201
156
157
158 View Code Duplication
def test_nested_injector_correctly_injects_method():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
159
    class Target:
160
        def target(self, x):
161
            def nested(y):
162
                if y > 100:
163
                    return y
164
165
            if x < 200:
166
                return nested(x)
167
168
    @inject(target=Target.target, injector=NestedInjector('nested', TailInjector()))
169
    def handler():
170
        return -1
171
172
    target = Target()
173
    assert target.target(13) == -1
174
    assert target.target(101) == 101
175
    assert target.target(200) is None
176