-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperandVisitorTest.java
More file actions
68 lines (56 loc) · 2.53 KB
/
Copy pathOperandVisitorTest.java
File metadata and controls
68 lines (56 loc) · 2.53 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
package de.uni_passau.fim.se2.sa.readability.utils;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.visitor.VoidVisitor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Unit tests for the {@link OperandVisitor} class to ensure that operands
* such as variables, literals, and identifiers are correctly counted from Java source code.
*/
public class OperandVisitorTest {
private OperandVisitor visitor;
@BeforeEach
public void initializeVisitor() {
visitor = new OperandVisitor();
}
/**
* Verifies that the visitor correctly counts a null literal and basic variable names.
*/
@Test
public void testNullLiteralAndVariableOperands() {
String source = "public class Sample { void run() { Object item = null; } }";
runVisitorOnSource(source, visitor);
Map<String, Integer> operands = visitor.getOperandsPerMethod();
assertEquals(1, operands.get("null"), "Expected one 'null' literal");
assertEquals(1, operands.get("Object"), "Expected one use of 'Object'");
assertEquals(1, operands.get("item"), "Expected one use of 'item'");
}
/**
* Tests whether method parameters and their usage within a method body are accurately tracked.
*/
@Test
public void testMethodParameterUsage() {
String source = "public class Sample { void compute(int a, String b) { int result = a + b.length(); } }";
runVisitorOnSource(source, visitor);
Map<String, Integer> operands = visitor.getOperandsPerMethod();
assertEquals(2, operands.get("a"), "'a' should be counted in declaration and usage");
assertEquals(2, operands.get("b"), "'b' should be counted in declaration and in 'b.length()'");
assertEquals(1, operands.get("result"), "'result' is assigned once");
assertEquals(1, operands.get("length"), "'length' method used once");
}
/**
* Helper function to parse Java code and run a given visitor on its AST.
*
* @param code the Java code to parse
* @param visitor the AST visitor to apply
*/
private void runVisitorOnSource(String code, VoidVisitor<Void> visitor) {
JavaParser parser = new JavaParser();
ParseResult<CompilationUnit> result = parser.parse(code);
result.getResult().ifPresent(cu -> cu.accept(visitor, null));
}
}