@Test public void testGetCommentMeasures() { final String[] commentText = {"/**", " * Creates new instance.", " * @param text the lines that make up the comment.", " * @param firstCol number of the first column of the comment.", " * @param lastLine number of the last line of the comment.", " * @param lastCol number of the last column of the comment.", " */"}; final Comment comment = new Comment(commentText, 5, 49, 66); Assert.assertEquals("Invalid comment start line number", 43, comment.getStartLineNo()); Assert.assertEquals("Invalid comment start column number", 5, comment.getStartColNo()); Assert.assertEquals("Invalid comment end line number", 49, comment.getEndLineNo()); Assert.assertEquals("Invalid comment end column number", 66, comment.getEndColNo()); }
@Test public void testIntersects() { final String[] commentText = {"// compute a single number for start and end", "// to simplify conditional logic"}; final Comment comment = new Comment(commentText, 9, 89, 53); Assert.assertTrue("Invalid intersection result", comment.intersects(89, 9, 89, 41)); Assert.assertTrue("Invalid intersection result", comment.intersects(89, 53, 90, 50)); Assert.assertTrue("Invalid intersection result", comment.intersects(87, 7, 88, 9)); Assert.assertFalse("Invalid intersection result", comment.intersects(90, 7, 91, 20)); Assert.assertFalse("Invalid intersection result", comment.intersects(89, 56, 89, 80)); }
/** * Report the location of a single line comment. * @param startLineNo the starting line number * @param startColNo the starting column number **/ public void reportSingleLineComment(int startLineNo, int startColNo) { final String line = line(startLineNo - 1); final String[] txt = {line.substring(startColNo)}; final Comment comment = new Comment(txt, startColNo, startLineNo, line.length() - 1); cppComments.put(startLineNo, comment); }
/** * Report the location of a block comment. * @param startLineNo the starting line number * @param startColNo the starting column number * @param endLineNo the ending line number * @param endColNo the ending column number **/ private void reportBlockComment(int startLineNo, int startColNo, int endLineNo, int endColNo) { final String[] cComment = extractBlockComment(startLineNo, startColNo, endLineNo, endColNo); final Comment comment = new Comment(cComment, startColNo, endLineNo, endColNo); // save the comment if (clangComments.containsKey(startLineNo)) { final List<TextBlock> entries = clangComments.get(startLineNo); entries.add(comment); } else { final List<TextBlock> entries = new ArrayList<>(); entries.add(comment); clangComments.put(startLineNo, entries); } // Remember if possible Javadoc comment final String firstLine = line(startLineNo - 1); if (firstLine.contains("/**") && !firstLine.contains("/**/")) { javadocComments.put(endLineNo - 1, comment); } }
@Test public void testGetJavadocBefore() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" "))); final Map<Integer, TextBlock> javadoc = new HashMap<>(); javadoc.put(0, new Comment(new String[] {"// "}, 2, 1, 2)); Whitebox.setInternalState(fileContents, "javadocComments", javadoc); final TextBlock javadocBefore = fileContents.getJavadocBefore(2); assertEquals("Invalid before javadoc", new Comment(new String[] {"// "}, 2, 1, 2).toString(), javadocBefore.toString()); }
@Test public void testBlockTag() { final String[] text = { "/** @see elsewhere ", " */", }; final Comment comment = new Comment(text, 1, 4, text[1].length()); final JavadocTags allTags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.ALL); assertEquals("Invalid valid tags size", 1, allTags.getValidTags().size()); }
@Test public void testDeprecatedAbbreviatedMethod() { // just to make UT coverage 100% final FileContents fileContents = new FileContents("filename", "123", "456"); fileContents.reportCppComment(1, 1); fileContents.reportCComment(1, 1, 1, 1); final Comment cppComment = new Comment(new String[] {"23"}, 1, 1, 2); final Comment cComment = new Comment(new String[] {"2"}, 1, 1, 1); assertEquals("Invalid cpp comment", cppComment.toString(), fileContents.getCppComments().get(1).toString()); assertEquals("Invalid c comment", cComment.toString(), fileContents.getCComments().get(1).get(0).toString()); }
@Test public void testTags() { final String[] text = { "/** @see elsewhere ", " * {@link List }, {@link List link text }", " {@link List#add(Object) link text}", " * {@link Class link text}", }; final Comment comment = new Comment(text, 1, 4, text[3].length()); final JavadocTags allTags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.ALL); assertEquals("Invalid valid tags size", 5, allTags.getValidTags().size()); }
@Test public void testReportComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportCComment(1, 2, 1, 2); final Map<Integer, List<TextBlock>> comments = fileContents.getCComments(); assertEquals("Invalid comment", new Comment(new String[] {"/"}, 2, 1, 2).toString(), comments.get(1).get(0).toString()); }
@Test public void testInlineTagLinkText() { final String[] text = { "/** {@link List link text }", }; final Comment comment = new Comment(text, 1, 1, text[0].length()); final List<JavadocTag> tags = JavadocUtil.getJavadocTags( comment, JavadocUtil.JavadocTagType.ALL).getValidTags(); assertEquals("Invalid first arg", "List link text", tags.get(0).getFirstArg()); }
@Test public void testReportCppComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" // "))); fileContents.reportCppComment(1, 2); final Map<Integer, TextBlock> cppComments = fileContents.getCppComments(); assertEquals("Invalid comment", new Comment(new String[] {" // "}, 2, 1, 6).toString(), cppComments.get(1).toString()); }
@Test public void testInlineTagMethodRef() { final String[] text = { "/** {@link List#add(Object)}", }; final Comment comment = new Comment(text, 1, 1, text[0].length()); final List<JavadocTag> tags = JavadocUtil.getJavadocTags( comment, JavadocUtil.JavadocTagType.ALL).getValidTags(); assertEquals("Invalid first arg", "List#add(Object)", tags.get(0).getFirstArg()); }
@Test public void testReportJavadocComment() { final FileContents fileContents = new FileContents( new FileText(new File("filename"), Collections.singletonList(" /** */ "))); fileContents.reportCComment(1, 2, 1, 6); final TextBlock comment = fileContents.getJavadocBefore(2); assertEquals("Invalid comment", new Comment(new String[] {"/** *"}, 2, 1, 6).toString(), comment.toString()); }
@Test public void testInlineTagPositions() { final String[] text = {"/** Also {@link Name value} */"}; final Comment comment = new Comment(text, 1, 0, text[0].length()); final List<JavadocTag> tags = JavadocUtil.getJavadocTags( comment, JavadocUtil.JavadocTagType.INLINE).getValidTags(); assertEquals("Invalid tags size", 1, tags.size()); assertEquals("Unexpected line number", 0, tags.get(0).getLineNo()); assertEquals("Unexpected column number", 10, tags.get(0).getColumnNo()); }
@Test public void testReportJavadocComment2() { final FileContents fileContents = new FileContents("filename", " /** */ "); fileContents.reportCComment(1, 2, 1, 6); final TextBlock comment = fileContents.getJavadocBefore(2); assertEquals("Invalid comment", new Comment(new String[] {"/** *"}, 2, 1, 6).toString(), comment.toString()); }
@Test public void testTagType() { final String[] text = { "/** @see block", " * {@link List inline}, {@link List#add(Object)}", }; final Comment comment = new Comment(text, 1, 2, text[1].length()); final JavadocTags blockTags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.BLOCK); final JavadocTags inlineTags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.INLINE); assertEquals("Invalid valid tags size", 1, blockTags.getValidTags().size()); assertEquals("Invalid valid tags size", 2, inlineTags.getValidTags().size()); }
@Test public void testInvalidTags() { final String[] text = { "/** @fake block", " * {@bogus inline}", " * {@link List valid}", }; final Comment comment = new Comment(text, 1, 3, text[2].length()); final JavadocTags allTags = JavadocUtil.getJavadocTags(comment, JavadocUtil.JavadocTagType.ALL); assertEquals("Unexpected invalid tags size", 2, allTags.getInvalidTags().size()); assertTag("Unexpected invalid tag", new InvalidJavadocTag(1, 4, "fake"), allTags.getInvalidTags().get(0)); assertTag("Unexpected invalid tag", new InvalidJavadocTag(2, 4, "bogus"), allTags.getInvalidTags().get(1)); assertEquals("Unexpected valid tags size", 1, allTags.getValidTags().size()); assertTag("Unexpected valid tag", new JavadocTag(3, 4, "link", "List valid"), allTags.getValidTags().get(0)); }
@Test public void testTagPositions() { final String[] text = { "/** @see elsewhere", " also {@link Name value} */", }; final Comment comment = new Comment(text, 1, 2, text[1].length()); final List<JavadocTag> tags = JavadocUtil.getJavadocTags( comment, JavadocUtil.JavadocTagType.ALL).getValidTags(); assertEquals("Invalid tags size", 2, tags.size()); final JavadocTag seeTag = tags.get(0); assertEquals("Invalid tag name", JavadocTagInfo.SEE.getName(), seeTag.getTagName()); assertEquals("Invalid line number", 1, seeTag.getLineNo()); assertEquals("Invalid column number", 4, seeTag.getColumnNo()); final JavadocTag linkTag = tags.get(1); assertEquals("Invalid tag name", JavadocTagInfo.LINK.getName(), linkTag.getTagName()); assertEquals("Invalid line number", 2, linkTag.getLineNo()); assertEquals("Invalid column number", 10, linkTag.getColumnNo()); }
/** * Report the location of a single line comment. * @param startLineNo the starting line number * @param startColNo the starting column number **/ public void reportSingleLineComment(int startLineNo, int startColNo) { final String line = line(startLineNo - 1); final String[] txt = {line.substring(startColNo)}; final Comment comment = new Comment(txt, startColNo, startLineNo, line.length() - 1); cppComments.put(startLineNo, comment); }