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.UnnecessaryParenthesesCheck.MSG_ASSIGN;
24 import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_EXPR;
25 import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_IDENT;
26 import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_LAMBDA;
27 import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_LITERAL;
28 import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_RETURN;
29 import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_STRING;
30
31 import org.junit.jupiter.api.Test;
32
33 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
35
36
37
38
39
40 public class UnnecessaryParenthesesCheckTest extends AbstractModuleTestSupport {
41
42 @Override
43 protected String getPackageLocation() {
44 return "com/puppycrawl/tools/checkstyle/checks/coding/unnecessaryparentheses";
45 }
46
47 @Test
48 public void testDefault() throws Exception {
49
50 final String[] expected = {
51 "18:22: " + getCheckMessage(MSG_ASSIGN),
52 "18:29: " + getCheckMessage(MSG_EXPR),
53 "18:31: " + getCheckMessage(MSG_IDENT, "i"),
54 "18:46: " + getCheckMessage(MSG_ASSIGN),
55 "19:15: " + getCheckMessage(MSG_ASSIGN),
56 "20:14: " + getCheckMessage(MSG_IDENT, "x"),
57 "20:17: " + getCheckMessage(MSG_ASSIGN),
58 "21:15: " + getCheckMessage(MSG_ASSIGN),
59 "22:14: " + getCheckMessage(MSG_IDENT, "x"),
60 "22:17: " + getCheckMessage(MSG_ASSIGN),
61 "25:22: " + getCheckMessage(MSG_ASSIGN),
62 "25:30: " + getCheckMessage(MSG_IDENT, "i"),
63 "25:46: " + getCheckMessage(MSG_ASSIGN),
64 "29:17: " + getCheckMessage(MSG_LITERAL, "0"),
65 "39:11: " + getCheckMessage(MSG_ASSIGN),
66 "43:11: " + getCheckMessage(MSG_ASSIGN),
67 "45:11: " + getCheckMessage(MSG_ASSIGN),
68 "47:11: " + getCheckMessage(MSG_ASSIGN),
69 "48:16: " + getCheckMessage(MSG_IDENT, "a"),
70 "49:14: " + getCheckMessage(MSG_IDENT, "a"),
71 "49:20: " + getCheckMessage(MSG_IDENT, "b"),
72 "49:26: " + getCheckMessage(MSG_LITERAL, "600"),
73 "49:40: " + getCheckMessage(MSG_LITERAL, "12.5f"),
74 "49:56: " + getCheckMessage(MSG_IDENT, "arg2"),
75 "50:14: " + getCheckMessage(MSG_STRING, "\"this\""),
76 "50:25: " + getCheckMessage(MSG_STRING, "\"that\""),
77 "51:11: " + getCheckMessage(MSG_ASSIGN),
78 "51:14: " + getCheckMessage(MSG_STRING, "\"this is a really, really...\""),
79 "53:16: " + getCheckMessage(MSG_RETURN),
80 "57:21: " + getCheckMessage(MSG_LITERAL, "1"),
81 "57:26: " + getCheckMessage(MSG_LITERAL, "13.5"),
82 "58:22: " + getCheckMessage(MSG_LITERAL, "true"),
83 "59:17: " + getCheckMessage(MSG_IDENT, "b"),
84 "63:17: " + getCheckMessage(MSG_ASSIGN),
85 "65:11: " + getCheckMessage(MSG_ASSIGN),
86 "67:16: " + getCheckMessage(MSG_RETURN),
87 "77:13: " + getCheckMessage(MSG_EXPR),
88 "81:16: " + getCheckMessage(MSG_EXPR),
89 "86:19: " + getCheckMessage(MSG_EXPR),
90 "87:23: " + getCheckMessage(MSG_LITERAL, "4000"),
91 "92:19: " + getCheckMessage(MSG_ASSIGN),
92 "94:11: " + getCheckMessage(MSG_ASSIGN),
93 "94:16: " + getCheckMessage(MSG_LITERAL, "3"),
94 "95:26: " + getCheckMessage(MSG_ASSIGN),
95 "106:11: " + getCheckMessage(MSG_ASSIGN),
96 "106:14: " + getCheckMessage(MSG_STRING, "\"12345678901234567890123\""),
97 };
98
99 verifyWithInlineConfigParser(
100 getPath("InputUnnecessaryParenthesesOperatorsAndCasts.java"), expected);
101 }
102
103 @Test
104 public void test15Extensions() throws Exception {
105 final String[] expected = {
106 "28:23: " + getCheckMessage(MSG_EXPR),
107 "28:51: " + getCheckMessage(MSG_LITERAL, "1"),
108 "59:20: " + getCheckMessage(MSG_ASSIGN),
109 };
110 verifyWithInlineConfigParser(
111 getPath("InputUnnecessaryParentheses15Extensions.java"), expected);
112 }
113
114 @Test
115 public void testLambdas() throws Exception {
116 final String[] expected = {
117 "17:35: " + getCheckMessage(MSG_LAMBDA),
118 "18:35: " + getCheckMessage(MSG_LAMBDA),
119 "25:18: " + getCheckMessage(MSG_LAMBDA),
120 "28:25: " + getCheckMessage(MSG_LAMBDA),
121 "47:25: " + getCheckMessage(MSG_LAMBDA),
122 "47:33: " + getCheckMessage(MSG_LAMBDA),
123 "50:25: " + getCheckMessage(MSG_LAMBDA),
124 "53:31: " + getCheckMessage(MSG_LAMBDA),
125 };
126 verifyWithInlineConfigParser(
127 getPath("InputUnnecessaryParenthesesLambdas.java"), expected);
128 }
129
130 @Test
131 public void testReturn() throws Exception {
132 final String[] expected = {
133 "21:33: " + getCheckMessage(MSG_RETURN),
134 "22:16: " + getCheckMessage(MSG_RETURN),
135 "25:16: " + getCheckMessage(MSG_RETURN),
136 "28:16: " + getCheckMessage(MSG_RETURN),
137 "31:16: " + getCheckMessage(MSG_RETURN),
138 "36:16: " + getCheckMessage(MSG_RETURN),
139 };
140 verifyWithInlineConfigParser(
141 getPath("InputUnnecessaryParenthesesReturnValue.java"), expected);
142 }
143
144 @Test
145 public void testUnnecessaryParenthesesSwitchExpression() throws Exception {
146 final String[] expected = {
147 "21:31: " + getCheckMessage(MSG_ASSIGN),
148 "24:13: " + getCheckMessage(MSG_LITERAL, 2),
149 "25:39: " + getCheckMessage(MSG_ASSIGN),
150 "30:18: " + getCheckMessage(MSG_RETURN),
151 "32:16: " + getCheckMessage(MSG_IDENT, "g"),
152 "36:18: " + getCheckMessage(MSG_RETURN),
153 "46:31: " + getCheckMessage(MSG_ASSIGN),
154 "48:13: " + getCheckMessage(MSG_LITERAL, 2),
155 "49:39: " + getCheckMessage(MSG_ASSIGN),
156 "53:18: " + getCheckMessage(MSG_RETURN),
157 "58:18: " + getCheckMessage(MSG_RETURN),
158 };
159 verifyWithInlineConfigParser(
160 getNonCompilablePath(
161 "InputUnnecessaryParenthesesCheckSwitchExpression.java"),
162 expected);
163 }
164
165 @Test
166 public void testUnnecessaryParenthesesTextBlocks() throws Exception {
167 final String[] expected = {
168 "19:23: " + getCheckMessage(MSG_STRING, "\"this\""),
169 "19:34: " + getCheckMessage(MSG_STRING, "\"that\""),
170 "19:45: " + getCheckMessage(MSG_STRING, "\"other\""),
171 "20:23: " + getCheckMessage(MSG_STRING, "\"\\n "
172 + " this\""),
173 "22:12: " + getCheckMessage(MSG_STRING, "\"\\n "
174 + " that\""),
175 "24:12: " + getCheckMessage(MSG_STRING, "\"\\n "
176 + " other\""),
177 "27:23: " + getCheckMessage(MSG_STRING, "\"\\n this is a test...\""),
178 "28:32: " + getCheckMessage(MSG_STRING, "\"\\n and another li...\""),
179 };
180 verifyWithInlineConfigParser(
181 getNonCompilablePath(
182 "InputUnnecessaryParenthesesCheckTextBlocks.java"),
183 expected);
184 }
185
186 @Test
187 public void testUnnecessaryParenthesesPatterns() throws Exception {
188 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
189 verifyWithInlineConfigParser(
190 getNonCompilablePath(
191 "InputUnnecessaryParenthesesCheckPatterns.java"),
192 expected);
193 }
194
195 @Test
196 public void testTokensNotNull() {
197 final UnnecessaryParenthesesCheck check = new UnnecessaryParenthesesCheck();
198 assertWithMessage("Acceptable tokens should not be null")
199 .that(check.getAcceptableTokens())
200 .isNotNull();
201 assertWithMessage("Default tokens should not be null")
202 .that(check.getDefaultTokens())
203 .isNotNull();
204 assertWithMessage("Required tokens should not be null")
205 .that(check.getRequiredTokens())
206 .isNotNull();
207 }
208
209 @Test
210 public void testIfStatement() throws Exception {
211
212 final String[] expected = {
213 "20:20: " + getCheckMessage(MSG_EXPR),
214 "34:13: " + getCheckMessage(MSG_EXPR),
215 "35:20: " + getCheckMessage(MSG_EXPR),
216 "39:13: " + getCheckMessage(MSG_EXPR),
217 "39:14: " + getCheckMessage(MSG_EXPR),
218 "40:20: " + getCheckMessage(MSG_EXPR),
219 "45:20: " + getCheckMessage(MSG_EXPR),
220 "49:13: " + getCheckMessage(MSG_EXPR),
221 "50:20: " + getCheckMessage(MSG_EXPR),
222 "54:13: " + getCheckMessage(MSG_EXPR),
223 "55:17: " + getCheckMessage(MSG_EXPR),
224 "56:28: " + getCheckMessage(MSG_EXPR),
225 "61:13: " + getCheckMessage(MSG_EXPR),
226 "66:14: " + getCheckMessage(MSG_EXPR),
227 "67:24: " + getCheckMessage(MSG_EXPR),
228 "70:13: " + getCheckMessage(MSG_EXPR),
229 "71:21: " + getCheckMessage(MSG_EXPR),
230 "72:21: " + getCheckMessage(MSG_EXPR),
231 "78:12: " + getCheckMessage(MSG_EXPR),
232 "79:20: " + getCheckMessage(MSG_EXPR),
233 "86:20: " + getCheckMessage(MSG_EXPR),
234 "103:13: " + getCheckMessage(MSG_EXPR),
235 "106:13: " + getCheckMessage(MSG_EXPR),
236 "107:21: " + getCheckMessage(MSG_EXPR),
237 "110:13: " + getCheckMessage(MSG_EXPR),
238 };
239
240 verifyWithInlineConfigParser(
241 getPath("InputUnnecessaryParenthesesIfStatement.java"), expected);
242 }
243
244 @Test
245 public void testIfStatement2() throws Exception {
246 final String[] expected = {
247 "28:17: " + getCheckMessage(MSG_EXPR),
248 "39:17: " + getCheckMessage(MSG_EXPR),
249 "51:25: " + getCheckMessage(MSG_EXPR),
250 "57:13: " + getCheckMessage(MSG_EXPR),
251 "59:28: " + getCheckMessage(MSG_EXPR),
252 "60:28: " + getCheckMessage(MSG_EXPR),
253 "61:20: " + getCheckMessage(MSG_EXPR),
254 "63:20: " + getCheckMessage(MSG_EXPR),
255 "74:20: " + getCheckMessage(MSG_EXPR),
256 };
257 verifyWithInlineConfigParser(
258 getPath("InputUnnecessaryParenthesesIfStatement2.java"), expected);
259 }
260
261 @Test
262 public void testIdentifier() throws Exception {
263 final String[] expected = {
264 "22:17: " + getCheckMessage(MSG_IDENT, "test"),
265 "31:18: " + getCheckMessage(MSG_ASSIGN),
266 "32:13: " + getCheckMessage(MSG_IDENT, "square"),
267 "46:22: " + getCheckMessage(MSG_IDENT, "clazz"),
268 "56:18: " + getCheckMessage(MSG_IDENT, "test"),
269 "57:22: " + getCheckMessage(MSG_IDENT, "clazz"),
270 "75:18: " + getCheckMessage(MSG_EXPR),
271 "76:17: " + getCheckMessage(MSG_EXPR),
272 "77:25: " + getCheckMessage(MSG_EXPR),
273 "82:48: " + getCheckMessage(MSG_IDENT, "get"),
274 "100:34: " + getCheckMessage(MSG_IDENT, "isComment"),
275
276 };
277 verifyWithInlineConfigParser(
278 getPath("InputUnnecessaryParenthesesIdentifier.java"), expected);
279 }
280
281 @Test
282 public void testOperator1() throws Exception {
283 final String[] expected = {
284 "20:17: " + getCheckMessage(MSG_EXPR),
285 "22:17: " + getCheckMessage(MSG_EXPR),
286 "24:17: " + getCheckMessage(MSG_EXPR),
287 "26:17: " + getCheckMessage(MSG_EXPR),
288 "28:17: " + getCheckMessage(MSG_EXPR),
289 "30:17: " + getCheckMessage(MSG_EXPR),
290 "32:17: " + getCheckMessage(MSG_EXPR),
291 "34:17: " + getCheckMessage(MSG_EXPR),
292 "36:17: " + getCheckMessage(MSG_EXPR),
293 "38:17: " + getCheckMessage(MSG_EXPR),
294 "40:17: " + getCheckMessage(MSG_EXPR),
295 "42:17: " + getCheckMessage(MSG_EXPR),
296 "47:19: " + getCheckMessage(MSG_EXPR),
297 "49:18: " + getCheckMessage(MSG_EXPR),
298 "51:18: " + getCheckMessage(MSG_EXPR),
299 "53:17: " + getCheckMessage(MSG_EXPR),
300 "55:18: " + getCheckMessage(MSG_EXPR),
301 "57:19: " + getCheckMessage(MSG_EXPR),
302 "59:18: " + getCheckMessage(MSG_EXPR),
303 "61:19: " + getCheckMessage(MSG_EXPR),
304 "63:18: " + getCheckMessage(MSG_EXPR),
305 "65:18: " + getCheckMessage(MSG_EXPR),
306 "67:19: " + getCheckMessage(MSG_EXPR),
307 "69:18: " + getCheckMessage(MSG_EXPR),
308 "85:20: " + getCheckMessage(MSG_EXPR),
309 "102:14: " + getCheckMessage(MSG_EXPR),
310 "106:13: " + getCheckMessage(MSG_EXPR),
311 };
312 verifyWithInlineConfigParser(
313 getPath("InputUnnecessaryParenthesesOperator.java"), expected);
314 }
315
316 @Test
317 public void testOperator2() throws Exception {
318 final String[] expected = {
319 "66:18: " + getCheckMessage(MSG_EXPR),
320 "67:17: " + getCheckMessage(MSG_EXPR),
321 "68:25: " + getCheckMessage(MSG_EXPR),
322 "82:14: " + getCheckMessage(MSG_EXPR),
323 "83:19: " + getCheckMessage(MSG_EXPR),
324 "92:21: " + getCheckMessage(MSG_EXPR),
325 "95:19: " + getCheckMessage(MSG_EXPR),
326 "98:20: " + getCheckMessage(MSG_EXPR),
327 "101:21: " + getCheckMessage(MSG_EXPR),
328 "107:20: " + getCheckMessage(MSG_EXPR),
329 "110:21: " + getCheckMessage(MSG_EXPR),
330 };
331 verifyWithInlineConfigParser(
332 getPath("InputUnnecessaryParenthesesOperator2.java"), expected);
333 }
334
335 @Test
336 public void testOperator3() throws Exception {
337 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
338 verifyWithInlineConfigParser(
339 getPath("InputUnnecessaryParenthesesOperator3.java"), expected);
340 }
341 }