View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.whitespace;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck.MSG_LINE_PREVIOUS;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck.MSG_WS_NOT_PRECEDED;
25  import static com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck.MSG_WS_PRECEDED;
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 MethodParamPadCheckTest
36      extends AbstractModuleTestSupport {
37  
38      @Override
39      public String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/checks/whitespace/methodparampad";
41      }
42  
43      @Test
44      public void testGetRequiredTokens() {
45          final MethodParamPadCheck checkObj = new MethodParamPadCheck();
46          assertWithMessage("MethodParamPadCheck#getRequiredTokens should return empty array "
47                  + "by default")
48              .that(checkObj.getRequiredTokens())
49              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
50      }
51  
52      @Test
53      public void testDefault() throws Exception {
54          final String[] expected = {
55              "21:32: " + getCheckMessage(MSG_WS_PRECEDED, "("),
56              "23:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
57              "27:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
58              "30:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
59              "37:24: " + getCheckMessage(MSG_WS_PRECEDED, "("),
60              "42:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
61              "46:39: " + getCheckMessage(MSG_WS_PRECEDED, "("),
62              "48:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
63              "52:16: " + getCheckMessage(MSG_WS_PRECEDED, "("),
64              "54:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
65              "60:21: " + getCheckMessage(MSG_WS_PRECEDED, "("),
66              "62:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
67              "66:18: " + getCheckMessage(MSG_WS_PRECEDED, "("),
68              "68:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
69              "71:36: " + getCheckMessage(MSG_WS_PRECEDED, "("),
70              "73:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
71              "84:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
72              "89:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
73          };
74          verifyWithInlineConfigParser(
75                  getPath("InputMethodParamPad.java"), expected);
76      }
77  
78      @Test
79      public void testAllowLineBreaks() throws Exception {
80          final String[] expected = {
81              "21:33: " + getCheckMessage(MSG_WS_PRECEDED, "("),
82              "23:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
83              "37:24: " + getCheckMessage(MSG_WS_PRECEDED, "("),
84              "46:39: " + getCheckMessage(MSG_WS_PRECEDED, "("),
85              "52:16: " + getCheckMessage(MSG_WS_PRECEDED, "("),
86              "60:21: " + getCheckMessage(MSG_WS_PRECEDED, "("),
87              "66:18: " + getCheckMessage(MSG_WS_PRECEDED, "("),
88              "71:36: " + getCheckMessage(MSG_WS_PRECEDED, "("),
89              "84:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
90          };
91          verifyWithInlineConfigParser(
92                  getPath("InputMethodParamPad2.java"), expected);
93      }
94  
95      @Test
96      public void testSpaceOption() throws Exception {
97          final String[] expected = {
98              "16:32: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
99              "18:14: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
100             "27:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
101             "30:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
102             "33:23: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
103             "42:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
104             "46:40: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
105             "49:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
106             "52:15: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
107             "55:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
108             "58:28: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
109             "60:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
110             "63:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
111             "66:40: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
112             "67:17: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
113             "70:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
114             "72:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
115             "75:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
116             "78:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
117             "82:45: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
118             "84:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
119             "93:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
120         };
121         verifyWithInlineConfigParser(
122                 getPath("InputMethodParamPad3.java"), expected);
123     }
124 
125     @Test
126     public void testMethodParamPadRecords() throws Exception {
127         final String[] expected = {
128             "19:16: " + getCheckMessage(MSG_WS_PRECEDED, "("),
129             "20:34: " + getCheckMessage(MSG_WS_PRECEDED, "("),
130             "31:17: " + getCheckMessage(MSG_WS_PRECEDED, "("),
131             "32:14: " + getCheckMessage(MSG_WS_PRECEDED, "("),
132             "33:18: " + getCheckMessage(MSG_WS_PRECEDED, "("),
133             "37:17: " + getCheckMessage(MSG_WS_PRECEDED, "("),
134             "38:33: " + getCheckMessage(MSG_WS_PRECEDED, "("),
135             "44:17: " + getCheckMessage(MSG_WS_PRECEDED, "("),
136             "45:18: " + getCheckMessage(MSG_WS_PRECEDED, "("),
137             "51:25: " + getCheckMessage(MSG_WS_PRECEDED, "("),
138             "57:25: " + getCheckMessage(MSG_WS_PRECEDED, "("),
139         };
140         verifyWithInlineConfigParser(
141                 getPath("InputMethodParamPadRecords.java"), expected);
142     }
143 
144     @Test
145     public void test1322879() throws Exception {
146         final String[] expected = {
147             "28:13: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
148         };
149         verifyWithInlineConfigParser(
150                 getPath("InputMethodParamPadWhitespaceAround.java"),
151                expected);
152     }
153 
154     @Test
155     public void testMethodParamPadCheckWithEmoji() throws Exception {
156         final String[] expected = {
157             "19:31: " + getCheckMessage(MSG_WS_PRECEDED, "("),
158             "21:30: " + getCheckMessage(MSG_WS_PRECEDED, "("),
159             "25:28: " + getCheckMessage(MSG_WS_PRECEDED, "("),
160             "32:36: " + getCheckMessage(MSG_WS_PRECEDED, "("),
161             "36:70: " + getCheckMessage(MSG_WS_PRECEDED, "("),
162             "38:31: " + getCheckMessage(MSG_WS_PRECEDED, "("),
163             "41:24: " + getCheckMessage(MSG_WS_PRECEDED, "("),
164             "47:24: " + getCheckMessage(MSG_WS_PRECEDED, "("),
165             "50:23: " + getCheckMessage(MSG_WS_PRECEDED, "("),
166         };
167         verifyWithInlineConfigParser(
168                 getPath("InputMethodParamPadCheckWithEmoji.java"), expected);
169     }
170 
171     @Test
172     public void testMethodParamPadCheckConstructors() throws Exception {
173         final String[] expected = {
174             "21:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
175             "25:14: " + getCheckMessage(MSG_WS_PRECEDED, "("),
176             "29:14: " + getCheckMessage(MSG_WS_PRECEDED, "("),
177         };
178         verifyWithInlineConfigParser(
179                 getPath("InputMethodParamPadCheckConstructors.java"), expected);
180     }
181 
182     @Test
183     public void testGetAcceptableTokens() {
184         final MethodParamPadCheck methodParamPadCheckObj = new MethodParamPadCheck();
185         final int[] actual = methodParamPadCheckObj.getAcceptableTokens();
186         final int[] expected = {
187             TokenTypes.CTOR_DEF,
188             TokenTypes.CTOR_CALL,
189             TokenTypes.LITERAL_NEW,
190             TokenTypes.METHOD_CALL,
191             TokenTypes.METHOD_DEF,
192             TokenTypes.SUPER_CTOR_CALL,
193             TokenTypes.ENUM_CONSTANT_DEF,
194             TokenTypes.RECORD_DEF,
195             TokenTypes.RECORD_PATTERN_DEF,
196         };
197         assertWithMessage("Default acceptable tokens are invalid")
198             .that(actual)
199             .isEqualTo(expected);
200     }
201 
202     @Test
203     public void testInvalidOption() {
204         final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class, () -> {
205             final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
206 
207             verifyWithInlineConfigParser(getPath("InputMethodParamPad4.java"), expected);
208         });
209         assertWithMessage("Invalid exception message")
210             .that(exc.getMessage())
211             .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
212                 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
213                 + "whitespace.MethodParamPadCheck");
214     }
215 
216     @Test
217     public void testTrimOptionProperty() throws Exception {
218         final String[] expected = {
219             "15:24: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
220             "26:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
221         };
222         verifyWithInlineConfigParser(
223                 getPath("InputMethodParamPadSetOptionTrim.java"), expected);
224     }
225 
226     @Test
227     public void testRecordPattern() throws Exception {
228         final String[] expected = {
229             "19:41: " + getCheckMessage(MSG_WS_PRECEDED, "("),
230             "22:42: " + getCheckMessage(MSG_WS_PRECEDED, "("),
231             "22:50: " + getCheckMessage(MSG_WS_PRECEDED, "("),
232             "31:31: " + getCheckMessage(MSG_WS_PRECEDED, "("),
233             "31:39: " + getCheckMessage(MSG_WS_PRECEDED, "("),
234             "36:31: " + getCheckMessage(MSG_WS_PRECEDED, "("),
235             "57:41: " + getCheckMessage(MSG_WS_PRECEDED, "("),
236             "57:49: " + getCheckMessage(MSG_WS_PRECEDED, "("),
237         };
238         verifyWithInlineConfigParser(
239                 getPath("InputMethodParamPadCheckRecordPattern.java"), expected);
240     }
241 
242     @Test
243     public void testRecordPattern2() throws Exception {
244         final String[] expected = {
245             "23:40: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
246             "23:46: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
247             "31:23: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
248             "36:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
249             "39:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
250             "40:26: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
251             "44:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
252             "46:22: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
253             "50:40: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
254             "50:47: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
255         };
256         verifyWithInlineConfigParser(
257                 getPath("InputMethodParamPadCheckRecordPattern2.java"), expected);
258     }
259 }