View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   * Test fixture for the UnnecessaryParenthesesCheck.
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 = {
189             "24:22: " + getCheckMessage(MSG_ASSIGN),
190             "27:21: " + getCheckMessage(MSG_ASSIGN),
191             "31:13: " + getCheckMessage(MSG_EXPR),
192         };
193         verifyWithInlineConfigParser(
194                 getNonCompilablePath(
195                 "InputUnnecessaryParenthesesCheckPatterns.java"),
196             expected);
197     }
198 
199     @Test
200     public void testTokensNotNull() {
201         final UnnecessaryParenthesesCheck check = new UnnecessaryParenthesesCheck();
202         assertWithMessage("Acceptable tokens should not be null")
203             .that(check.getAcceptableTokens())
204             .isNotNull();
205         assertWithMessage("Default tokens should not be null")
206             .that(check.getDefaultTokens())
207             .isNotNull();
208         assertWithMessage("Required tokens should not be null")
209             .that(check.getRequiredTokens())
210             .isNotNull();
211     }
212 
213     @Test
214     public void testIfStatement() throws Exception {
215 
216         final String[] expected = {
217             "20:20: " + getCheckMessage(MSG_EXPR),
218             "34:13: " + getCheckMessage(MSG_EXPR),
219             "35:20: " + getCheckMessage(MSG_EXPR),
220             "39:13: " + getCheckMessage(MSG_EXPR),
221             "39:14: " + getCheckMessage(MSG_EXPR),
222             "40:20: " + getCheckMessage(MSG_EXPR),
223             "45:20: " + getCheckMessage(MSG_EXPR),
224             "49:13: " + getCheckMessage(MSG_EXPR),
225             "50:20: " + getCheckMessage(MSG_EXPR),
226             "54:13: " + getCheckMessage(MSG_EXPR),
227             "55:17: " + getCheckMessage(MSG_EXPR),
228             "56:28: " + getCheckMessage(MSG_EXPR),
229             "61:13: " + getCheckMessage(MSG_EXPR),
230             "66:14: " + getCheckMessage(MSG_EXPR),
231             "67:24: " + getCheckMessage(MSG_EXPR),
232             "70:13: " + getCheckMessage(MSG_EXPR),
233             "71:21: " + getCheckMessage(MSG_EXPR),
234             "72:21: " + getCheckMessage(MSG_EXPR),
235             "78:12: " + getCheckMessage(MSG_EXPR),
236             "79:20: " + getCheckMessage(MSG_EXPR),
237             "86:20: " + getCheckMessage(MSG_EXPR),
238             "103:13: " + getCheckMessage(MSG_EXPR),
239             "106:13: " + getCheckMessage(MSG_EXPR),
240             "107:21: " + getCheckMessage(MSG_EXPR),
241             "110:13: " + getCheckMessage(MSG_EXPR),
242         };
243 
244         verifyWithInlineConfigParser(
245                 getPath("InputUnnecessaryParenthesesIfStatement.java"), expected);
246     }
247 
248     @Test
249     public void testIfStatement2() throws Exception {
250         final String[] expected = {
251             "28:17: " + getCheckMessage(MSG_EXPR),
252             "39:17: " + getCheckMessage(MSG_EXPR),
253             "51:25: " + getCheckMessage(MSG_EXPR),
254             "57:13: " + getCheckMessage(MSG_EXPR),
255             "59:28: " + getCheckMessage(MSG_EXPR),
256             "60:28: " + getCheckMessage(MSG_EXPR),
257             "61:20: " + getCheckMessage(MSG_EXPR),
258             "63:20: " + getCheckMessage(MSG_EXPR),
259             "74:20: " + getCheckMessage(MSG_EXPR),
260         };
261         verifyWithInlineConfigParser(
262                 getPath("InputUnnecessaryParenthesesIfStatement2.java"), expected);
263     }
264 
265     @Test
266     public void testIdentifier() throws Exception {
267         final String[] expected = {
268             "22:17: " + getCheckMessage(MSG_IDENT, "test"),
269             "31:18: " + getCheckMessage(MSG_ASSIGN),
270             "32:13: " + getCheckMessage(MSG_IDENT, "square"),
271             "46:22: " + getCheckMessage(MSG_IDENT, "clazz"),
272             "56:18: " + getCheckMessage(MSG_IDENT, "test"),
273             "57:22: " + getCheckMessage(MSG_IDENT, "clazz"),
274             "75:18: " + getCheckMessage(MSG_EXPR),
275             "76:17: " + getCheckMessage(MSG_EXPR),
276             "77:25: " + getCheckMessage(MSG_EXPR),
277             "82:48: " + getCheckMessage(MSG_IDENT, "get"),
278             "100:34: " + getCheckMessage(MSG_IDENT, "isComment"),
279 
280         };
281         verifyWithInlineConfigParser(
282                 getPath("InputUnnecessaryParenthesesIdentifier.java"), expected);
283     }
284 
285     @Test
286     public void testOperator1() throws Exception {
287         final String[] expected = {
288             "20:17: " + getCheckMessage(MSG_EXPR),
289             "22:17: " + getCheckMessage(MSG_EXPR),
290             "24:17: " + getCheckMessage(MSG_EXPR),
291             "26:17: " + getCheckMessage(MSG_EXPR),
292             "28:17: " + getCheckMessage(MSG_EXPR),
293             "30:17: " + getCheckMessage(MSG_EXPR),
294             "32:17: " + getCheckMessage(MSG_EXPR),
295             "34:17: " + getCheckMessage(MSG_EXPR),
296             "36:17: " + getCheckMessage(MSG_EXPR),
297             "38:17: " + getCheckMessage(MSG_EXPR),
298             "40:17: " + getCheckMessage(MSG_EXPR),
299             "42:17: " + getCheckMessage(MSG_EXPR),
300             "47:19: " + getCheckMessage(MSG_EXPR),
301             "49:18: " + getCheckMessage(MSG_EXPR),
302             "51:18: " + getCheckMessage(MSG_EXPR),
303             "53:17: " + getCheckMessage(MSG_EXPR),
304             "55:18: " + getCheckMessage(MSG_EXPR),
305             "57:19: " + getCheckMessage(MSG_EXPR),
306             "59:18: " + getCheckMessage(MSG_EXPR),
307             "61:19: " + getCheckMessage(MSG_EXPR),
308             "63:18: " + getCheckMessage(MSG_EXPR),
309             "65:18: " + getCheckMessage(MSG_EXPR),
310             "67:19: " + getCheckMessage(MSG_EXPR),
311             "69:18: " + getCheckMessage(MSG_EXPR),
312             "85:20: " + getCheckMessage(MSG_EXPR),
313             "102:14: " + getCheckMessage(MSG_EXPR),
314             "106:13: " + getCheckMessage(MSG_EXPR),
315         };
316         verifyWithInlineConfigParser(
317                 getPath("InputUnnecessaryParenthesesOperator.java"), expected);
318     }
319 
320     @Test
321     public void testOperator2() throws Exception {
322         final String[] expected = {
323             "66:18: " + getCheckMessage(MSG_EXPR),
324             "67:17: " + getCheckMessage(MSG_EXPR),
325             "68:25: " + getCheckMessage(MSG_EXPR),
326             "82:14: " + getCheckMessage(MSG_EXPR),
327             "83:19: " + getCheckMessage(MSG_EXPR),
328             "92:21: " + getCheckMessage(MSG_EXPR),
329             "95:19: " + getCheckMessage(MSG_EXPR),
330             "98:20: " + getCheckMessage(MSG_EXPR),
331             "101:21: " + getCheckMessage(MSG_EXPR),
332             "107:20: " + getCheckMessage(MSG_EXPR),
333             "110:21: " + getCheckMessage(MSG_EXPR),
334         };
335         verifyWithInlineConfigParser(
336                 getPath("InputUnnecessaryParenthesesOperator2.java"), expected);
337     }
338 
339     @Test
340     public void testOperator3() throws Exception {
341         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
342         verifyWithInlineConfigParser(
343                 getPath("InputUnnecessaryParenthesesOperator3.java"), expected);
344     }
345 
346     @Test
347     public void testWhenExpressions() throws Exception {
348         final String[] expected = {
349             "22:33: " + getCheckMessage(MSG_EXPR),
350             "24:32: " + getCheckMessage(MSG_EXPR),
351             "28:26: " + getCheckMessage(MSG_EXPR),
352             "31:31: " + getCheckMessage(MSG_EXPR),
353             "31:32: " + getCheckMessage(MSG_EXPR),
354             "37:44: " + getCheckMessage(MSG_EXPR),
355             "40:44: " + getCheckMessage(MSG_EXPR),
356         };
357         verifyWithInlineConfigParser(
358                 getNonCompilablePath("InputUnnecessaryParenthesesWhenExpressions.java"), expected);
359     }
360 }