File: test_cognitive_complexity.py

package info (click to toggle)
cognitive-complexity 1.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 132 kB
  • sloc: python: 376; makefile: 10; sh: 5
file content (256 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
from conftest import get_code_snippet_compexity


def test_simple_if_simple_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a:  # +1
            return 1
    """) == 1


def test_simple_if_serial_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a and b and True:  # +2
            return 1
    """) == 2


def test_simple_if_serial_heterogenious_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a and b or True:  # +3
            return 1
    """) == 3


def test_simple_if_complex_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if (  # +1
            a and b and  # +1
            (c or d)  # +1
        ):
            return 1
    """) == 3


def test_simple_structure_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if (a):  # +1
            return 1
        if (b):  # +1
            return 2
    """) == 2


def test_simple_elif_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if (a):  # +1
            return 1
        elif (b):  # +1
            return 2
        else:  # +1
            return 3
    """) == 3


def test_simple_else_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a):
        if (a):  # +1
            return 1
        else:  # +1
            return 3
    """) == 2


def test_nested_structure_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a:  # +1
            for i in range(b):  # +2
                return 1
    """) == 3


def test_very_nested_structure_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a:  # +1
            for i in range(b):  # +2
                if b:  # +3
                    return 1
    """) == 6


def test_try_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        try:
            for foo in bar:  # +1
                return a
        except Exception:  # +1
            if a < 0:  # +2
                return a
    """) == 4


def test_recursion_complexity():
    assert get_code_snippet_compexity("""
    def f(a):
        return a * f(a - 1)  # +1 for recursion
    """) == 1


def test_real_function():
    assert get_code_snippet_compexity("""
    def process_raw_constant(constant, min_word_length):
        processed_words = []
        raw_camelcase_words = []
        for raw_word in re.findall(r'[a-z]+', constant):  # +1
            word = raw_word.strip()
            if (  # +2 (nesting = 1)
                len(word) >= min_word_length  # +2 (2 bool operator sequences)
                and not (word.startswith('-') or word.endswith('-'))
            ):
                if is_camel_case_word(word):  # +3 (nesting=2)
                    raw_camelcase_words.append(word)
                else:  # +1
                    processed_words.append(word.lower())
        return processed_words, raw_camelcase_words
    """) == 9


def test_break_and_continue():
    assert get_code_snippet_compexity("""
    def f(a):
        for a in range(10):  # +1
            if a % 2:  # +2
                continue
            if a == 8:  # +2
                break
    """) == 5


def test_nested_functions():
    assert get_code_snippet_compexity("""
    def f(a):
        def foo(a):
            if a:  # +2
                return 1
        bar = lambda a: lambda b: b or 2  # +1
        return bar(foo(a))(a)
    """) == 3


def test_ternary_operator():
    assert get_code_snippet_compexity("""
    def f(a):
        if a % 2:  # +1
            return 'c' if a else 'd'  # +2
        return 'a' if a else 'b'  # +1
    """) == 4


def test_nested_if_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a == b:  # +1
            if (a):  # +2 (nesting=1)
                return 1
        return 0
    """) == 3


def test_nested_else_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a == b:  # +1
            if (a):  # +2 (nesting=1)
                return 1
            else:  # +1
                return 3
        return 0
    """) == 4


def test_nested_elif_condition_complexity():
    assert get_code_snippet_compexity("""
    def f(a, b):
        if a == b:  # +1
            if (a):  # +2 (nesting=1)
                return 1
            elif (b):  # +1
                return 2
            else:  # +1
                return 3
        return 0
    """) == 5


def test_for_else_complexity():
    assert get_code_snippet_compexity("""
    def f(a):
        for a in range(10):  # +1
            if a % 2:  # +2
                continue
            if a == 8:  # +2
                break
        else:  # +1
            return 5
    """) == 6


def test_while_else_complexity():
    assert get_code_snippet_compexity("""
    def f(a):
        a = 0
        while a < 10:  # +1
            if a % 2:  # +2
                continue
            if a == 8:  # +2
                break
            a += 1
        else:  # +1
            return 5
    """) == 6


def test_a_decorator_complexity():
    assert get_code_snippet_compexity("""
    def a_decorator(a, b):
        def inner(func):  # nesting = 0
            if condition:  # +1
                print(b)
            func()
        return inner
    """) == 1


def test_not_a_decorator_complexity():
    assert get_code_snippet_compexity("""
    def not_a_decorator(a, b):
        my_var = a*b
        def inner(func):  # nesting = 1
            if condition:  # +1 structure, +1 nesting
                print(b)
            func()
        return inner
    """) == 2


def test_decorator_generator_complexity():
    assert get_code_snippet_compexity("""
    def decorator_generator(a):
        def generator(func):
            def decorator(func): # nesting = 0
                if condition: # +1
                    print(b)
                return func()
            return decorator
        return generator
    """) == 1