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