Skip to content

Commit

Permalink
added 2 tests to check that if conditions inside an else block are be…
Browse files Browse the repository at this point in the history
…ing parsed correctly.
  • Loading branch information
Ahmedfir committed Jun 12, 2024
1 parent 4c44bd6 commit 08c8b71
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CliRequestTest {

private static final String file_with_if = "src/test/resources/javafile/DummyClassWithIf.java";
private static final String file_with_else_if = "src/test/resources/javafile/DummyClassWithElseIf.java";
private static final String file_with_nested_else_if = "src/test/resources/javafile/DummyClassWithNestedElseIf.java";
private static final String file_1 = "src/test/resources/javafile/ArgumentImpl.java";
private static final String nested_if_conditions = "src/test/resources/javafile/DummyClassWithIfNestedCdt.java";
private static final String lines_1_str = "109@115@124@126";
Expand Down Expand Up @@ -67,10 +68,10 @@ public void setUp() throws Exception {

private File[] getOutputAndExpectedFiles(String dir) throws IOException {
Path expectDir = expectedDir.resolve(dir);
//assertTrue(expectDir.toFile().isDirectory());
assertTrue(expectDir.toFile().isDirectory());
Path expectedJson = expectDir.resolve(LocationsCollector.DEFAULT_JSON_LOCATIONS_FILE_NAME);
File expectedFile = expectedJson.toFile();
//assertTrue(expectedFile.isFile());
assertTrue(expectedFile.isFile());

Path outDir = outputDir.resolve(dir);
Files.createDirectories(outDir);
Expand Down Expand Up @@ -201,6 +202,78 @@ public void sys_test__process_1_if_nested_conditions() throws IOException {
}


@Test
public void sys_test__nested_else_if__mask_full_cdt_disabled() throws IOException {
File[] files = getOutputAndExpectedFiles("sys_test__nested_else_if__mask_full_cdt_disabled");
File expectedFile = files[0];
File outDir = files[1];
File outFile = files[2];

String[] req = {"-in=" + file_with_nested_else_if + "::", "-out=" + outDir};
IF_CONDITIONS_AS_TKN = false;
CliRequest cliRequest = CliRequest.parseArgs(req);
LocationsCollector locator = cliRequest.start();
List<FileLocations> fileLocations = locator.getItems();
assertTrue("nothing parse!", fileLocations != null && !fileLocations.isEmpty());
assertEquals("wrong files parsed!", 1, fileLocations.size());
FileLocations fileLocation = fileLocations.get(0);
assertEquals(file_with_nested_else_if, fileLocation.getFile_path());
assertEquals(1, fileLocation.getClassPredictions().size());
ClassLocations cp = fileLocation.getClassPredictions().get(0);
assertEquals("DummyClassWithElseIf", cp.getQualifiedName());
assertEquals(1, cp.getMethodPredictions().size());
MethodLocations methodP = cp.getMethodPredictions().get(0);
assertEquals(2, methodP.getStartLineNumber());
assertEquals(9, methodP.getEndLineNumber());
assertEquals("meth(int,int)", methodP.getMethodSignature());
assertEquals(new CodePosition(40, 198), methodP.getCodePosition());
assertEquals(3, methodP.getLine_predictions().size());
LineLocations lp1 = methodP.getLine_predictions().get(0);
LineLocations lp2 = methodP.getLine_predictions().get(1);
assertEquals(3, lp1.getLine_number());
assertEquals(5, lp2.getLine_number());
assertEquals(1, lp1.getLocations().size());
assertEquals(3, lp2.getLocations().size());
assertTrue("The files differ!", FileUtils.contentEquals(expectedFile, outFile));
}


@Test
public void sys_test__nested_else_if__mask_full_cdt_enabled() throws IOException {
File[] files = getOutputAndExpectedFiles("sys_test__nested_else_if__mask_full_cdt_enabled");
File expectedFile = files[0];
File outDir = files[1];
File outFile = files[2];

String[] req = {"-in=" + file_with_nested_else_if + "::", "-out=" + outDir};
IF_CONDITIONS_AS_TKN = true;
CliRequest cliRequest = CliRequest.parseArgs(req);
LocationsCollector locator = cliRequest.start();
List<FileLocations> fileLocations = locator.getItems();
assertTrue("nothing parse!", fileLocations != null && !fileLocations.isEmpty());
assertEquals("wrong files parsed!", 1, fileLocations.size());
FileLocations fileLocation = fileLocations.get(0);
assertEquals(file_with_nested_else_if, fileLocation.getFile_path());
assertEquals(1, fileLocation.getClassPredictions().size());
ClassLocations cp = fileLocation.getClassPredictions().get(0);
assertEquals("DummyClassWithElseIf", cp.getQualifiedName());
assertEquals(1, cp.getMethodPredictions().size());
MethodLocations methodP = cp.getMethodPredictions().get(0);
assertEquals(2, methodP.getStartLineNumber());
assertEquals(9, methodP.getEndLineNumber());
assertEquals("meth(int,int)", methodP.getMethodSignature());
assertEquals(new CodePosition(40, 198), methodP.getCodePosition());
assertEquals(3, methodP.getLine_predictions().size());
LineLocations lp1 = methodP.getLine_predictions().get(0);
LineLocations lp2 = methodP.getLine_predictions().get(1);
assertEquals(3, lp1.getLine_number());
assertEquals(5, lp2.getLine_number());
assertEquals(1, lp1.getLocations().size());
assertEquals(4, lp2.getLocations().size());
assertTrue("The files differ!", FileUtils.contentEquals(expectedFile, outFile));
}


@Test
public void sys_test__else_if__mask_full_cdt_disabled() throws IOException {
File[] files = getOutputAndExpectedFiles("sys_test__else_if__mask_full_cdt_disabled");
Expand Down Expand Up @@ -276,6 +349,7 @@ public void sys_test__else_if__mask_full_cdt_enabled() throws IOException {
// assertTrue("The files differ!", FileUtils.contentEquals(expectedFile, outFile));
}


@AfterClass
public static void afterClass() throws Exception {
FileUtils.deleteDirectory(outputDir.toFile());
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"file_path":"src/test/resources/javafile/DummyClassWithIfNestedCdt.java","classPredictions":[{"qualifiedName":"DummyClassWithIf","methodPredictions":[{"startLineNumber":2,"endLineNumber":4,"methodSignature":"meth(int,int)","codePosition":{"startPosition":36,"endPosition":114},"line_predictions":[{"line_number":3,"locations":[{"node":"AND","codePosition":{"startPosition":88,"endPosition":91},"nodeType":"CtBinaryOperatorImpl","firstMutantId":0,"operator":"BinaryOperatorMutator","suffix":""},{"node":"(a \u003c 10) \u0026\u0026 (b \u003e 11)","codePosition":{"startPosition":80,"endPosition":99},"nodeType":"CtBinaryOperatorImpl","firstMutantId":5,"operator":"IfConditionReferenceLocation","suffix":""},{"node":"LT","codePosition":{"startPosition":82,"endPosition":84},"nodeType":"CtBinaryOperatorImpl","firstMutantId":10,"operator":"BinaryOperatorMutator","suffix":""},{"node":"a","codePosition":{"startPosition":81,"endPosition":81},"nodeType":"CtVariableReadImpl","firstMutantId":15,"operator":"LiteralMutator","suffix":""},{"node":"10","codePosition":{"startPosition":85,"endPosition":86},"nodeType":"CtLiteralImpl","firstMutantId":20,"operator":"LiteralMutator","suffix":""},{"node":"GT","codePosition":{"startPosition":94,"endPosition":96},"nodeType":"CtBinaryOperatorImpl","firstMutantId":25,"operator":"BinaryOperatorMutator","suffix":""},{"node":"b","codePosition":{"startPosition":93,"endPosition":93},"nodeType":"CtVariableReadImpl","firstMutantId":30,"operator":"LiteralMutator","suffix":""},{"node":"11","codePosition":{"startPosition":97,"endPosition":98},"nodeType":"CtLiteralImpl","firstMutantId":35,"operator":"LiteralMutator","suffix":""}]}]}]}]}]
[{"file_path":"src/test/resources/javafile/DummyClassWithNestedElseIf.java","classPredictions":[{"qualifiedName":"DummyClassWithElseIf","methodPredictions":[{"startLineNumber":2,"endLineNumber":9,"methodSignature":"meth(int,int)","codePosition":{"startPosition":40,"endPosition":198},"line_predictions":[{"line_number":3,"locations":[{"node":"true","codePosition":{"startPosition":84,"endPosition":87},"nodeType":"CtLiteralImpl","firstMutantId":0,"operator":"LiteralMutator","suffix":""}]},{"line_number":5,"locations":[{"node":"GT","codePosition":{"startPosition":132,"endPosition":134},"nodeType":"CtBinaryOperatorImpl","firstMutantId":5,"operator":"BinaryOperatorMutator","suffix":""},{"node":"a \u003e b","codePosition":{"startPosition":131,"endPosition":135},"nodeType":"CtBinaryOperatorImpl","firstMutantId":10,"operator":"IfConditionReferenceLocation","suffix":""},{"node":"a","codePosition":{"startPosition":131,"endPosition":131},"nodeType":"CtVariableReadImpl","firstMutantId":15,"operator":"LiteralMutator","suffix":""},{"node":"b","codePosition":{"startPosition":135,"endPosition":135},"nodeType":"CtVariableReadImpl","firstMutantId":20,"operator":"LiteralMutator","suffix":""}]},{"line_number":6,"locations":[{"node":"PLUS","codePosition":{"startPosition":164,"endPosition":166},"nodeType":"CtBinaryOperatorImpl","firstMutantId":25,"operator":"BinaryOperatorMutator","suffix":""},{"node":"a","codePosition":{"startPosition":163,"endPosition":163},"nodeType":"CtVariableReadImpl","firstMutantId":30,"operator":"LiteralMutator","suffix":""},{"node":"b","codePosition":{"startPosition":167,"endPosition":167},"nodeType":"CtVariableReadImpl","firstMutantId":35,"operator":"LiteralMutator","suffix":""}]}]}]}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"file_path":"src/test/resources/javafile/DummyClassWithNestedElseIf.java","classPredictions":[{"qualifiedName":"DummyClassWithElseIf","methodPredictions":[{"startLineNumber":2,"endLineNumber":9,"methodSignature":"meth(int,int)","codePosition":{"startPosition":40,"endPosition":198},"line_predictions":[{"line_number":3,"locations":[{"node":"true","codePosition":{"startPosition":84,"endPosition":87},"nodeType":"CtLiteralImpl","firstMutantId":0,"operator":"LiteralMutator","suffix":""}]},{"line_number":5,"locations":[{"node":"GT","codePosition":{"startPosition":132,"endPosition":134},"nodeType":"CtBinaryOperatorImpl","firstMutantId":5,"operator":"BinaryOperatorMutator","suffix":""},{"node":"a","codePosition":{"startPosition":131,"endPosition":131},"nodeType":"CtVariableReadImpl","firstMutantId":10,"operator":"LiteralMutator","suffix":""},{"node":"b","codePosition":{"startPosition":135,"endPosition":135},"nodeType":"CtVariableReadImpl","firstMutantId":15,"operator":"LiteralMutator","suffix":""}]},{"line_number":6,"locations":[{"node":"PLUS","codePosition":{"startPosition":164,"endPosition":166},"nodeType":"CtBinaryOperatorImpl","firstMutantId":20,"operator":"BinaryOperatorMutator","suffix":""},{"node":"a","codePosition":{"startPosition":163,"endPosition":163},"nodeType":"CtVariableReadImpl","firstMutantId":25,"operator":"LiteralMutator","suffix":""},{"node":"b","codePosition":{"startPosition":167,"endPosition":167},"nodeType":"CtVariableReadImpl","firstMutantId":30,"operator":"LiteralMutator","suffix":""}]}]}]}]}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"file_path":"src/test/resources/javafile/DummyClassWithNestedElseIf.java","classPredictions":[{"qualifiedName":"DummyClassWithElseIf","methodPredictions":[{"startLineNumber":2,"endLineNumber":9,"methodSignature":"meth(int,int)","codePosition":{"startPosition":40,"endPosition":198},"line_predictions":[{"line_number":3,"locations":[{"node":"true","codePosition":{"startPosition":84,"endPosition":87},"nodeType":"CtLiteralImpl","firstMutantId":0,"operator":"LiteralMutator","suffix":""}]},{"line_number":5,"locations":[{"node":"GT","codePosition":{"startPosition":132,"endPosition":134},"nodeType":"CtBinaryOperatorImpl","firstMutantId":5,"operator":"BinaryOperatorMutator","suffix":""},{"node":"a \u003e b","codePosition":{"startPosition":131,"endPosition":135},"nodeType":"CtBinaryOperatorImpl","firstMutantId":10,"operator":"IfConditionReferenceLocation","suffix":""},{"node":"a","codePosition":{"startPosition":131,"endPosition":131},"nodeType":"CtVariableReadImpl","firstMutantId":15,"operator":"LiteralMutator","suffix":""},{"node":"b","codePosition":{"startPosition":135,"endPosition":135},"nodeType":"CtVariableReadImpl","firstMutantId":20,"operator":"LiteralMutator","suffix":""}]},{"line_number":6,"locations":[{"node":"PLUS","codePosition":{"startPosition":164,"endPosition":166},"nodeType":"CtBinaryOperatorImpl","firstMutantId":25,"operator":"BinaryOperatorMutator","suffix":""},{"node":"a","codePosition":{"startPosition":163,"endPosition":163},"nodeType":"CtVariableReadImpl","firstMutantId":30,"operator":"LiteralMutator","suffix":""},{"node":"b","codePosition":{"startPosition":167,"endPosition":167},"nodeType":"CtVariableReadImpl","firstMutantId":35,"operator":"LiteralMutator","suffix":""}]}]}]}]}]
10 changes: 10 additions & 0 deletions src/test/resources/javafile/DummyClassWithNestedElseIf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class DummyClassWithElseIf {
public int meth(int a, int b) {
if (true) return a;
else {
if (a > b) {
return a + b;
}
}
}
}

0 comments on commit 08c8b71

Please sign in to comment.