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.checks;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.TrailingCommentCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class TrailingCommentCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/trailingcomment";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final TrailingCommentCheck checkObj = new TrailingCommentCheck();
41          final int[] expected = {TokenTypes.SINGLE_LINE_COMMENT,
42              TokenTypes.BLOCK_COMMENT_BEGIN, };
43          assertWithMessage("Required tokens array is not empty")
44                  .that(checkObj.getRequiredTokens())
45                  .isEqualTo(expected);
46      }
47  
48      @Test
49      public void testGetAcceptableTokens() {
50          final TrailingCommentCheck checkObj = new TrailingCommentCheck();
51          final int[] expected = {TokenTypes.SINGLE_LINE_COMMENT,
52              TokenTypes.BLOCK_COMMENT_BEGIN, };
53          assertWithMessage("Acceptable tokens array is not empty")
54                  .that(checkObj.getAcceptableTokens())
55                  .isEqualTo(expected);
56      }
57  
58      @Test
59      public void testDefaults() throws Exception {
60          final String[] expected = {
61              "13:12: " + getCheckMessage(MSG_KEY),
62              "17:12: " + getCheckMessage(MSG_KEY),
63              "19:22: " + getCheckMessage(MSG_KEY),
64              "30:19: " + getCheckMessage(MSG_KEY),
65              "31:21: " + getCheckMessage(MSG_KEY),
66              "42:50: " + getCheckMessage(MSG_KEY),
67              "44:51: " + getCheckMessage(MSG_KEY),
68              "46:31: " + getCheckMessage(MSG_KEY),
69          };
70          verifyWithInlineConfigParser(
71                  getPath("InputTrailingComment.java"), expected);
72      }
73  
74      @Test
75      public void testLegalComment() throws Exception {
76          final String[] expected = {
77              "13:12: " + getCheckMessage(MSG_KEY),
78              "17:12: " + getCheckMessage(MSG_KEY),
79              "19:22: " + getCheckMessage(MSG_KEY),
80              "30:19: " + getCheckMessage(MSG_KEY),
81              "32:21: " + getCheckMessage(MSG_KEY),
82              "42:50: " + getCheckMessage(MSG_KEY),
83              "45:31: " + getCheckMessage(MSG_KEY),
84          };
85          verifyWithInlineConfigParser(
86                  getPath("InputTrailingComment2.java"), expected);
87      }
88  
89      @Test
90      public void testFormat() throws Exception {
91          final String[] expected = {
92              "1:1: " + getCheckMessage(MSG_KEY),
93              "12:12: " + getCheckMessage(MSG_KEY),
94              "13:5: " + getCheckMessage(MSG_KEY),
95              "14:33: " + getCheckMessage(MSG_KEY),
96              "15:39: " + getCheckMessage(MSG_KEY),
97              "16:22: " + getCheckMessage(MSG_KEY),
98              "21:44: " + getCheckMessage(MSG_KEY),
99              "22:7: " + getCheckMessage(MSG_KEY),
100             "23:5: " + getCheckMessage(MSG_KEY),
101             "26:19: " + getCheckMessage(MSG_KEY),
102             "27:36: " + getCheckMessage(MSG_KEY),
103             "34:5: " + getCheckMessage(MSG_KEY),
104             "37:50: " + getCheckMessage(MSG_KEY),
105             "38:62: " + getCheckMessage(MSG_KEY),
106             "39:31: " + getCheckMessage(MSG_KEY),
107             "42:39: " + getCheckMessage(MSG_KEY),
108             "43:9: " + getCheckMessage(MSG_KEY),
109             "51:5: " + getCheckMessage(MSG_KEY),
110         };
111         verifyWithInlineConfigParser(
112                 getPath("InputTrailingComment3.java"), expected);
113     }
114 
115     @Test
116     public void testLegalCommentWithNoPrecedingWhitespace() throws Exception {
117         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
118 
119         verifyWithInlineConfigParser(
120                 getPath("InputTrailingCommentWithNoPrecedingWhitespace.java"), expected);
121     }
122 
123     @Test
124     public void testWithEmoji() throws Exception {
125         final String[] expected = {
126             "13:24: " + getCheckMessage(MSG_KEY),
127             "15:27: " + getCheckMessage(MSG_KEY),
128             "21:33: " + getCheckMessage(MSG_KEY),
129             "25:13: " + getCheckMessage(MSG_KEY),
130             "27:16: " + getCheckMessage(MSG_KEY),
131             "28:24: " + getCheckMessage(MSG_KEY),
132             "33:37: " + getCheckMessage(MSG_KEY),
133         };
134         verifyWithInlineConfigParser(
135             getPath("InputTrailingCommentWithEmoji.java"), expected);
136     }
137 }