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.regexp;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_DUPLICATE_REGEXP;
24 import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_ILLEGAL_REGEXP;
25 import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_REQUIRED_REGEXP;
26
27 import java.util.List;
28
29 import org.junit.jupiter.api.Test;
30
31 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33
34 public class RegexpCheckTest extends AbstractModuleTestSupport {
35
36 @Override
37 protected String getPackageLocation() {
38 return "com/puppycrawl/tools/checkstyle/checks/regexp/regexp";
39 }
40
41 @Test
42 public void testGetAcceptableTokens() {
43 final RegexpCheck regexpCheck = new RegexpCheck();
44 assertWithMessage("RegexpCheck#getAcceptableTokens should return empty array by default")
45 .that(regexpCheck.getAcceptableTokens())
46 .isEmpty();
47 }
48
49 @Test
50 public void testGetRequiredTokens() {
51 final RegexpCheck checkObj = new RegexpCheck();
52 assertWithMessage("RegexpCheck#getRequiredTokens should return empty array by default")
53 .that(checkObj.getRequiredTokens())
54 .isEmpty();
55 }
56
57 @Test
58 public void testRequiredPass() throws Exception {
59 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
60 verifyWithInlineConfigParser(
61 getPath("InputRegexpSemantic.java"), expected);
62 }
63
64 @Test
65 public void testRequiredFail() throws Exception {
66 final String[] expected = {
67 "1: " + getCheckMessage(MSG_REQUIRED_REGEXP, "This\\stext is not in the file"),
68 };
69 verifyWithInlineConfigParser(getPath("InputRegexpSemantic2.java"), expected);
70 }
71
72 @Test
73 public void testDefault() throws Exception {
74 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
75 verifyWithInlineConfigParser(
76 getPath("InputRegexpCheckDefault.java"), expected);
77 }
78
79 @Test
80 public void testRequiredNoDuplicatesPass() throws Exception {
81 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
82 verifyWithInlineConfigParser(
83 getPath("InputRegexpSemantic3.java"), expected);
84 }
85
86 @Test
87 public void testSetDuplicatesTrue() throws Exception {
88 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89 verifyWithInlineConfigParser(
90 getPath("InputRegexpSemantic4.java"), expected);
91 }
92
93 @Test
94 public void testRequiredNoDuplicatesFail() throws Exception {
95 final String[] expected = {
96 "27: " + getCheckMessage(MSG_DUPLICATE_REGEXP, "Boolean x = new Boolean"),
97 "32: " + getCheckMessage(MSG_DUPLICATE_REGEXP, "Boolean x = new Boolean"),
98 };
99 verifyWithInlineConfigParser(
100 getPath("InputRegexpSemantic5.java"), expected);
101 }
102
103 @Test
104 public void testIllegalPass() throws Exception {
105 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
106 verifyWithInlineConfigParser(
107 getPath("InputRegexpSemantic6.java"), expected);
108 }
109
110 @Test
111 public void testStopEarly() throws Exception {
112 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
113 verifyWithInlineConfigParser(
114 getPath("InputRegexpCheckStopEarly.java"), expected);
115 }
116
117 @Test
118 public void testIllegalFailBelowErrorLimit() throws Exception {
119 final String[] expected = {
120 "15: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
121 "16: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
122 "17: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
123 };
124 verifyWithInlineConfigParser(
125 getPath("InputRegexpSemantic7.java"), expected);
126 }
127
128 @Test
129 public void testIllegalFailAboveErrorLimit() throws Exception {
130 final String error = "The error limit has been exceeded, "
131 + "the check is aborting, there may be more unreported errors.";
132 final String[] expected = {
133 "15: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import"),
134 "16: " + getCheckMessage(MSG_ILLEGAL_REGEXP, error + "^import"),
135 };
136 verifyWithInlineConfigParser(
137 getPath("InputRegexpSemantic8.java"), expected);
138 }
139
140 @Test
141 public void testMessagePropertyGood()
142 throws Exception {
143 final String[] expected = {
144 "78: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "Bad line :("),
145 };
146 verifyWithInlineConfigParser(
147 getPath("InputRegexpSemantic9.java"), expected);
148 }
149
150 @Test
151 public void testMessagePropertyBad()
152 throws Exception {
153 final String[] expected = {
154 "78: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
155 };
156 verifyWithInlineConfigParser(
157 getPath("InputRegexpSemantic10.java"), expected);
158 }
159
160 @Test
161 public void testMessagePropertyBad2()
162 throws Exception {
163 final String[] expected = {
164 "78: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "System\\.(out)|(err)\\.print(ln)?\\("),
165 };
166 verifyWithInlineConfigParser(
167 getPath("InputRegexpSemantic11.java"), expected);
168 }
169
170 @Test
171 public void testIgnoreCaseTrue() throws Exception {
172 final String[] expected = {
173 "78: " + getCheckMessage(MSG_ILLEGAL_REGEXP,
174 "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
175 };
176 verifyWithInlineConfigParser(
177 getPath("InputRegexpSemantic12.java"), expected);
178 }
179
180 @Test
181 public void testIgnoreCaseFalse() throws Exception {
182 final String[] expectedTrue = {
183 "78: " + getCheckMessage(MSG_ILLEGAL_REGEXP,
184 "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("),
185 };
186 verifyWithInlineConfigParser(
187 getPath("InputRegexpSemantic13.java"), expectedTrue);
188
189 final String[] expectedFalse = CommonUtil.EMPTY_STRING_ARRAY;
190 verifyWithInlineConfigParser(
191 getPath("InputRegexpSemantic14.java"), expectedFalse);
192 }
193
194 @Test
195 public void testIgnoreCommentsCppStyle() throws Exception {
196
197 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
198 verifyWithInlineConfigParser(
199 getPath("InputRegexpTrailingComment.java"), expected);
200 }
201
202 @Test
203 public void testIgnoreCommentsFalseCppStyle() throws Exception {
204
205 final String[] expected = {
206 "16: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "don't\\suse trailing comments"),
207 };
208 verifyWithInlineConfigParser(
209 getPath("InputRegexpTrailingComment2.java"), expected);
210 }
211
212 @Test
213 public void testIgnoreCommentsBlockStyle() throws Exception {
214
215 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
216 verifyWithInlineConfigParser(
217 getPath("InputRegexpTrailingComment3.java"), expected);
218 }
219
220 @Test
221 public void testIgnoreCommentsFalseBlockStyle() throws Exception {
222 final String[] expected = {
223 "31: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "c-style\\s1"),
224 };
225 verifyWithInlineConfigParser(
226 getPath("InputRegexpTrailingComment4.java"), expected);
227 }
228
229 @Test
230 public void testIgnoreCommentsMultipleBlockStyle() throws Exception {
231
232 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
233 verifyWithInlineConfigParser(
234 getPath("InputRegexpTrailingComment5.java"), expected);
235 }
236
237 @Test
238 public void testIgnoreCommentsMultiLine() throws Exception {
239 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
240 verifyWithInlineConfigParser(
241 getPath("InputRegexpTrailingComment6.java"), expected);
242 }
243
244 @Test
245 public void testIgnoreCommentsInlineStart() throws Exception {
246 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
247 verifyWithInlineConfigParser(
248 getPath("InputRegexpTrailingComment7.java"), expected);
249 }
250
251 @Test
252 public void testIgnoreCommentsInlineEnd() throws Exception {
253 final String[] expected = {
254 "34: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "int z"),
255 };
256 verifyWithInlineConfigParser(
257 getPath("InputRegexpTrailingComment8.java"), expected);
258 }
259
260 @Test
261 public void testIgnoreCommentsInlineMiddle() throws Exception {
262 final String[] expected = {
263 "35: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "int y"),
264 };
265 verifyWithInlineConfigParser(
266 getPath("InputRegexpTrailingComment9.java"), expected);
267 }
268
269 @Test
270 public void testIgnoreCommentsNoSpaces() throws Exception {
271
272 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
273 verifyWithInlineConfigParser(
274 getPath("InputRegexpTrailingComment10.java"), expected);
275 }
276
277 @Test
278 public void testOnFileStartingWithEmptyLine() throws Exception {
279 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
280 verifyWithInlineConfigParser(getPath("InputRegexpStartingWithEmptyLine.java"), expected);
281 }
282
283 @Test
284 public void testIgnoreCommentsCppStyleWithIllegalPatternFalse() throws Exception {
285
286 final String[] expected = {
287 "1: " + getCheckMessage(MSG_REQUIRED_REGEXP, "don't use trailing comments"),
288 };
289 verifyWithInlineConfigParser(getPath("InputRegexpTrailingComment11.java"), expected);
290 }
291
292 @Test
293 public void testStateIsClearedOnBeginTreeErrorCount() throws Exception {
294 final String file1 = getPath("InputRegexpCheckB2.java");
295 final String file2 = getPath("InputRegexpCheckB1.java");
296 final List<String> expectedFromFile1 = List.of(
297 "12: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import")
298 );
299 final List<String> expectedFromFile2 = List.of(
300 "12: " + getCheckMessage(MSG_ILLEGAL_REGEXP, "^import")
301 );
302 verifyWithInlineConfigParser(file1, file2, expectedFromFile1, expectedFromFile2);
303 }
304
305 @Test
306 public void testStateIsClearedOnBeginTreeMatchCount() throws Exception {
307 final String file1 = getPath("InputRegexpCheckB3.java");
308 final String file2 = getPath("InputRegexpCheckB4.java");
309 final List<String> expectedFirstInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
310 final List<String> expectedSecondInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
311 verifyWithInlineConfigParser(file1, file2,
312 expectedFirstInput, expectedSecondInput);
313 }
314
315 @Test
316 public void testOnFileStartingWithEmptyLine2() throws Exception {
317 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
318 verifyWithInlineConfigParser(getPath("InputRegexpCheckEmptyLine2.java"),
319 expected);
320 }
321 }