1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.grammar.comments;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import org.junit.jupiter.api.Test;
25
26 import com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport;
27 import com.puppycrawl.tools.checkstyle.JavaParser;
28 import com.puppycrawl.tools.checkstyle.api.Comment;
29
30 public class CommentsTest extends AbstractTreeTestSupport {
31
32 @Override
33 protected String getPackageLocation() {
34 return "com/puppycrawl/tools/checkstyle/grammar/comments";
35 }
36
37 @Test
38 public void testCompareExpectedTreeWithInput1() throws Exception {
39 verifyAst(getPath("InputComments1Ast.txt"), getPath("InputComments1.java"),
40 JavaParser.Options.WITH_COMMENTS);
41 }
42
43 @Test
44 public void testCompareExpectedTreeWithInput2() throws Exception {
45 verifyAst(getPath("InputComments2Ast.txt"), getPath("InputComments2.java"),
46 JavaParser.Options.WITH_COMMENTS);
47 }
48
49 @Test
50 public void testInputFullOfBlockComments() throws Exception {
51 verifyAst(getPath("InputFullOfBlockCommentsAst.txt"),
52 getPath("InputFullOfBlockComments.java"),
53 JavaParser.Options.WITH_COMMENTS);
54 }
55
56 @Test
57 public void testInputFullOfSinglelineComments() throws Exception {
58 verifyAst(getPath("InputFullOfSinglelineCommentsAst.txt"),
59 getPath("InputFullOfSinglelineComments.java"),
60 JavaParser.Options.WITH_COMMENTS);
61 }
62
63 @Test
64 public void testToString() {
65 final Comment comment = new Comment(new String[] {"value"}, 1, 2, 3);
66 assertWithMessage("Invalid toString result")
67 .that(comment.toString())
68 .isEqualTo("Comment[text=[value], startLineNo=2, endLineNo=2, "
69 + "startColNo=1, endColNo=3]");
70 }
71
72 @Test
73 public void testGetCommentMeasures() {
74 final String[] commentText = {"/**",
75 " * Creates new instance.",
76 " * @param text the lines that make up the comment.",
77 " * @param firstCol number of the first column of the comment.",
78 " * @param lastLine number of the last line of the comment.",
79 " * @param lastCol number of the last column of the comment.",
80 " */"};
81 final Comment comment = new Comment(commentText, 5, 49, 66);
82
83 assertWithMessage("Invalid comment start line number")
84 .that(comment.getStartLineNo())
85 .isEqualTo(43);
86 assertWithMessage("Invalid comment start column number")
87 .that(comment.getStartColNo())
88 .isEqualTo(5);
89 assertWithMessage("Invalid comment end line number")
90 .that(comment.getEndLineNo())
91 .isEqualTo(49);
92 assertWithMessage("Invalid comment end column number")
93 .that(comment.getEndColNo())
94 .isEqualTo(66);
95 }
96
97 @Test
98 public void testIntersects() {
99 final String[] commentText = {"// compute a single number for start and end",
100 "// to simplify conditional logic"};
101 final Comment comment = new Comment(commentText, 9, 89, 53);
102
103 assertWithMessage("Invalid intersection result")
104 .that(comment.intersects(89, 9, 89, 41))
105 .isTrue();
106 assertWithMessage("Invalid intersection result")
107 .that(comment.intersects(89, 53, 90, 50))
108 .isTrue();
109 assertWithMessage("Invalid intersection result")
110 .that(comment.intersects(87, 7, 88, 9))
111 .isTrue();
112 assertWithMessage("Invalid intersection result")
113 .that(comment.intersects(90, 7, 91, 20))
114 .isFalse();
115 assertWithMessage("Invalid intersection result")
116 .that(comment.intersects(89, 56, 89, 80))
117 .isFalse();
118 }
119
120 }