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.blocks;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_BREAK_AFTER;
24 import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_NEW;
25 import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_PREVIOUS;
26 import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34
35 public class LeftCurlyCheckTest extends AbstractModuleTestSupport {
36
37 @Override
38 public String getPackageLocation() {
39 return "com/puppycrawl/tools/checkstyle/checks/blocks/leftcurly";
40 }
41
42
43
44
45
46 @Test
47 public void testLeftCurlyOptionValueOf() {
48 final LeftCurlyOption option = LeftCurlyOption.valueOf("NL");
49 assertWithMessage("Invalid valueOf result")
50 .that(option)
51 .isEqualTo(LeftCurlyOption.NL);
52 }
53
54 @Test
55 public void testGetRequiredTokens() {
56 final LeftCurlyCheck checkObj = new LeftCurlyCheck();
57 assertWithMessage("LeftCurlyCheck#getRequiredTokens should return empty array by default")
58 .that(checkObj.getRequiredTokens())
59 .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
60 }
61
62 @Test
63 public void testDefault() throws Exception {
64 final String[] expected = {
65 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
66 "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
67 "23:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
68 "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
69 "31:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
70 };
71 verifyWithInlineConfigParser(
72 getPath("InputLeftCurlyTestDefault.java"), expected);
73 }
74
75 @Test
76 public void testNl() throws Exception {
77 final String[] expected = {
78 "36:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
79 "40:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
80 "45:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
81 "49:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
82 "54:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
83 "59:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
84 "64:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
85 "67:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
86 "68:23: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 23),
87 "69:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
88 };
89 verifyWithInlineConfigParser(
90 getPath("InputLeftCurlyDefaultTestNl.java"), expected);
91 }
92
93 @Test
94 public void testNlow() throws Exception {
95 final String[] expected = {
96 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
97 "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
98 "23:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
99 "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
100 "31:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
101 "36:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
102 "40:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
103 "45:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
104 "49:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
105 "54:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
106 "59:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
107 "64:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
108 };
109 verifyWithInlineConfigParser(
110 getPath("InputLeftCurlyDefaultTestNlow.java"), expected);
111 }
112
113 @Test
114 public void testDefault2() throws Exception {
115 final String[] expected = {
116 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
117 "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
118 "29:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
119 "32:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
120 "36:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
121 "44:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
122 "46:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
123 "51:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
124 "54:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
125 "58:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
126 "70:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
127 "74:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
128 "82:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
129 "85:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
130 "89:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
131 };
132 verifyWithInlineConfigParser(
133 getPath("InputLeftCurlyMethod.java"), expected);
134 }
135
136 @Test
137 public void testNewline2() throws Exception {
138 final String[] expected = {
139 "19:44: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 44),
140 "26:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
141 "39:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
142 "48:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
143 "61:35: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 35),
144 "65:17: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 17),
145 "79:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
146 "92:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
147 };
148 verifyWithInlineConfigParser(
149 getPath("InputLeftCurlyMethodTestNewLine2.java"), expected);
150 }
151
152 @Test
153 public void testDefault3Basic() throws Exception {
154 final String[] expected = {
155 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
156 "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
157 "24:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
158 "26:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
159 "28:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
160 "35:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
161 "39:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
162 "47:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
163 "51:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
164 "57:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
165 "59:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
166 "68:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
167 "80:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
168 "88:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
169 };
170 verifyWithInlineConfigParser(
171 getPath("InputLeftCurlyTestDefault3Basic.java"), expected);
172 }
173
174 @Test
175 public void testDefault3Empty() throws Exception {
176 final String[] expected = {
177 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
178 "30:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
179 };
180 verifyWithInlineConfigParser(
181 getPath("InputLeftCurlyTestDefault3Empty.java"), expected);
182 }
183
184 @Test
185 public void testDefault3Enum() throws Exception {
186 final String[] expected = {
187 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
188 "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
189 };
190 verifyWithInlineConfigParser(
191 getPath("InputLeftCurlyTestDefault3Enum.java"), expected);
192 }
193
194 @Test
195 public void testDefault3Initializer() throws Exception {
196 final String[] expected = {
197 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
198 "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
199 "26:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
200 "33:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
201 };
202 verifyWithInlineConfigParser(
203 getPath("InputLeftCurlyTestDefault3Initializer.java"), expected);
204 }
205
206 @Test
207 public void testDefault3Misc() throws Exception {
208 final String[] expected = {
209 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
210 "25:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
211 "28:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
212 "37:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
213 "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
214 "48:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
215 "50:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
216 "52:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
217 };
218 verifyWithInlineConfigParser(
219 getPath("InputLeftCurlyTestDefault3Misc.java"), expected);
220 }
221
222 @Test
223 public void testNewline3Basic() throws Exception {
224 final String[] expected = {
225 "31:33: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 33),
226 "82:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
227 "88:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
228 };
229 verifyWithInlineConfigParser(
230 getPath("InputLeftCurlyTestNewLine3Basic.java"), expected);
231 }
232
233 @Test
234 public void testNewline3Empty() throws Exception {
235 final String[] expected = {
236 "24:49: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 49),
237 };
238 verifyWithInlineConfigParser(
239 getPath("InputLeftCurlyTestNewLine3Empty.java"), expected);
240 }
241
242 @Test
243 public void testNewline3Enum() throws Exception {
244 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
245 verifyWithInlineConfigParser(
246 getPath("InputLeftCurlyTestNewLine3Enum.java"), expected);
247 }
248
249 @Test
250 public void testNewline3Initializer() throws Exception {
251 final String[] expected = {
252 "27:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
253 "34:16: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 16),
254 };
255 verifyWithInlineConfigParser(
256 getPath("InputLeftCurlyTestNewLine3Initializer.java"), expected);
257 }
258
259 @Test
260 public void testNewline3Misc() throws Exception {
261 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
262 verifyWithInlineConfigParser(
263 getPath("InputLeftCurlyTestNewLine3Misc.java"), expected);
264 }
265
266 @Test
267 public void testMissingBracesConditional() throws Exception {
268 final String[] expected = {
269 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
270 "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
271 "26:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
272 };
273 verifyWithInlineConfigParser(
274 getPath("InputLeftCurlyTestMissingBracesConditional.java"), expected);
275 }
276
277 @Test
278 public void testMissingBracesLoop() throws Exception {
279 final String[] expected = {
280 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
281 "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
282 "26:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
283 "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
284 "56:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
285 };
286 verifyWithInlineConfigParser(
287 getPath("InputLeftCurlyTestMissingBracesLoop.java"), expected);
288 }
289
290 @Test
291 public void testMissingBracesMisc() throws Exception {
292 final String[] expected = {
293 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
294 "20:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
295 };
296 verifyWithInlineConfigParser(
297 getPath("InputLeftCurlyTestMissingBracesMisc.java"), expected);
298 }
299
300 @Test
301 public void testDefaultWithAnnotations() throws Exception {
302 final String[] expected = {
303 "23:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
304 "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
305 "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
306 "40:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
307 "63:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
308 "71:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
309 };
310 verifyWithInlineConfigParser(
311 getPath("InputLeftCurlyTestDefaultWithAnnotations.java"), expected);
312 }
313
314 @Test
315 public void testNlWithAnnotations() throws Exception {
316 final String[] expected = {
317 "48:55: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 55),
318 "51:41: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 41),
319 "57:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
320 "79:42: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 42),
321 };
322 verifyWithInlineConfigParser(
323 getPath("InputLeftCurlyTestNlWithAnnotations.java"), expected);
324 }
325
326 @Test
327 public void testNlowWithAnnotations() throws Exception {
328 final String[] expected = {
329 "23:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
330 "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
331 "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
332 "40:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
333 "63:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
334 "71:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
335 };
336 verifyWithInlineConfigParser(
337 getPath("InputLeftCurlyTestNlowWithAnnotations.java"), expected);
338 }
339
340 @Test
341 public void testLineBreakAfter() throws Exception {
342 final String[] expected = {
343 "22:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
344 "25:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
345 "29:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
346 "31:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
347 "33:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
348 "39:22: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 22),
349 "41:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
350 "48:33: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 33),
351 "49:21: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 21),
352 "52:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 29),
353 "52:34: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 34),
354 "58:37: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 37),
355 "64:12: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 12),
356 "67:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
357 "69:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
358 "79:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
359 };
360 verifyWithInlineConfigParser(
361 getPath("InputLeftCurlyTestLineBreakAfter.java"), expected);
362 }
363
364 @Test
365 public void testIgnoreEnumsOptionTrue() throws Exception {
366 final String[] expectedWhileTrue = {
367 "21:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 44),
368 };
369 verifyWithInlineConfigParser(
370 getPath("InputLeftCurlyIgnoreEnumsOptTrue.java"), expectedWhileTrue);
371 }
372
373 @Test
374 public void testIgnoreEnumsOptionFalse() throws Exception {
375 final String[] expectedWhileFalse = {
376 "17:17: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 17),
377 "21:44: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 44),
378 };
379 verifyWithInlineConfigParser(
380 getPath("InputLeftCurlyIgnoreEnumsOptFalse.java"), expectedWhileFalse);
381 }
382
383 @Test
384 public void testObjBlockTokenOnly() throws Exception {
385 final String[] expected = {
386 "13:17: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 17),
387 };
388 verifyWithInlineConfigParser(
389 getPath("InputLeftCurlyObjBlockTokenOnly.java"), expected);
390 }
391
392 @Test
393 public void testObjBlockWithTypeToken() throws Exception {
394 final String[] expected = {
395 "13:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
396 "15:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
397 };
398 verifyWithInlineConfigParser(
399 getPath("InputLeftCurlyObjBlockWithTypeToken.java"), expected);
400 }
401
402 @Test
403 public void testDefaultLambda() throws Exception {
404 final String[] expected = {
405 "17:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
406 "24:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
407 "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
408 };
409 verifyWithInlineConfigParser(
410 getPath("InputLeftCurlyTestDefaultLambda.java"),
411 expected);
412 }
413
414 @Test
415 public void testNewLineOptionWithLambda() throws Exception {
416 final String[] expected = {
417 "18:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
418 "24:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
419 };
420 verifyWithInlineConfigParser(
421 getPath("InputLeftCurlyTestNewLineOptionWithLambda.java"),
422 expected);
423 }
424
425 @Test
426 public void testEolSwitch() throws Exception {
427 final String[] expected = {
428 "22:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
429 "26:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
430 "33:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
431 "47:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
432 "52:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
433 };
434 verifyWithInlineConfigParser(
435 getPath("InputLeftCurlyTestEolSwitch.java"), expected);
436 }
437
438 @Test
439 public void testNlSwitch() throws Exception {
440 final String[] expected = {
441 "24:21: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 21),
442 "56:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
443 };
444 verifyWithInlineConfigParser(
445 getPath("InputLeftCurlyTestNlSwitch.java"), expected);
446 }
447
448 @Test
449 public void testNlowSwitch() throws Exception {
450 final String[] expected = {
451 "22:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
452 };
453 verifyWithInlineConfigParser(
454 getPath("InputLeftCurlyTestNlowSwitch.java"), expected);
455 }
456
457 @Test
458 public void testLeftCurlySwitchExpressions() throws Exception {
459 final String[] expected = {
460 "20:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
461 "22:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
462 "27:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
463 "32:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
464 "36:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
465 "45:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
466 "47:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
467 "51:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
468 "55:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
469 "59:21: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 21),
470 };
471 verifyWithInlineConfigParser(
472 getPath("InputLeftCurlyTestSwitchExpressions.java"), expected);
473 }
474
475 @Test
476 public void testLeftCurlySwitchExpressionsNewLine() throws Exception {
477
478 final String[] expected = {
479 "17:57: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 57),
480 "18:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
481 "43:25: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 25),
482 "54:23: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 23),
483 };
484 verifyWithInlineConfigParser(
485 getPath("InputLeftCurlyTestSwitchExpressionsNewLine.java"),
486 expected);
487 }
488
489 @Test
490 public void testGetAcceptableTokens() {
491 final LeftCurlyCheck check = new LeftCurlyCheck();
492 final int[] actual = check.getAcceptableTokens();
493 final int[] expected = {
494 TokenTypes.ANNOTATION_DEF,
495 TokenTypes.CLASS_DEF,
496 TokenTypes.CTOR_DEF,
497 TokenTypes.ENUM_CONSTANT_DEF,
498 TokenTypes.ENUM_DEF,
499 TokenTypes.INTERFACE_DEF,
500 TokenTypes.LAMBDA,
501 TokenTypes.LITERAL_CASE,
502 TokenTypes.LITERAL_CATCH,
503 TokenTypes.LITERAL_DEFAULT,
504 TokenTypes.LITERAL_DO,
505 TokenTypes.LITERAL_ELSE,
506 TokenTypes.LITERAL_FINALLY,
507 TokenTypes.LITERAL_FOR,
508 TokenTypes.LITERAL_IF,
509 TokenTypes.LITERAL_SWITCH,
510 TokenTypes.LITERAL_SYNCHRONIZED,
511 TokenTypes.LITERAL_TRY,
512 TokenTypes.LITERAL_WHILE,
513 TokenTypes.METHOD_DEF,
514 TokenTypes.OBJBLOCK,
515 TokenTypes.STATIC_INIT,
516 TokenTypes.RECORD_DEF,
517 TokenTypes.COMPACT_CTOR_DEF,
518 TokenTypes.SWITCH_RULE,
519 };
520 assertWithMessage("Default acceptable tokens are invalid")
521 .that(actual)
522 .isEqualTo(expected);
523 }
524
525 @Test
526 public void testFirstLine() throws Exception {
527 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
528 verifyWithInlineConfigParser(
529 getPath("InputLeftCurlyTestFirstLine.java"), expected);
530 }
531
532 @Test
533 public void testCoverageIncrease() throws Exception {
534 final String[] expected = {
535 "21:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
536 "30:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
537 "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
538 "48:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
539 "62:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
540 "67:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
541 "71:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
542 "76:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
543 "81:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
544 };
545 verifyWithInlineConfigParser(
546 getPath("InputLeftCurlyTestCoverageIncrease.java"), expected);
547 }
548
549 @Test
550 public void testLeftCurlyRecordsAndCompactCtors() throws Exception {
551 final String[] expected = {
552 "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
553 "24:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
554 "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
555 "36:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
556 "43:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
557 "56:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
558 };
559 verifyWithInlineConfigParser(
560 getPath("InputLeftCurlyTestRecordsAndCompactCtors.java"), expected);
561 }
562
563 @Test
564 public void testLeftCurlyWithEmoji() throws Exception {
565 final String[] expected = {
566 "17:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
567 "37:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
568 "39:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
569 "46:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
570 "50:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
571 "54:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
572 "60:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
573 "67:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
574 "72:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
575 "78:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
576 "81:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
577 };
578 verifyWithInlineConfigParser(getPath("InputLeftCurlyWithEmoji.java"), expected);
579 }
580
581 @Test
582 public void testLeftCurlyWithEmojiNewLine() throws Exception {
583 final String[] expected = {
584 "18:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
585 "20:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
586 "25:29: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 29),
587 "28:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
588 "31:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
589 "34:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
590 "43:39: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 39),
591 "60:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
592 "61:28: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 28),
593 "75:26: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 26),
594 "76:26: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 26),
595 "77:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
596 "89:52: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 52),
597 };
598 verifyWithInlineConfigParser(getPath("InputLeftCurlyWithEmojiNl.java"), expected);
599 }
600
601 @Test
602 public void testInvalidOption() {
603
604 final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class,
605 () -> {
606 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
607 verifyWithInlineConfigParser(
608 getPath("InputLeftCurlyTestInvalidOption.java"), expected);
609 });
610 assertWithMessage("Invalid exception message")
611 .that(exc.getMessage())
612 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
613 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
614 + "blocks.LeftCurlyCheck");
615 }
616
617 @Test
618 public void testTrimOptionProperty() throws Exception {
619 final String[] expected = {
620 "13:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
621 "20:16: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 16),
622 };
623 verifyWithInlineConfigParser(
624 getPath("InputLeftCurlyWithTrimOptionProperty.java"), expected);
625 }
626
627 @Test
628 public void testForEnumConstantDef() throws Exception {
629 final String[] expected = {
630 "14:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
631 "19:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
632 };
633 verifyWithInlineConfigParser(
634 getPath("InputLeftCurlyEnumConstantDef.java"), expected);
635 }
636
637 @Test
638 public void commentBeforeLeftCurly() throws Exception {
639 final String[] expected = {
640 "32:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
641 };
642 verifyWithInlineConfigParser(
643 getPath("InputLeftCurlyCommentBeforeLeftCurly.java"), expected);
644 }
645
646 @Test
647 public void commentBeforeLeftCurly2() throws Exception {
648 final String[] expected = {
649 "54:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
650 "66:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 29),
651 };
652 verifyWithInlineConfigParser(
653 getPath("InputLeftCurlyCommentBeforeLeftCurly2.java"), expected);
654 }
655
656 @Test
657 public void testSwitchRuleLineBreakAfter() throws Exception {
658 final String[] expected = {
659 "27:31: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 31),
660 "29:31: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 31),
661 "43:24: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 24),
662 "93:7: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 7),
663 };
664 verifyWithInlineConfigParser(
665 getPath("InputLeftCurlySwitchRuleLineBreakAfter.java"), expected);
666 }
667
668 @Test
669 public void testSwitchRuleWithBlock() throws Exception {
670 final String[] expected = {
671 "17:23: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 23),
672 };
673 verifyWithInlineConfigParser(
674 getPath("InputLeftCurlySwitchRuleWithBlock.java"), expected);
675 }
676
677 @Test
678 public void testSwitchMutation() throws Exception {
679 final String[] expected = {
680 "17:23: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 23),
681 "19:24: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 24),
682 };
683 verifyWithInlineConfigParser(
684 getPath("InputLeftCurlySwitchMutation.java"), expected);
685 }
686
687 }