-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclomaticComplexityVisitorTest.java
More file actions
115 lines (100 loc) · 3.33 KB
/
Copy pathCyclomaticComplexityVisitorTest.java
File metadata and controls
115 lines (100 loc) · 3.33 KB
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
package de.uni_passau.fim.se2.sa.readability.utils;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CyclomaticComplexityVisitorTest {
private JavaParser parser;
@BeforeEach
public void setUp() {
parser = new JavaParser();
}
/**
* Parses code, applies CyclomaticComplexityVisitor to the first method found.
*/
private int computeComplexity(String methodCode) {
String classWrapper = "class Test { " + methodCode + " }";
CompilationUnit cu = parser.parse(classWrapper).getResult().orElseThrow();
MethodDeclaration method = cu.findFirst(MethodDeclaration.class).orElseThrow();
CyclomaticComplexityVisitor visitor = new CyclomaticComplexityVisitor();
method.accept(visitor, null);
return visitor.getComplexity();
}
@Test
public void testSimpleMethod_NoControlFlow() {
String code = "void simple() { int x = 0; x++; }";
assertEquals(1, computeComplexity(code), "Base complexity should be 1");
}
@Test
public void testIfStatement() {
String code = "void test() { if (true) {} }";
assertEquals(2, computeComplexity(code));
}
@Test
public void testLoops() {
String code = """
void test() {
for (int i = 0; i < 10; i++) {}
while (true) {}
do {} while (false);
for (String s : list) {}
}
""";
assertEquals(5, computeComplexity(code), "Each loop adds 1 to base 1");
}
@Test
public void testSwitchStatement() {
String code = """
void test(int x) {
switch (x) {
case 1: break;
case 2: break;
default: break;
}
}
""";
assertEquals(3, computeComplexity(code), "Each case except default increases complexity");
}
@Test
public void testCatchBlock() {
String code = """
void test() {
try {
int x = 1 / 0;
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
""";
assertEquals(2, computeComplexity(code));
}
@Test
public void testTernaryConditional() {
String code = "void test() { int x = (1 > 0) ? 1 : 2; }";
assertEquals(2, computeComplexity(code));
}
@Test
public void testLogicalAndOrOperators() {
String code = "void test() { if (a && b || c) {} }";
assertEquals(4, computeComplexity(code), "if + && + || => 1 + 1 + 1 + 1");
}
@Test
public void testMixedControlFlow() {
String code = """
void test(int x) {
if (x > 0 && x < 10) {
for (int i = 0; i < x; i++) {
while (i < x) {
x++;
}
}
} else {
x = (x > 5) ? 1 : 2;
}
}
""";
assertEquals(6, computeComplexity(code));
}
}