Skip to content

Commit

Permalink
Comment locator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 6, 2024
1 parent 0a43751 commit 74c8a79
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/test/java/org/codetracker/util/CodeElementLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.codetracker.element.Attribute;
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Method;
import org.codetracker.element.Variable;
import org.eclipse.jgit.lib.Repository;
Expand Down Expand Up @@ -712,4 +713,40 @@ public void testClosingBracketClassLocator() throws Exception {
assertTrue(((Class)codeElement).isClosingCurlyBracket());
}
}
// Comment tests
@Test
public void testCommentInMethodLocator() throws Exception {
GitService gitService = new GitServiceImpl();
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Main.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 660;
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(), Comment.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.LINE_COMMENT);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
assertEquals(((Comment)codeElement).getOperation().get().getName(), "listFiles");
}
}

@Test
public void testCommentInInnerClassMethodLocator() throws Exception {
GitService gitService = new GitServiceImpl();
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 607;
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(), Comment.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.LINE_COMMENT);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
assertEquals(((Comment)codeElement).getOperation().get().getName(), "startElement");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.codetracker.element.Attribute;
import org.codetracker.element.Block;
import org.codetracker.element.Class;
import org.codetracker.element.Comment;
import org.codetracker.element.Method;
import org.codetracker.element.Variable;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -583,4 +584,34 @@ public void testClosingBracketClassLocator() throws Exception {
assertEquals(codeElement.getLocation().getStartLine(), 60);
assertTrue(((Class)codeElement).isClosingCurlyBracket());
}
// Comment tests
@Test
public void testCommentInMethodLocator() 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 = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 660;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Comment.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.LINE_COMMENT);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
assertEquals(((Comment)codeElement).getOperation().get().getName(), "listFiles");
}

@Test
public void testCommentInInnerClassMethodLocator() throws Exception {
final String cloneURL = "https://github.com/checkstyle/checkstyle.git";
final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java";
final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3";
final int lineNumber = 607;
CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber);
CodeElement codeElement = locator.locate();
assertNotNull(codeElement);
assertEquals(codeElement.getClass(), Comment.class);
assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.LINE_COMMENT);
assertEquals(codeElement.getLocation().getStartLine(), lineNumber);
assertEquals(((Comment)codeElement).getOperation().get().getName(), "startElement");
}
}

0 comments on commit 74c8a79

Please sign in to comment.