-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclomaticComplexityFeatureTest.java
More file actions
57 lines (47 loc) · 1.88 KB
/
Copy pathCyclomaticComplexityFeatureTest.java
File metadata and controls
57 lines (47 loc) · 1.88 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
package de.uni_passau.fim.se2.sa.readability.features;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CyclomaticComplexityFeatureTest {
private CyclomaticComplexityFeature feature;
@BeforeEach
void setUp() {
feature = new CyclomaticComplexityFeature();
}
@Test
void testEmptySnippet() {
String code = "class A {}";
double complexity = feature.computeMetric(code);
assertEquals(1.0, complexity, "Empty class should have complexity 1");
}
@Test
void testSimpleMethod() {
String code = "class A { void f() { int x = 0; } }";
double complexity = feature.computeMetric(code);
assertEquals(1.0, complexity, "Simple method should have complexity 1");
}
@Test
void testIfElse() {
String code = "class A { void f() { if (true) {} else {} } }";
double complexity = feature.computeMetric(code);
assertEquals(2.0, complexity, "if-else adds one decision point");
}
@Test
void testMultipleBranches() {
String code = "class A { void f() { if (true) {} for(;;) {} while(true) {} } }";
double complexity = feature.computeMetric(code);
assertEquals(4.0, complexity, "Each control structure adds a decision point");
}
@Test
void testNestedBranches() {
String code = "class A { void f() { if (true) { for(int i=0;i<10;i++) { while(true) {} } } } }";
double complexity = feature.computeMetric(code);
assertEquals(4.0, complexity, "Nested structures add their own complexity");
}
@Test
void testLogicalOperators() {
String code = "class A { void f() { if (a && b || c) {} } }";
double complexity = feature.computeMetric(code);
assertEquals(4.0, complexity, "Each && or || adds a decision point");
}
}