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.checks.indentation;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck.MSG_KEY_BLOCK;
24 import static com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck.MSG_KEY_SINGLE;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32
33 public class CommentsIndentationCheckTest extends AbstractModuleTestSupport {
34
35 @Override
36 protected String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/indentation/commentsindentation";
38 }
39
40 @Test
41 public void testCommentIsAtTheEndOfBlockOne() throws Exception {
42 final String[] expected = {
43 "25:26: " + getCheckMessage(MSG_KEY_SINGLE, 24, 25, 8),
44 "40:6: " + getCheckMessage(MSG_KEY_SINGLE, 42, 5, 4),
45 "44:1: " + getCheckMessage(MSG_KEY_SINGLE, 43, 0, 8),
46 "54:16: " + getCheckMessage(MSG_KEY_SINGLE, 53, 15, 12),
47 "56:11: " + getCheckMessage(MSG_KEY_SINGLE, 52, 10, 8),
48 "61:14: " + getCheckMessage(MSG_KEY_SINGLE, 60, 13, 8),
49 "81:19: " + getCheckMessage(MSG_KEY_SINGLE, 77, 18, 8),
50 "95:32: " + getCheckMessage(MSG_KEY_SINGLE, 92, 31, 8),
51 "107:22: " + getCheckMessage(MSG_KEY_SINGLE, 105, 21, 8),
52 };
53 final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlockOne.java";
54 verifyWithInlineConfigParser(getPath(testInputFile), expected);
55 }
56
57 @Test
58 public void testCommentIsAtTheEndOfBlockTwo() throws Exception {
59 final String[] expected = {
60 "22:30: " + getCheckMessage(MSG_KEY_SINGLE, 23, 29, 12),
61 "45:27: " + getCheckMessage(MSG_KEY_SINGLE, 38, 26, 8),
62 "71:34: " + getCheckMessage(MSG_KEY_SINGLE, 68, 33, 8),
63 "81:22: " + getCheckMessage(MSG_KEY_SINGLE, 80, 21, 8),
64 "93:35: " + getCheckMessage(MSG_KEY_SINGLE, 91, 34, 8),
65 "115:27: " + getCheckMessage(MSG_KEY_SINGLE, 113, 26, 8),
66 };
67 final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlockTwo.java";
68 verifyWithInlineConfigParser(getPath(testInputFile), expected);
69 }
70
71 @Test
72 public void testCommentIsAtTheEndOfBlockThree() throws Exception {
73 final String[] expected = {
74 "21:1: " + getCheckMessage(MSG_KEY_SINGLE, 20, 0, 8),
75 "35:13: " + getCheckMessage(MSG_KEY_SINGLE, 32, 12, 8),
76 "42:1: " + getCheckMessage(MSG_KEY_SINGLE, 40, 0, 8),
77 "56:13: " + getCheckMessage(MSG_KEY_SINGLE, 53, 12, 8),
78 "63:1: " + getCheckMessage(MSG_KEY_SINGLE, 60, 0, 8),
79 "73:15: " + getCheckMessage(MSG_KEY_SINGLE, 72, 14, 8),
80 "79:10: " + getCheckMessage(MSG_KEY_SINGLE, 77, 9, 8),
81 "85:10: " + getCheckMessage(MSG_KEY_SINGLE, 84, 9, 8),
82 };
83 final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlockThree.java";
84 verifyWithInlineConfigParser(getPath(testInputFile), expected);
85 }
86
87 @Test
88 public void testCommentIsAtTheEndOfBlockFour() throws Exception {
89 final String[] expected = {
90 "22:10: " + getCheckMessage(MSG_KEY_SINGLE, 21, 9, 8),
91 "28:1: " + getCheckMessage(MSG_KEY_SINGLE, 29, 0, 4),
92 "41:1: " + getCheckMessage(MSG_KEY_SINGLE, 38, 0, 8),
93 "60:10: " + getCheckMessage(MSG_KEY_SINGLE, 57, 9, 8),
94 "86:13: " + getCheckMessage(MSG_KEY_BLOCK, 87, 12, 8),
95 "89:13: " + getCheckMessage(MSG_KEY_SINGLE, 87, 12, 8),
96 "99:13: " + getCheckMessage(MSG_KEY_SINGLE, 98, 12, 8),
97 "106:9: " + getCheckMessage(MSG_KEY_SINGLE, 109, 8, 10),
98 };
99 final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlockFour.java";
100 verifyWithInlineConfigParser(getPath(testInputFile), expected);
101 }
102
103 @Test
104 public void testCommentIsAtTheEndOfBlockFive() throws Exception {
105 final String[] expected = {
106 "61:1: " + getCheckMessage(MSG_KEY_SINGLE, 59, 0, 8),
107 "77:11: " + getCheckMessage(MSG_KEY_BLOCK, 73, 10, 8),
108 "87:11: " + getCheckMessage(MSG_KEY_BLOCK, 81, 10, 8),
109 "95:11: " + getCheckMessage(MSG_KEY_BLOCK, 91, 10, 8),
110 "103:11: " + getCheckMessage(MSG_KEY_BLOCK, 99, 10, 8),
111 "111:11: " + getCheckMessage(MSG_KEY_BLOCK, 107, 10, 8),
112 };
113 final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlockFive.java";
114 verifyWithInlineConfigParser(getPath(testInputFile), expected);
115 }
116
117 @Test
118 public void testCommentIsAtTheEndOfBlockSix() throws Exception {
119 final String[] expected = {
120 "26:11: " + getCheckMessage(MSG_KEY_SINGLE, 19, 10, 8),
121 "33:1: " + getCheckMessage(MSG_KEY_SINGLE, 30, 0, 8),
122 "39:1: " + getCheckMessage(MSG_KEY_SINGLE, 36, 0, 8),
123 "45:1: " + getCheckMessage(MSG_KEY_SINGLE, 43, 0, 8),
124 "52:5: " + getCheckMessage(MSG_KEY_SINGLE, 48, 4, 8),
125 "56:13: " + getCheckMessage(MSG_KEY_SINGLE, 55, 12, 8),
126 "62:1: " + getCheckMessage(MSG_KEY_SINGLE, 60, 0, 8),
127 "67:1: " + getCheckMessage(MSG_KEY_SINGLE, 66, 0, 8),
128 "82:1: " + getCheckMessage(MSG_KEY_SINGLE, 79, 0, 8),
129 "94:13: " + getCheckMessage(MSG_KEY_SINGLE, 92, 12, 8),
130 };
131 final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlockSix.java";
132 verifyWithInlineConfigParser(getPath(testInputFile), expected);
133 }
134
135 @Test
136 public void testCommentIsInsideSwitchBlockOne() throws Exception {
137 final String[] expected = {
138 "27:13: " + getCheckMessage(MSG_KEY_BLOCK, 28, 12, 16),
139 "33:20: " + getCheckMessage(MSG_KEY_SINGLE, "32, 34", 19, "16, 12"),
140 "39:20: " + getCheckMessage(MSG_KEY_SINGLE, "38, 40", 19, "16, 12"),
141 "56:7: " + getCheckMessage(MSG_KEY_SINGLE, 57, 6, 16),
142 "63:9: " + getCheckMessage(MSG_KEY_SINGLE, 64, 8, 12),
143 "67:23: " + getCheckMessage(MSG_KEY_SINGLE, 66, 22, 16),
144 "76:15: " + getCheckMessage(MSG_KEY_SINGLE, "73, 77", 14, "12, 16"),
145 "96:25: " + getCheckMessage(MSG_KEY_SINGLE, 97, 24, 20),
146 };
147 final String testInputFile = "InputCommentsIndentationInSwitchBlockOne.java";
148 verifyWithInlineConfigParser(getPath(testInputFile), expected);
149 }
150
151 @Test
152 public void testCommentIsInsideSwitchBlockTwo() throws Exception {
153 final String[] expected = {
154 "18:25: " + getCheckMessage(MSG_KEY_SINGLE, 19, 24, 20),
155 "43:16: " + getCheckMessage(MSG_KEY_SINGLE, "42, 44", 15, "17, 12"),
156 "55:9: " + getCheckMessage(MSG_KEY_SINGLE, 56, 8, 12),
157 "68:5: " + getCheckMessage(MSG_KEY_SINGLE, 69, 4, 8),
158 "87:19: " + getCheckMessage(MSG_KEY_SINGLE, "86, 88", 18, "16, 12"),
159 };
160 final String testInputFile = "InputCommentsIndentationInSwitchBlockTwo.java";
161 verifyWithInlineConfigParser(getPath(testInputFile), expected);
162 }
163
164 @Test
165 public void testCommentIsInsideSwitchBlockThree() throws Exception {
166 final String[] expected = {
167 "18:25: " + getCheckMessage(MSG_KEY_SINGLE, 19, 24, 20),
168 "45:5: " + getCheckMessage(MSG_KEY_SINGLE, "44, 46", 4, "12, 12"),
169 "48:23: " + getCheckMessage(MSG_KEY_SINGLE, "47, 51", 22, "16, 12"),
170 "49:21: " + getCheckMessage(MSG_KEY_SINGLE, "47, 51", 20, "16, 12"),
171 "50:18: " + getCheckMessage(MSG_KEY_SINGLE, "47, 51", 17, "16, 12"),
172 "74:7: " + getCheckMessage(MSG_KEY_SINGLE, "73, 75", 6, "12, 12"),
173 };
174 final String testInputFile = "InputCommentsIndentationInSwitchBlockThree.java";
175 verifyWithInlineConfigParser(getPath(testInputFile), expected);
176 }
177
178 @Test
179 public void testCommentIsInsideSwitchBlockFour() throws Exception {
180 final String[] expected = {
181 "18:25: " + getCheckMessage(MSG_KEY_SINGLE, 19, 24, 20),
182 "34:12: " + getCheckMessage(MSG_KEY_BLOCK, "33, 37", 11, "16, 12"),
183 "39:12: " + getCheckMessage(MSG_KEY_SINGLE, "38, 40", 11, "16, 12"),
184 "69:1: " + getCheckMessage(MSG_KEY_SINGLE, "70", 0, "8"),
185 };
186 final String testInputFile = "InputCommentsIndentationInSwitchBlockFour.java";
187 verifyWithInlineConfigParser(getPath(testInputFile), expected);
188 }
189
190 @Test
191 public void testCommentIsInsideEmptyBlock() throws Exception {
192 final String[] expected = {
193 "16:20: " + getCheckMessage(MSG_KEY_SINGLE, 19, 19, 31),
194 "17:24: " + getCheckMessage(MSG_KEY_BLOCK, 19, 23, 31),
195 "40:1: " + getCheckMessage(MSG_KEY_SINGLE, 41, 0, 8),
196 "64:1: " + getCheckMessage(MSG_KEY_SINGLE, 65, 0, 8),
197 "78:1: " + getCheckMessage(MSG_KEY_SINGLE, 79, 0, 8),
198 "110:1: " + getCheckMessage(MSG_KEY_SINGLE, 111, 0, 8),
199 "114:1: " + getCheckMessage(MSG_KEY_SINGLE, 115, 0, 8),
200 };
201 final String testInputFile = "InputCommentsIndentationInEmptyBlock.java";
202 verifyWithInlineConfigParser(getPath(testInputFile), expected);
203 }
204
205 @Test
206 public void testSurroundingCodeOne() throws Exception {
207 final String[] expected = {
208 "20:15: " + getCheckMessage(MSG_KEY_SINGLE, 21, 14, 12),
209 "31:17: " + getCheckMessage(MSG_KEY_BLOCK, 32, 16, 12),
210 "33:17: " + getCheckMessage(MSG_KEY_BLOCK, 35, 16, 12),
211 "36:17: " + getCheckMessage(MSG_KEY_BLOCK, 39, 16, 12),
212 "58:28: " + getCheckMessage(MSG_KEY_SINGLE, 61, 27, 36),
213 "59:24: " + getCheckMessage(MSG_KEY_BLOCK, 61, 23, 36),
214 "98:15: " + getCheckMessage(MSG_KEY_SINGLE, 99, 14, 8),
215 "106:14: " + getCheckMessage(MSG_KEY_SINGLE, 108, 13, 8),
216 };
217 final String testInputFile = "InputCommentsIndentationSurroundingCodeOne.java";
218 verifyWithInlineConfigParser(getPath(testInputFile), expected);
219 }
220
221 @Test
222 public void testSurroundingCodeTwo() throws Exception {
223 final String[] expected = {
224 "20:34: " + getCheckMessage(MSG_KEY_SINGLE, 21, 33, 8),
225 "42:13: " + getCheckMessage(MSG_KEY_BLOCK, 43, 12, 8),
226 "48:5: " + getCheckMessage(MSG_KEY_BLOCK, 49, 4, 8),
227 "55:5: " + getCheckMessage(MSG_KEY_BLOCK, 53, 4, 8),
228 };
229 final String testInputFile = "InputCommentsIndentationSurroundingCodeTwo.java";
230 verifyWithInlineConfigParser(getPath(testInputFile), expected);
231 }
232
233 @Test
234 public void testNoNpeWhenBlockCommentEndsClassFile() throws Exception {
235 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
236 final String testInputFile = "InputCommentsIndentationNoNpe.java";
237 verifyWithInlineConfigParser(getPath(testInputFile), expected);
238 }
239
240 @Test
241 public void testCheckOnlySingleLineComments() throws Exception {
242 final String[] expected = {
243 "20:15: " + getCheckMessage(MSG_KEY_SINGLE, 21, 14, 12),
244 "57:28: " + getCheckMessage(MSG_KEY_SINGLE, 60, 27, 36),
245 "97:15: " + getCheckMessage(MSG_KEY_SINGLE, 98, 14, 8),
246 "105:14: " + getCheckMessage(MSG_KEY_SINGLE, 107, 13, 8),
247 "115:34: " + getCheckMessage(MSG_KEY_SINGLE, 116, 33, 8),
248 };
249 final String testInputFile = "InputCommentsIndentationSurroundingCode2One.java";
250 verifyWithInlineConfigParser(getPath(testInputFile), expected);
251 }
252
253 @Test
254 public void testCheckOnlyBlockCommentsOne() throws Exception {
255 final String[] expected = {
256 "30:17: " + getCheckMessage(MSG_KEY_BLOCK, 31, 16, 12),
257 "32:17: " + getCheckMessage(MSG_KEY_BLOCK, 34, 16, 12),
258 "35:17: " + getCheckMessage(MSG_KEY_BLOCK, 38, 16, 12),
259 "57:24: " + getCheckMessage(MSG_KEY_BLOCK, 59, 23, 36),
260 };
261 final String testInputFile = "InputCommentsIndentationSurroundingCode3One.java";
262 verifyWithInlineConfigParser(getPath(testInputFile), expected);
263 }
264
265 @Test
266 public void testCheckOnlyBlockCommentsTwo() throws Exception {
267 final String[] expected = {
268 "40:13: " + getCheckMessage(MSG_KEY_BLOCK, 41, 12, 8),
269 "46:5: " + getCheckMessage(MSG_KEY_BLOCK, 47, 4, 8),
270 "53:5: " + getCheckMessage(MSG_KEY_BLOCK, 51, 4, 8),
271 };
272 final String testInputFile = "InputCommentsIndentationSurroundingCode3Two.java";
273 verifyWithInlineConfigParser(getPath(testInputFile), expected);
274 }
275
276 @Test
277 public void testVisitToken() {
278 final CommentsIndentationCheck check = new CommentsIndentationCheck();
279 final DetailAstImpl methodDef = new DetailAstImpl();
280 methodDef.setType(TokenTypes.METHOD_DEF);
281 methodDef.setText("methodStub");
282 try {
283 check.visitToken(methodDef);
284 assertWithMessage("IllegalArgumentException should have been thrown!").fail();
285 }
286 catch (IllegalArgumentException ex) {
287 final String msg = ex.getMessage();
288 assertWithMessage("Invalid exception message")
289 .that(msg)
290 .isEqualTo("Unexpected token type: methodStub");
291 }
292 }
293
294 @Test
295 public void testJavadoc() throws Exception {
296 final String[] expected = {
297 "10:3: " + getCheckMessage(MSG_KEY_BLOCK, 13, 2, 0),
298 "16:1: " + getCheckMessage(MSG_KEY_BLOCK, 17, 0, 4),
299 "19:9: " + getCheckMessage(MSG_KEY_BLOCK, 22, 8, 4),
300 "26:11: " + getCheckMessage(MSG_KEY_BLOCK, 27, 10, 8),
301 };
302 final String testInputFile = "InputCommentsIndentationJavadoc.java";
303 verifyWithInlineConfigParser(getPath(testInputFile), expected);
304 }
305
306 @Test
307 public void testMultiblockStructuresOne() throws Exception {
308 final String[] expected = {
309 "19:9: " + getCheckMessage(MSG_KEY_SINGLE, 18, 8, 12),
310 "25:17: " + getCheckMessage(MSG_KEY_SINGLE, "24, 26", 16, "12, 8"),
311 "30:1: " + getCheckMessage(MSG_KEY_SINGLE, "29, 31", 0, "12, 8"),
312 "40:9: " + getCheckMessage(MSG_KEY_SINGLE, 39, 8, 12),
313 "46:1: " + getCheckMessage(MSG_KEY_SINGLE, "45, 47", 0, "12, 8"),
314 "51:17: " + getCheckMessage(MSG_KEY_SINGLE, "50, 52", 16, "12, 8"),
315 "61:9: " + getCheckMessage(MSG_KEY_SINGLE, 60, 8, 12),
316 "67:1: " + getCheckMessage(MSG_KEY_SINGLE, "66, 68", 0, "12, 8"),
317 "72:17: " + getCheckMessage(MSG_KEY_SINGLE, "71, 73", 16, "12, 8"),
318 "82:9: " + getCheckMessage(MSG_KEY_SINGLE, 81, 8, 12),
319 "88:1: " + getCheckMessage(MSG_KEY_SINGLE, "87, 89", 0, "12, 8"),
320 "93:17: " + getCheckMessage(MSG_KEY_SINGLE, "92, 94", 16, "12, 8"),
321 "103:9: " + getCheckMessage(MSG_KEY_SINGLE, 102, 8, 12),
322 "109:1: " + getCheckMessage(MSG_KEY_SINGLE, "108, 110", 0, "12, 8"),
323 "114:17: " + getCheckMessage(MSG_KEY_SINGLE, "113, 115", 16, "12, 8"),
324 };
325 final String testInputFile = "InputCommentsIndentationInMultiblockStructuresOne.java";
326 verifyWithInlineConfigParser(getPath(testInputFile), expected);
327 }
328
329 @Test
330 public void testMultiblockStructuresTwo() throws Exception {
331 final String[] expected = {
332 "20:9: " + getCheckMessage(MSG_KEY_SINGLE, 19, 8, 12),
333 "26:17: " + getCheckMessage(MSG_KEY_SINGLE, "25, 27", 16, "12, 8"),
334 "31:1: " + getCheckMessage(MSG_KEY_SINGLE, "30, 32", 0, "12, 8"),
335 };
336 final String testInputFile = "InputCommentsIndentationInMultiblockStructuresTwo.java";
337 verifyWithInlineConfigParser(getPath(testInputFile), expected);
338 }
339
340 @Test
341 public void testCommentsAfterAnnotation() throws Exception {
342 final String[] expected = {
343 "21:5: " + getCheckMessage(MSG_KEY_SINGLE, 22, 4, 0),
344 "25:9: " + getCheckMessage(MSG_KEY_SINGLE, 26, 8, 4),
345 "43:5: " + getCheckMessage(MSG_KEY_SINGLE, 44, 4, 0),
346 "48:9: " + getCheckMessage(MSG_KEY_SINGLE, 49, 8, 4),
347 "57:3: " + getCheckMessage(MSG_KEY_SINGLE, 58, 2, 4),
348 };
349 final String testInputFile = "InputCommentsIndentationAfterAnnotation.java";
350 verifyWithInlineConfigParser(getPath(testInputFile), expected);
351 }
352
353 @Test
354 public void testCommentsInSameMethodCallWithSameIndent() throws Exception {
355 final String[] expected = {
356 "23:7: " + getCheckMessage(MSG_KEY_SINGLE, 24, 6, 4),
357 "30:11: " + getCheckMessage(MSG_KEY_SINGLE, 31, 10, 4),
358 };
359 verifyWithInlineConfigParser(
360 getPath("InputCommentsIndentationWithInMethodCallWithSameIndent.java"),
361 expected);
362 }
363
364 @Test
365 public void testCommentIndentationWithEmoji() throws Exception {
366 final String[] expected = {
367 "14:9: " + getCheckMessage(MSG_KEY_SINGLE, 15, 8, 16),
368 "25:13: " + getCheckMessage(MSG_KEY_SINGLE, 24, 12, 8),
369 "27:9: " + getCheckMessage(MSG_KEY_SINGLE, 29, 8, 4),
370 "46:17: " + getCheckMessage(MSG_KEY_SINGLE, 45, 16, 24),
371 "70:13: " + getCheckMessage(MSG_KEY_SINGLE, 72, 12, 8),
372 "74:9: " + getCheckMessage(MSG_KEY_SINGLE, 73, 8, 12),
373 "88:13: " + getCheckMessage(MSG_KEY_SINGLE, 90, 12, 8),
374 "98:17: " + getCheckMessage(MSG_KEY_BLOCK, 99, 16, 12),
375 "100:17: " + getCheckMessage(MSG_KEY_BLOCK, 102, 16, 12),
376 "103:17: " + getCheckMessage(MSG_KEY_BLOCK, 116, 16, 12, 1),
377 };
378 verifyWithInlineConfigParser(
379 getPath("InputCommentsIndentationCheckWithEmoji.java"),
380 expected);
381 }
382
383 @Test
384 public void testCommentsBlockCommentBeforePackage() throws Exception {
385 final String[] expected = {
386 "8:1: " + getCheckMessage(MSG_KEY_BLOCK, 11, 0, 1),
387 };
388 verifyWithInlineConfigParser(
389 getPath("InputCommentsIndentationBlockCommentBeforePackage.java"),
390 expected);
391 }
392
393 @Test
394 public void testCommentsAfterRecordsAndCompactCtors() throws Exception {
395 final String[] expected = {
396 "15:17: " + getCheckMessage(MSG_KEY_SINGLE, 16, 16, 20),
397 "28:1: " + getCheckMessage(MSG_KEY_SINGLE, 29, 0, 4),
398 "31:9: " + getCheckMessage(MSG_KEY_SINGLE, 32, 8, 4),
399 "37:9: " + getCheckMessage(MSG_KEY_SINGLE, 40, 8, 5),
400 "42:9: " + getCheckMessage(MSG_KEY_SINGLE, 40, 8, 5),
401 };
402 verifyWithInlineConfigParser(
403 getNonCompilablePath("InputCommentsIndentationRecordsAndCompactCtors.java"),
404 expected);
405 }
406
407 @Test
408 public void testCommentsAtTheEndOfMethodCall() throws Exception {
409 final String[] expected = {
410 "24:16: " + getCheckMessage(MSG_KEY_SINGLE, 20, 15, 8),
411 };
412 verifyWithInlineConfigParser(
413 getPath("InputCommentsIndentationCommentsAfterMethodCall.java"),
414 expected);
415 }
416
417 }