tests.test_injectors.test_inject_static_method   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 185
Duplicated Lines 57.84 %

Importance

Changes 0
Metric Value
eloc 139
dl 107
loc 185
rs 10
c 0
b 0
f 0
wmc 19

9 Functions

Rating   Name   Duplication   Size   Complexity  
A test_field_injector_correctly_injects_static_method_before_all_fields() 17 17 2
A test_head_injector_correctly_injects_static_method() 17 17 2
A test_field_injector_correctly_injects_static_method_after_all_fields() 17 17 2
A test_return_injector_correctly_injects_static_method_ordinal_returns() 18 18 2
A test_tail_injector_correctly_injects_static_method() 0 14 2
A test_field_injector_correctly_injects_static_method_before_ordinal_field() 19 19 2
A test_return_injector_correctly_injects_static_method_all_returns() 0 18 2
A test_nested_injector_correctly_injects_static_method() 0 19 3
A test_field_injector_correctly_injects_static_method_after_ordinal_field() 19 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
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():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
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():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
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():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
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():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
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():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
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