tests.test_injectors.test_inject_function   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 156
Duplicated Lines 29.49 %

Importance

Changes 0
Metric Value
eloc 111
dl 46
loc 156
rs 10
c 0
b 0
f 0
wmc 19

9 Functions

Rating   Name   Duplication   Size   Complexity  
A test_nested_injector_correctly_injects_function() 16 16 3
A test_return_injector_correctly_injects_function_ordinal_returns() 0 15 2
A test_field_injector_correctly_injects_function_after_all_fields() 0 14 2
A test_return_injector_correctly_injects_function_all_returns() 0 15 2
A test_tail_injector_correctly_injects_function() 0 11 2
A test_field_injector_correctly_injects_function_before_all_fields() 0 14 2
A test_head_injector_correctly_injects_function() 0 14 2
A test_field_injector_correctly_injects_function_before_ordinal_field() 16 16 2
A test_field_injector_correctly_injects_function_after_ordinal_field() 14 14 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
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)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
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)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable y does not seem to be defined.
Loading history...
135
136
    assert target(13) == 15
137
    assert target(101) == 201
138
139
140 View Code Duplication
def test_nested_injector_correctly_injects_function():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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