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;
21
22 import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_MAX;
23 import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_MIN;
24 import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_SUM_MAX;
25 import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_SUM_MIN;
26
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32 public class DescendantTokenCheckTest extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/descendanttoken";
37 }
38
39 @Test
40 public void testDefault()
41 throws Exception {
42 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
43 verifyWithInlineConfigParser(
44 getPath("InputDescendantTokenIllegalTokens.java"), expected);
45 }
46
47 @Test
48 public void testMaximumNumber()
49 throws Exception {
50 final String[] expected = {
51 "32:12: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_NATIVE", "LITERAL_NATIVE"),
52 };
53 verifyWithInlineConfigParser(
54 getPath("InputDescendantTokenIllegalTokens2.java"), expected);
55 }
56
57 @Test
58 public void testMessage()
59 throws Exception {
60 final String[] expected = {
61 "32:12: Using 'native' is not allowed.",
62 };
63 verifyWithInlineConfigParser(
64 getPath("InputDescendantTokenIllegalTokens3.java"), expected);
65 }
66
67 @Test
68 public void testMinimumNumber()
69 throws Exception {
70 final String[] expected = {
71 "24:9: " + getCheckMessage(MSG_KEY_MIN, 1, 2, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
72 };
73 verifyWithInlineConfigParser(
74 getPath("InputDescendantTokenIllegalTokens4.java"), expected);
75 }
76
77 @Test
78 public void testMinimumDepth()
79 throws Exception {
80 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81 verifyWithInlineConfigParser(
82 getPath("InputDescendantTokenIllegalTokens5.java"), expected);
83 }
84
85 @Test
86 public void testMaximumDepth()
87 throws Exception {
88 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89 verifyWithInlineConfigParser(
90 getPath("InputDescendantTokenIllegalTokens6.java"), expected);
91 }
92
93 @Test
94 public void testEmptyStatements()
95 throws Exception {
96
97 final String[] expected = {
98 "22:7: Empty statement.",
99 "27:7: Empty statement.",
100 "32:19: Empty statement.",
101 "36:10: Empty statement.",
102 "39:16: Empty statement.",
103 "43:10: Empty statement.",
104 "53:10: Empty statement.",
105 "59:13: Empty statement.",
106 "61:13: Empty statement.",
107 "64:19: Empty statement.",
108 "68:10: Empty statement.",
109 "71:9: Empty statement.",
110 "76:10: Empty statement.",
111 "82:10: Empty statement.",
112 "86:10: Empty statement.",
113 "90:10: Empty statement.",
114 };
115
116 verifyWithInlineConfigParser(
117 getPath("InputDescendantTokenEmptyStatement.java"), expected);
118 }
119
120 @Test
121 public void testMissingSwitchDefault() throws Exception {
122
123 final String[] expected = {
124 "32:9: switch without \"default\" clause.",
125 };
126
127 verifyWithInlineConfigParser(
128 getPath("InputDescendantTokenMissingSwitchDefault.java"), expected);
129 }
130
131 @Test
132 public void testStringLiteralEquality() throws Exception {
133 final String[] expected = {
134 "22:18: Literal Strings should be compared using equals(), not '=='.",
135 "27:20: Literal Strings should be compared using equals(), not '=='.",
136 "32:22: Literal Strings should be compared using equals(), not '=='.",
137 };
138 verifyWithInlineConfigParser(
139 getPath("InputDescendantTokenStringLiteralEquality.java"), expected);
140 }
141
142 @Test
143 public void testIllegalTokenDefault() throws Exception {
144
145 final String[] expected = {
146 "23:9: Using 'LITERAL_SWITCH' is not allowed.",
147 "26:18: Using 'POST_DEC' is not allowed.",
148 "27:18: Using 'POST_INC' is not allowed.",
149 };
150 verifyWithInlineConfigParser(
151 getPath("InputDescendantTokenIllegalTokens7.java"), expected);
152 }
153
154 @Test
155 public void testIllegalTokenNative() throws Exception {
156
157 final String[] expected = {
158 "32:12: Using 'LITERAL_NATIVE' is not allowed.",
159 };
160 verifyWithInlineConfigParser(
161 getPath("InputDescendantTokenIllegalTokens8.java"), expected);
162 }
163
164 @Test
165 public void testReturnFromCatch() throws Exception {
166
167 final String[] expected = {
168 "22:11: Return from catch is not allowed.",
169 "30:11: Return from catch is not allowed.",
170 };
171
172 verifyWithInlineConfigParser(
173 getPath("InputDescendantTokenReturnFromCatch.java"), expected);
174 }
175
176 @Test
177 public void testReturnFromFinally() throws Exception {
178
179 final String[] expected = {
180 "22:11: Return from finally is not allowed.",
181 "30:11: Return from finally is not allowed.",
182 };
183
184 verifyWithInlineConfigParser(
185 getPath("InputDescendantTokenReturnFromFinally.java"), expected);
186 }
187
188 @Test
189 public void testNoSum() throws Exception {
190
191 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
192
193 verifyWithInlineConfigParser(
194 getPath("InputDescendantTokenReturnFromFinally2.java"), expected);
195 }
196
197 @Test
198 public void testWithSumCustomMsg() throws Exception {
199
200 final String[] expected = {
201 "37:32: this cannot be null.",
202 "37:50: this cannot be null.",
203 "38:33: this cannot be null.",
204 "38:51: this cannot be null.",
205 };
206
207 verifyWithInlineConfigParser(
208 getPath("InputDescendantTokenReturnFromFinally3.java"), expected);
209 }
210
211 @Test
212 public void testWithSumDefaultMsg() throws Exception {
213
214 final String[] expected = {
215 "37:32: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "EQUAL"),
216 "37:50: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "EQUAL"),
217 "38:33: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "NOT_EQUAL"),
218 "38:51: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "NOT_EQUAL"),
219 };
220
221 verifyWithInlineConfigParser(
222 getPath("InputDescendantTokenReturnFromFinally4.java"), expected);
223 }
224
225 @Test
226 public void testWithSumLessThenMinDefMsg() throws Exception {
227
228 final String[] expected = {
229 "32:44: " + getCheckMessage(MSG_KEY_SUM_MIN, 0, 3, "EQUAL"),
230 "38:32: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "EQUAL"),
231 "38:50: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "EQUAL"),
232 "39:33: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "NOT_EQUAL"),
233 "39:51: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "NOT_EQUAL"),
234 "41:13: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "EQUAL"),
235 "41:36: " + getCheckMessage(MSG_KEY_SUM_MIN, 1, 3, "EQUAL"),
236 };
237
238 verifyWithInlineConfigParser(
239 getPath("InputDescendantTokenReturnFromFinally5.java"), expected);
240 }
241
242 @Test
243 public void testWithSumLessThenMinCustomMsg() throws Exception {
244
245 final String[] expected = {
246 "31:44: custom message",
247 "37:32: custom message",
248 "37:50: custom message",
249 "38:33: custom message",
250 "38:51: custom message",
251 "40:13: custom message",
252 "40:36: custom message",
253 };
254
255 verifyWithInlineConfigParser(
256 getPath("InputDescendantTokenReturnFromFinally6.java"), expected);
257 }
258
259 @Test
260 public void testMaxTokenType() throws Exception {
261 final String[] expected = {
262 "21:48: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "LCURLY"),
263 "21:48: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "RCURLY"),
264 };
265 verifyWithInlineConfigParser(
266 getPath("InputDescendantTokenLastTokenType.java"), expected);
267 }
268
269 @Test
270 public void testMaxTokenTypeReverseOrder() throws Exception {
271 final String[] expected = {
272 "21:49: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "LCURLY"),
273 "21:49: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "RCURLY"),
274 };
275 verifyWithInlineConfigParser(
276 getPath("InputDescendantTokenLastTokenType2.java"), expected);
277 }
278
279 @Test
280 public void testLimitedToken() throws Exception {
281 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
282 verifyWithInlineConfigParser(
283 getPath("InputDescendantTokenTestLimitedToken.java"), expected);
284 }
285
286 }