View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.whitespace;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.SingleSpaceSeparatorCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class SingleSpaceSeparatorCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      public String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/whitespace/singlespaceseparator";
35      }
36  
37      @Test
38      public void testNoSpaceErrors() throws Exception {
39          verifyWithInlineConfigParser(
40                  getPath("InputSingleSpaceSeparatorNoErrors.java"),
41                  CommonUtil.EMPTY_STRING_ARRAY);
42      }
43  
44      @Test
45      public void testNoStackoverflowError() throws Exception {
46          verifyWithLimitedResources(getPath("InputSingleSpaceSeparatorNoStackoverflowError.java"),
47                  CommonUtil.EMPTY_STRING_ARRAY);
48      }
49  
50      @Test
51      public void testGetAcceptableTokens() {
52          final SingleSpaceSeparatorCheck check = new SingleSpaceSeparatorCheck();
53  
54          assertWithMessage("Invalid acceptable tokens")
55              .that(check.getAcceptableTokens())
56              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
57      }
58  
59      @Test
60      public void testSpaceErrors() throws Exception {
61          final String[] expected = {
62              "8:10: " + getCheckMessage(MSG_KEY),
63              "8:28: " + getCheckMessage(MSG_KEY),
64              "15:9: " + getCheckMessage(MSG_KEY),
65              "17:19: " + getCheckMessage(MSG_KEY),
66              "17:52: " + getCheckMessage(MSG_KEY),
67              "22:21: " + getCheckMessage(MSG_KEY),
68              "23:12: " + getCheckMessage(MSG_KEY),
69              "23:16: " + getCheckMessage(MSG_KEY),
70              "30:4: " + getCheckMessage(MSG_KEY),
71              "32:6: " + getCheckMessage(MSG_KEY),
72              "34:8: " + getCheckMessage(MSG_KEY),
73              "36:9: " + getCheckMessage(MSG_KEY),
74              "39:14: " + getCheckMessage(MSG_KEY),
75              "39:24: " + getCheckMessage(MSG_KEY),
76              "39:33: " + getCheckMessage(MSG_KEY),
77              "44:16: " + getCheckMessage(MSG_KEY),
78              "44:23: " + getCheckMessage(MSG_KEY),
79              "48:17: " + getCheckMessage(MSG_KEY),
80              "48:24: " + getCheckMessage(MSG_KEY),
81              "53:20: " + getCheckMessage(MSG_KEY),
82              "55:22: " + getCheckMessage(MSG_KEY),
83              "60:22: " + getCheckMessage(MSG_KEY),
84              "60:28: " + getCheckMessage(MSG_KEY),
85              "64:15: " + getCheckMessage(MSG_KEY),
86              "64:24: " + getCheckMessage(MSG_KEY),
87              "64:32: " + getCheckMessage(MSG_KEY),
88              "64:47: " + getCheckMessage(MSG_KEY),
89              "71:17: " + getCheckMessage(MSG_KEY),
90              "73:17: " + getCheckMessage(MSG_KEY),
91              "73:34: " + getCheckMessage(MSG_KEY),
92              "78:8: " + getCheckMessage(MSG_KEY),
93          };
94  
95          verifyWithInlineConfigParser(
96                  getPath("InputSingleSpaceSeparatorErrors.java"), expected);
97      }
98  
99      @Test
100     public void testSpaceErrorsAroundComments() throws Exception {
101         final String[] expected = {
102             "12:11: " + getCheckMessage(MSG_KEY),
103             "12:43: " + getCheckMessage(MSG_KEY),
104             "16:14: " + getCheckMessage(MSG_KEY),
105             "23:14: " + getCheckMessage(MSG_KEY),
106             "23:25: " + getCheckMessage(MSG_KEY),
107             "28:8: " + getCheckMessage(MSG_KEY),
108         };
109 
110         verifyWithInlineConfigParser(
111                 getPath("InputSingleSpaceSeparatorComments.java"), expected);
112     }
113 
114     @Test
115     public void testSpaceErrorsInChildNodes() throws Exception {
116         final String[] expected = {
117             "13:16: " + getCheckMessage(MSG_KEY),
118         };
119 
120         verifyWithInlineConfigParser(
121                 getPath("InputSingleSpaceSeparatorChildNodes.java"), expected);
122     }
123 
124     @Test
125     public void testMinColumnNo() throws Exception {
126         final String[] expected = {
127             "13:4: " + getCheckMessage(MSG_KEY),
128         };
129 
130         verifyWithInlineConfigParser(
131                 getPath("InputSingleSpaceSeparatorMinColumnNo.java"), expected);
132     }
133 
134     @Test
135     public void testWhitespaceInStartOfTheLine() throws Exception {
136         final String[] expected = {
137             "13:7: " + getCheckMessage(MSG_KEY),
138         };
139 
140         verifyWithInlineConfigParser(
141                 getPath("InputSingleSpaceSeparatorStartOfTheLine.java"), expected);
142     }
143 
144     @Test
145     public void testSpaceErrorsIfCommentsIgnored() throws Exception {
146         final String[] expected = {
147             "21:14: " + getCheckMessage(MSG_KEY),
148         };
149 
150         verifyWithInlineConfigParser(
151                 getPath("InputSingleSpaceSeparatorComments2.java"), expected);
152     }
153 
154     @Test
155     public void testEmpty() throws Exception {
156         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
157 
158         verifyWithInlineConfigParser(
159                 getPath("InputSingleSpaceSeparatorEmpty.java"), expected);
160     }
161 
162     @Test
163     public void testSpaceErrorsWithEmoji() throws Exception {
164         final String[] expected = {
165             "14:18: " + getCheckMessage(MSG_KEY),
166             "16:17: " + getCheckMessage(MSG_KEY),
167             "18:27: " + getCheckMessage(MSG_KEY),
168             "24:46: " + getCheckMessage(MSG_KEY),
169             "27:9: " + getCheckMessage(MSG_KEY),
170             "33:17: " + getCheckMessage(MSG_KEY),
171             "36:14: " + getCheckMessage(MSG_KEY),
172             "36:25: " + getCheckMessage(MSG_KEY),
173             "36:37: " + getCheckMessage(MSG_KEY),
174             "41:43: " + getCheckMessage(MSG_KEY),
175             "41:46: " + getCheckMessage(MSG_KEY),
176             "45:15: " + getCheckMessage(MSG_KEY),
177             "47:16: " + getCheckMessage(MSG_KEY),
178         };
179         verifyWithInlineConfigParser(
180                 getPath("InputSingleSpaceSeparatorWithEmoji.java"), expected);
181     }
182 
183     @Test
184     public void testSpaceErrorsAroundCommentsWithEmoji() throws Exception {
185         final String[] expected = {
186             "25:22: " + getCheckMessage(MSG_KEY),
187             "25:26: " + getCheckMessage(MSG_KEY),
188             "29:26: " + getCheckMessage(MSG_KEY),
189             "30:13: " + getCheckMessage(MSG_KEY),
190             "37:8: " + getCheckMessage(MSG_KEY),
191             "39:37: " + getCheckMessage(MSG_KEY),
192             "41:46: " + getCheckMessage(MSG_KEY),
193             "43:24: " + getCheckMessage(MSG_KEY),
194         };
195         verifyWithInlineConfigParser(
196             getPath("InputSingleSpaceSeparatorCommentsWithEmoji.java"), expected);
197     }
198 
199     @Test
200     public void testSpaceErrorsAroundForElse() throws Exception {
201         final String[] expected = {
202             "13:14: " + getCheckMessage(MSG_KEY),
203             "17:17: " + getCheckMessage(MSG_KEY),
204             "17:28: " + getCheckMessage(MSG_KEY),
205             "24:16: " + getCheckMessage(MSG_KEY),
206         };
207         verifyWithInlineConfigParser(
208             getPath("InputSingleSpaceSeparatorReservedWords.java"), expected);
209     }
210 
211 }