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.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.InnerAssignmentCheck.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 InnerAssignmentCheckTest
31 extends AbstractModuleTestSupport {
32
33 @Override
34 protected String getPackageLocation() {
35 return "com/puppycrawl/tools/checkstyle/checks/coding/innerassignment";
36 }
37
38 @Test
39 public void testIt() throws Exception {
40 final String[] expected = {
41 "22:15: " + getCheckMessage(MSG_KEY),
42 "22:19: " + getCheckMessage(MSG_KEY),
43 "27:39: " + getCheckMessage(MSG_KEY),
44 "29:35: " + getCheckMessage(MSG_KEY),
45 "76:19: " + getCheckMessage(MSG_KEY),
46 };
47 verifyWithInlineConfigParser(
48 getPath("InputInnerAssignment.java"), expected);
49 }
50
51 @Test
52 public void testMethod() throws Exception {
53 final String[] expected = {
54 "73:22: " + getCheckMessage(MSG_KEY),
55 };
56 verifyWithInlineConfigParser(
57 getPath("InputInnerAssignmentMethod.java"), expected);
58 }
59
60 @Test
61 public void testDemoBug1195047Comment3() throws Exception {
62 final String[] expected = {
63 "18:16: " + getCheckMessage(MSG_KEY),
64 "19:24: " + getCheckMessage(MSG_KEY),
65 "20:19: " + getCheckMessage(MSG_KEY),
66 "21:17: " + getCheckMessage(MSG_KEY),
67 "22:29: " + getCheckMessage(MSG_KEY),
68 "23:20: " + getCheckMessage(MSG_KEY),
69 "24:17: " + getCheckMessage(MSG_KEY),
70 "24:31: " + getCheckMessage(MSG_KEY),
71 "24:41: " + getCheckMessage(MSG_KEY),
72 "29:16: " + getCheckMessage(MSG_KEY),
73 "29:27: " + getCheckMessage(MSG_KEY),
74 "33:32: " + getCheckMessage(MSG_KEY),
75 };
76 verifyWithInlineConfigParser(
77 getPath("InputInnerAssignmentDemoBug1195047Comment3.java"), expected);
78 }
79
80 @Test
81 public void testLambdaExpression() throws Exception {
82 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
83 verifyWithInlineConfigParser(
84 getPath("InputInnerAssignmentLambdaExpressions.java"),
85 expected);
86 }
87
88 @Test
89 public void testInnerAssignmentNotInLoopContext() throws Exception {
90 final String[] expected = {
91 "12:28: " + getCheckMessage(MSG_KEY),
92 };
93 verifyWithInlineConfigParser(
94 getPath("InputInnerAssignmentNotInLoopContext.java"),
95 expected);
96 }
97
98 @Test
99 public void testTokensNotNull() {
100 final InnerAssignmentCheck check = new InnerAssignmentCheck();
101 assertWithMessage("Acceptable tokens should not be null")
102 .that(check.getAcceptableTokens())
103 .isNotNull();
104 assertWithMessage("Default tokens should not be null")
105 .that(check.getDefaultTokens())
106 .isNotNull();
107 assertWithMessage("Required tokens should not be null")
108 .that(check.getRequiredTokens())
109 .isNotNull();
110 }
111
112 @Test
113 public void testInnerAssignmentSwitchAndSwitchExpression() throws Exception {
114 final String[] expected = {
115 "28:23: " + getCheckMessage(MSG_KEY),
116 "38:25: " + getCheckMessage(MSG_KEY),
117 "40:25: " + getCheckMessage(MSG_KEY),
118 "41:26: " + getCheckMessage(MSG_KEY),
119 "49:25: " + getCheckMessage(MSG_KEY),
120 "51:31: " + getCheckMessage(MSG_KEY),
121 "52:26: " + getCheckMessage(MSG_KEY),
122 "59:42: " + getCheckMessage(MSG_KEY),
123 "61:34: " + getCheckMessage(MSG_KEY),
124 "94:25: " + getCheckMessage(MSG_KEY),
125 "96:26: " + getCheckMessage(MSG_KEY),
126 "98:27: " + getCheckMessage(MSG_KEY),
127 };
128 verifyWithInlineConfigParser(
129 getNonCompilablePath("InputInnerAssignmentSwitchAndSwitchExpression.java"),
130 expected);
131 }
132 }