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