Skip to content

Commit b91691b

Browse files
committed
[FEATURE] Adding a new constructor for Sequence from a Pair. Adding also test cases for it.
1 parent b46f4bf commit b91691b

2 files changed

Lines changed: 137 additions & 5 deletions

File tree

source code/src/neo/reducecognitivecomplexity/algorithms/Sequence.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,32 @@ public class Sequence {
3232
/**
3333
* Create an empty Sequence for a given compilation unit
3434
*
35-
* @param Compilation unit sibling nodes belongs to
35+
* @param compilationUnit Compilation unit sibling nodes belongs to
3636
*/
3737
public Sequence(CompilationUnit compilationUnit) {
3838
this.siblingNodes = new ArrayList<ASTNode>();
3939
this.compilationUnit = compilationUnit;
4040
}
4141

4242
/**
43-
* Create a Sequence for the given compilation unit from a given list of sibling nodes
43+
* Create a Sequence for the given compilation unit from a given {@link Pair}
4444
*
45-
* @param Compilation unit sibling nodes belongs to
46-
* @param sibling nodes
45+
* @param compilationUnit Compilation unit {@link Pair} belongs to
46+
* @param pair {@link Pair} defining sibling nodes
47+
*/
48+
public Sequence(CompilationUnit compilationUnit, Pair pair) {
49+
this.compilationUnit = compilationUnit;
50+
51+
this.siblingNodes = new Utils.NodeFinderVisitorForGivenSelection(
52+
compilationUnit.getRoot(), pair.getA().intValue(), pair.getB() - pair.getA())
53+
.getNodes();
54+
}
55+
56+
/**
57+
* Create a Sequence for the given compilation unit from a given list of sibling nodes
58+
*
59+
* @param compilationUnit Compilation unit sibling nodes belongs to
60+
* @param siblingNodes sibling nodes
4761
*/
4862
public Sequence(CompilationUnit compilationUnit, List<ASTNode> siblingNodes) {
4963
this.compilationUnit = compilationUnit;
@@ -84,7 +98,7 @@ public CodeExtractionMetrics evaluate() {
8498
* provided {@link RefactoringCache}. Calls to this method are faster than
8599
* calling {@link evaluate(CompilationUnit compilationUnit)}.
86100
*
87-
* @param rf
101+
* @param rf refactoring cache
88102
* @return Metrics of the code extraction associated to the Sequence.
89103
*/
90104
public CodeExtractionMetrics evaluate(RefactoringCache rf) {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package test.neo.reducecognitivecomplexity.algorithms;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.io.File;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.ArrayList;
9+
10+
import org.eclipse.jdt.core.dom.CompilationUnit;
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.DisplayName;
13+
import org.junit.jupiter.api.Order;
14+
import org.junit.jupiter.api.Test;
15+
16+
import neo.reducecognitivecomplexity.algorithms.Sequence;
17+
import neo.reducecognitivecomplexity.algorithms.Pair;
18+
import neo.reducecognitivecomplexity.jdt.Utils;
19+
20+
class SequenceTest {
21+
static ArrayList<Sequence> sequence = new ArrayList<>();
22+
static ArrayList<String> expectedCode = new ArrayList<>();
23+
24+
@BeforeAll
25+
static void setUp() throws Exception {
26+
Path resourceDirectory = Paths.get("src","test","resources");
27+
String absolutePath = resourceDirectory.toFile().getAbsolutePath() + File.separatorChar;
28+
29+
ArrayList<String> javaFileName = new ArrayList<>();
30+
ArrayList<CompilationUnit> cu = new ArrayList<>();
31+
ArrayList<Pair> pair = new ArrayList<>();
32+
33+
//First test case (comments remove from source code, because they are not in the AST)
34+
javaFileName.add("EZInjection.java");
35+
pair.add(new Pair(4948,5392));
36+
expectedCode.add(
37+
"boolean print = all;\n"
38+
+ "if (!all && debugClasses.length >= 1)\n"
39+
+ "{\n"
40+
+ " for (String s : debugClasses)\n"
41+
+ " {\n"
42+
+ " if (info.split(\"\\\\.\")[0].equals(s.replaceAll(\"\\\\.\", \"/\")))\n"
43+
+ " {\n"
44+
+ " print = true;\n"
45+
+ " break;\n"
46+
+ " }\n"
47+
+ " }\n"
48+
+ "}\n"
49+
+ "if (print)\n"
50+
+ " print(\"Method call: \" + info);");
51+
52+
//Second test case (comments remove from source code, because they are not in the AST)
53+
javaFileName.add("EZInjection.java");
54+
pair.add(new Pair(11006,11302));
55+
expectedCode.add(
56+
"if (useProxy)\n"
57+
+ "{\n"
58+
+ " try\n"
59+
+ " {\n"
60+
+ " String[] split = proxy.split(\":\");\n"
61+
+ " setProxy(split[0], split[1]);\n"
62+
+ " } catch (Exception e) {\n"
63+
+ " \n"
64+
+ " }\n"
65+
+ "}\n"
66+
+ "print(\"Done setting up.\");\n"
67+
+ "setFinished();");
68+
69+
//Third test case (comments remove from source code, because they are not in the AST)
70+
javaFileName.add("FileDrop.java");
71+
pair.add(new Pair(22739,23373));
72+
expectedCode.add("c.addHierarchyListener(evt -> {\n"
73+
+ " log(out, \"FileDrop: Hierarchy changed.\");\n"
74+
+ " final Component parent = c.getParent();\n"
75+
+ " if (parent == null) {\n"
76+
+ " c.setDropTarget(null);\n"
77+
+ " log(out, \"FileDrop: Drop target cleared from component.\");\n"
78+
+ " } \n"
79+
+ " else {\n"
80+
+ " new DropTarget(c, dropListener);\n"
81+
+ " log(out, \"FileDrop: Drop target added to component.\");\n"
82+
+ " } \n"
83+
+ "}); \n"
84+
+ "if (c.getParent() != null) {\n"
85+
+ " new DropTarget(c, dropListener);\n"
86+
+ "}");
87+
88+
for (int i = 0; i < expectedCode.size(); i++)
89+
{
90+
cu.add(Utils.createCompilationUnitFromFile(absolutePath + javaFileName.get(i)));
91+
sequence.add(new Sequence(cu.get(i), pair.get(i)));
92+
}
93+
}
94+
95+
@Test
96+
@Order(1)
97+
@DisplayName ("Checking Sequence initialization")
98+
void initialization() {
99+
//Comparing code after removing spaces, \n and \t
100+
for (int i = 0; i < sequence.size(); i++)
101+
{
102+
String a = sequence.get(i).toString2();
103+
a = a.substring(1, a.length()-1); //removing [ and ] from the original string
104+
a = a.replace(",", ""); // removing the char used to split lines of code
105+
a = a.replace(" ", "");
106+
a = a.replace("\n", "");
107+
a = a.replace("\t", "");
108+
109+
String b = expectedCode.get(i);
110+
b = b.replace(" ", "");
111+
b = b.replace(",", "");
112+
b = b.replace("\n", "");
113+
b = b.replace("\t", "");
114+
115+
assertEquals(a, b);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)