Skip to content

Commit

Permalink
blockPredicate() without name. Updated tests to work without name.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jun 26, 2024
1 parent e30ade8 commit c049d06
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ protected boolean attributePredicateWithoutName(Attribute attribute) {
}

protected boolean blockPredicate(Block block) {
String blockCodeElementTypeName = block.getComposite().getLocationInfo().getCodeElementType().getName();
if(blockCodeElementTypeName != null) {
return blockCodeElementTypeName.equals(name) &&
block.getComposite().getLocationInfo().getStartLine() == lineNumber &&
block.getComposite().getLocationInfo().getEndLine() >= lineNumber;
}
return block.getComposite().getLocationInfo().getStartLine() == lineNumber &&
block.getComposite().getLocationInfo().getEndLine() >= lineNumber;
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/codetracker/util/CodeElementLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,22 @@ public void testAnonymousClassMethodParameterLocator() throws Exception {
}
}

@Test
public void testStatementLocator() throws Exception {
GitService gitService = new GitServiceImpl();
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 392;
try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle",
"https://github.com/checkstyle/checkstyle.git")){
CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
}
}

@Test
public void testForLocator() throws Exception {
GitService gitService = new GitServiceImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,26 @@ public void testAnonymousClassMethodParameterLocator() throws Exception {
assertEquals(codeElement.getClass(), Variable.class);
}

@Test
public void testStatementLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 392;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
}

@Test
public void testForLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "for";
final int lineNumber = 387;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -289,9 +301,8 @@ public void testNesterForLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "for";
final int lineNumber = 391;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -303,9 +314,8 @@ public void testNestedIfLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "if";
final int lineNumber = 389;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -317,9 +327,8 @@ public void testIfLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "if";
final int lineNumber = 396;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -331,9 +340,8 @@ public void testIfLocator2() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Main.java";
final String commitId = "b1b49751d38af0bf2476aea1f4595283615ab7de";
final String name = "if";
final int lineNumber = 231;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -345,9 +353,8 @@ public void testTryLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "try";
final int lineNumber = 317;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -359,9 +366,8 @@ public void testIfInsideCatchLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "if";
final int lineNumber = 331;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -373,9 +379,8 @@ public void testForInsideTryLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "for";
final int lineNumber = 319;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -387,9 +392,8 @@ public void testElseIfLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "if";
final int lineNumber = 471;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand All @@ -401,9 +405,8 @@ public void testIfInsideAnonymousLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/gui/TreeTable.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final String name = "if";
final int lineNumber = 119;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber);
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Block.class);
Expand Down

0 comments on commit c049d06

Please sign in to comment.