forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalLocalVariableCheckTest.java
323 lines (286 loc) · 12.5 KB
/
FinalLocalVariableCheckTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2023 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.coding;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.puppycrawl.tools.checkstyle.checks.coding.FinalLocalVariableCheck.MSG_KEY;
import org.junit.jupiter.api.Test;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DetailAstImpl;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
public class FinalLocalVariableCheckTest
extends AbstractModuleTestSupport {
@Override
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/checks/coding/finallocalvariable";
}
@Test
public void testDefault() throws Exception {
final String[] expected = {
"17:13: " + getCheckMessage(MSG_KEY, "i"),
"17:16: " + getCheckMessage(MSG_KEY, "j"),
"18:18: " + getCheckMessage(MSG_KEY, "runnable"),
"28:13: " + getCheckMessage(MSG_KEY, "i"),
"32:13: " + getCheckMessage(MSG_KEY, "z"),
"34:16: " + getCheckMessage(MSG_KEY, "obj"),
"38:16: " + getCheckMessage(MSG_KEY, "x"),
"44:18: " + getCheckMessage(MSG_KEY, "runnable"),
"48:21: " + getCheckMessage(MSG_KEY, "q"),
"64:13: " + getCheckMessage(MSG_KEY, "i"),
"68:13: " + getCheckMessage(MSG_KEY, "z"),
"70:16: " + getCheckMessage(MSG_KEY, "obj"),
"74:16: " + getCheckMessage(MSG_KEY, "x"),
"82:21: " + getCheckMessage(MSG_KEY, "w"),
"83:26: " + getCheckMessage(MSG_KEY, "runnable"),
"104:17: " + getCheckMessage(MSG_KEY, "weird"),
"105:17: " + getCheckMessage(MSG_KEY, "j"),
"106:17: " + getCheckMessage(MSG_KEY, "k"),
"193:13: " + getCheckMessage(MSG_KEY, "x"),
"198:17: " + getCheckMessage(MSG_KEY, "x"),
"218:17: " + getCheckMessage(MSG_KEY, "n"),
"224:13: " + getCheckMessage(MSG_KEY, "q"),
"225:13: " + getCheckMessage(MSG_KEY, "w"),
"234:21: " + getCheckMessage(MSG_KEY, "w"),
"235:21: " + getCheckMessage(MSG_KEY, "e"),
"255:17: " + getCheckMessage(MSG_KEY, "n"),
"267:17: " + getCheckMessage(MSG_KEY, "t"),
"277:21: " + getCheckMessage(MSG_KEY, "foo"),
"296:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"308:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"352:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"365:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"368:21: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"383:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"394:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"426:13: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"429:21: " + getCheckMessage(MSG_KEY, "shouldBeFinal"),
"449:33: " + getCheckMessage(MSG_KEY, "table"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariable.java"), expected);
}
@Test
public void testRecordsInput() throws Exception {
final String[] expected = {
"20:17: " + getCheckMessage(MSG_KEY, "b"),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputFinalLocalVariableCheckRecords.java"), expected);
}
@Test
public void testParameter() throws Exception {
final String[] expected = {
"53:28: " + getCheckMessage(MSG_KEY, "aArg"),
"157:36: " + getCheckMessage(MSG_KEY, "_o"),
"162:37: " + getCheckMessage(MSG_KEY, "_o1"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariable2.java"), expected);
}
@Test
public void testNativeMethods() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableNativeMethods.java"), expected);
}
@Test
public void testFalsePositive() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableFalsePositive.java"), expected);
}
@Test
public void testEnhancedForLoopVariableTrue() throws Exception {
final String[] expected = {
"16:20: " + getCheckMessage(MSG_KEY, "a"),
"23:13: " + getCheckMessage(MSG_KEY, "x"),
"29:66: " + getCheckMessage(MSG_KEY, "snippets"),
"30:32: " + getCheckMessage(MSG_KEY, "filteredSnippets"),
"31:21: " + getCheckMessage(MSG_KEY, "snippet"),
"46:20: " + getCheckMessage(MSG_KEY, "a"),
"49:16: " + getCheckMessage(MSG_KEY, "a"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableEnhancedForLoopVariable.java"),
expected);
}
@Test
public void testEnhancedForLoopVariableFalse() throws Exception {
final String[] expected = {
"23:13: " + getCheckMessage(MSG_KEY, "x"),
"29:66: " + getCheckMessage(MSG_KEY, "snippets"),
"30:32: " + getCheckMessage(MSG_KEY, "filteredSnippets"),
"49:16: " + getCheckMessage(MSG_KEY, "a"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableEnhancedForLoopVariable2.java"),
expected);
}
@Test
public void testLambda()
throws Exception {
final String[] expected = {
"40:16: " + getCheckMessage(MSG_KEY, "result"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableNameLambda.java"),
expected);
}
@Test
public void testVariableNameShadowing()
throws Exception {
final String[] expected = {
"12:28: " + getCheckMessage(MSG_KEY, "text"),
"25:13: " + getCheckMessage(MSG_KEY, "x"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableNameShadowing.java"), expected);
}
@Test
public void testImproperToken() {
final FinalLocalVariableCheck check = new FinalLocalVariableCheck();
final DetailAstImpl lambdaAst = new DetailAstImpl();
lambdaAst.setType(TokenTypes.LAMBDA);
try {
check.visitToken(lambdaAst);
assertWithMessage("IllegalStateException is expected").fail();
}
catch (IllegalStateException ex) {
// it is OK
}
}
@Test
public void testVariableWhichIsAssignedMultipleTimes() throws Exception {
final String[] expected = {
"57:13: " + getCheckMessage(MSG_KEY, "i"),
"130:16: " + getCheckMessage(MSG_KEY, "path"),
"134:20: " + getCheckMessage(MSG_KEY, "relativePath"),
"210:17: " + getCheckMessage(MSG_KEY, "kind"),
"215:24: " + getCheckMessage(MSG_KEY, "m"),
"417:17: " + getCheckMessage(MSG_KEY, "increment"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableAssignedMultipleTimes.java"), expected);
}
@Test
public void testVariableIsAssignedInsideAndOutsideSwitchBlock() throws Exception {
final String[] expected = {
"39:13: " + getCheckMessage(MSG_KEY, "b"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableAssignedInsideAndOutsideSwitch.java"),
expected);
}
@Test
public void testFinalLocalVariableFalsePositives() throws Exception {
final String[] expected = {
"352:16: " + getCheckMessage(MSG_KEY, "c2"),
"2195:16: " + getCheckMessage(MSG_KEY, "b"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableFalsePositives.java"), expected);
}
@Test
public void testMultipleAndNestedConditions() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableMultipleAndNestedConditions.java"),
expected);
}
@Test
public void testMultiTypeCatch() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableMultiCatch.java"),
expected);
}
@Test
public void testLeavingSlistToken() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableLeavingSlistToken.java"), expected);
}
@Test
public void testBreakOrReturn() throws Exception {
final String[] expected = {
"15:19: " + getCheckMessage(MSG_KEY, "e"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableBreak.java"), expected);
}
@Test
public void testAnonymousClass() throws Exception {
final String[] expected = {
"13:16: " + getCheckMessage(MSG_KEY, "testSupport"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableAnonymousClass.java"), expected);
}
@Test
public void testReceiverParameter() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableReceiverParameter.java"), expected);
}
@Test
public void testFinalLocalVariableSwitchExpressions() throws Exception {
final String[] expected = {
"15:19: " + getCheckMessage(MSG_KEY, "e"),
"53:19: " + getCheckMessage(MSG_KEY, "e"),
"91:19: " + getCheckMessage(MSG_KEY, "e"),
"125:19: " + getCheckMessage(MSG_KEY, "e"),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputFinalLocalVariableCheckSwitchExpressions.java"),
expected);
}
@Test
public void testFinalLocalVariableSwitchAssignment() throws Exception {
final String[] expected = {
"21:13: " + getCheckMessage(MSG_KEY, "a"),
"44:13: " + getCheckMessage(MSG_KEY, "b"),
"46:21: " + getCheckMessage(MSG_KEY, "x"),
"72:16: " + getCheckMessage(MSG_KEY, "res"),
"92:16: " + getCheckMessage(MSG_KEY, "res"),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputFinalLocalVariableCheckSwitchAssignment.java"),
expected);
}
@Test
public void testFinalLocalVariableSwitchStatement() throws Exception {
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableSwitchStatement.java"),
expected);
}
@Test
public void testConstructor() throws Exception {
final String[] expected = {
"14:44: " + getCheckMessage(MSG_KEY, "a"),
"18:44: " + getCheckMessage(MSG_KEY, "a"),
"19:43: " + getCheckMessage(MSG_KEY, "b"),
"22:47: " + getCheckMessage(MSG_KEY, "str"),
"35:21: " + getCheckMessage(MSG_KEY, "str"),
};
verifyWithInlineConfigParser(
getPath("InputFinalLocalVariableConstructor.java"),
expected);
}
}