Skip to content

Commit 4aac3c5

Browse files
code clean up, more tests
1 parent 86863fa commit 4aac3c5

2 files changed

Lines changed: 62 additions & 32 deletions

File tree

aibolit/metrics/cc/main.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,43 @@ class CCMetric:
1717
"""
1818

1919
def value(self, ast: AST) -> int:
20-
total_complexity = 0
21-
22-
for method in ast.get_proxy_nodes(ASTNodeType.METHOD_DECLARATION):
23-
total_complexity += self._method_complexity(ast, method)
24-
25-
for constructor in ast.get_proxy_nodes(ASTNodeType.CONSTRUCTOR_DECLARATION):
26-
total_complexity += self._method_complexity(ast, constructor)
27-
28-
return total_complexity
20+
return sum(
21+
self._method_complexity(ast, node)
22+
for node in ast.get_proxy_nodes(
23+
ASTNodeType.METHOD_DECLARATION, ASTNodeType.CONSTRUCTOR_DECLARATION
24+
)
25+
)
2926

3027
def _method_complexity(self, ast: AST, method: ASTNode) -> int:
31-
complexity = 1
3228
method_ast = ast.get_subtree(method)
33-
34-
for node in method_ast.get_proxy_nodes():
35-
complexity += self._node_complexity(ast, node)
36-
37-
return complexity
29+
return 1 + sum(self._node_complexity(ast, node) for node in method_ast.get_proxy_nodes())
3830

3931
def _node_complexity(self, ast: AST, node: ASTNode) -> int:
40-
complexity = 0
4132
if node.node_type in _SIMPLE_NODES:
42-
complexity = 1
33+
return 1
4334
elif node.node_type in _CONDITION_NODES:
44-
complexity = self._condition_complexity(ast, node)
35+
return self._condition_complexity(ast, node)
4536
elif node.node_type == ASTNodeType.FOR_STATEMENT:
46-
complexity = self._for_statement_complexity(ast, node)
47-
48-
return complexity
37+
return self._for_statement_complexity(ast, node)
38+
return 0
4939

5040
def _condition_complexity(self, ast: AST, node: ASTNode) -> int:
5141
return 1 + self._expression_complexity(ast, node.condition)
5242

5343
def _for_statement_complexity(self, ast: AST, node: ASTNode) -> int:
54-
complexity = 1
5544
if hasattr(node.control, 'condition'):
56-
complexity = self._condition_complexity(ast, node.control)
57-
return complexity
45+
return self._condition_complexity(ast, node.control)
46+
return 1
5847

5948
def _expression_complexity(self, ast: AST, expression: ASTNode | None) -> int:
6049
if expression is None:
6150
return 0
62-
6351
expression_ast = ast.get_subtree(expression)
64-
count = 0
65-
66-
for node in expression_ast.get_proxy_nodes(ASTNodeType.BINARY_OPERATION):
67-
if node.operator in ('&&', '||'):
68-
count += 1
69-
70-
return count
52+
return sum(
53+
1
54+
for node in expression_ast.get_proxy_nodes(ASTNodeType.BINARY_OPERATION)
55+
if node.operator in ('&&', '||')
56+
)
7157

7258

7359
_SIMPLE_NODES = (

test/metrics/cc/test_all_types.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,50 @@ class Dummy {
181181
).strip()
182182
self.assertEqual(self._cc_metric_for(content), 4)
183183

184+
def test_multiple_nested_statements(self):
185+
content = dedent(
186+
'''\
187+
class Dummy {
188+
void test() { // CC = 11
189+
int x = 0, y = 2;
190+
boolean a = false, b = true;
191+
if (a && (y == 1 ? b : true)) { // +3
192+
if (y == x) { // +1
193+
while (true) { // +1
194+
if (x++ < 20) { // +1
195+
break; // +1
196+
}
197+
}
198+
} else if (y == t && !d) { // +2
199+
x = a ? y : x; // +1
200+
} else {
201+
x = 2;
202+
}
203+
}
204+
}
205+
}
206+
'''
207+
).strip()
208+
self.assertEqual(self._cc_metric_for(content), 11)
209+
210+
def test_multiple_boolean_operators(self):
211+
content = dedent(
212+
'''\
213+
class Dummy {
214+
void test() { // CC = 8
215+
int x=0, y=1;
216+
boolean a, b;
217+
if (x > 2 || y < 4) { // +2
218+
while (x++ < 10 && !(y++ < 0)); // +2
219+
} else if (a && b || x < 4) { // +3
220+
return;
221+
}
222+
}
223+
}
224+
'''
225+
).strip()
226+
self.assertEqual(self._cc_metric_for(content), 8)
227+
184228
def _cc_metric_for(self, content: str) -> int:
185229
return CCMetric().value(
186230
AST.build_from_javalang(

0 commit comments

Comments
 (0)