View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.api;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  
24  import org.junit.jupiter.api.Test;
25  
26  public class CommentTest {
27  
28      @Test
29      public void test() {
30          final String[] text = {"test"};
31          final Comment comment = new Comment(text, 1, 2, 3);
32  
33          assertWithMessage("invalid text")
34                  .that(comment.getText())
35                  .isEqualTo(text);
36          assertWithMessage("invalid start line")
37                  .that(comment.getStartLineNo())
38                  .isEqualTo(2);
39          assertWithMessage("invalid end line")
40                  .that(comment.getEndLineNo())
41                  .isEqualTo(2);
42          assertWithMessage("invalid start column")
43                  .that(comment.getStartColNo())
44                  .isEqualTo(1);
45          assertWithMessage("invalid end column")
46                  .that(comment.getEndColNo())
47                  .isEqualTo(3);
48          assertWithMessage("invalid string")
49                  .that(comment.toString())
50                  .isEqualTo("Comment[text=[test], startLineNo=2,"
51                          + " endLineNo=2, startColNo=1, endColNo=3]");
52      }
53  
54      @Test
55      public void testIntersects() {
56          final String[] text = {"test", "test"};
57          final Comment comment = new Comment(text, 2, 4, 4);
58  
59          final String message = "invalid";
60          assertWithMessage(message)
61                  .that(comment.intersects(1, 1, 1, 1))
62                  .isFalse();
63          assertWithMessage(message)
64                  .that(comment.intersects(5, 5, 5, 5))
65                  .isFalse();
66          assertWithMessage(message)
67                  .that(comment.intersects(1, 1, 5, 5))
68                  .isTrue();
69          assertWithMessage(message)
70                  .that(comment.intersects(1, 1, 3, 5))
71                  .isTrue();
72          assertWithMessage(message)
73                  .that(comment.intersects(3, 5, 5, 5))
74                  .isTrue();
75      }
76  
77      @Test
78      public void testIntersects2() {
79          final String[] text = {"a"};
80          final Comment comment = new Comment(text, 2, 2, 2);
81  
82          assertWithMessage("invalid")
83                  .that(comment.intersects(2, 2, 2, 2))
84                  .isTrue();
85      }
86  
87      @Test
88      public void testIntersects3() {
89          final String[] text = {"test"};
90          final Comment comment = new Comment(text, 1, 1, 2);
91  
92          assertWithMessage("invalid")
93                  .that(comment.intersects(1, Integer.MAX_VALUE, 1, 2))
94                  .isFalse();
95      }
96  }