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.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.TextBlockGoogleStyleFormattingCheck.MSG_CLOSE_QUOTES_ERROR;
24 import static com.puppycrawl.tools.checkstyle.checks.coding.TextBlockGoogleStyleFormattingCheck.MSG_OPEN_QUOTES_ERROR;
25 import static com.puppycrawl.tools.checkstyle.checks.coding.TextBlockGoogleStyleFormattingCheck.MSG_VERTICALLY_UNALIGNED;
26
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31
32 public class TextBlockGoogleStyleFormattingCheckTest extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/coding/textblockgooglestyleformatting";
37 }
38
39 @Test
40 public void testGetAcceptableTokens() {
41 final TextBlockGoogleStyleFormattingCheck check =
42 new TextBlockGoogleStyleFormattingCheck();
43
44 final int[] acceptableTokens = {TokenTypes.TEXT_BLOCK_LITERAL_BEGIN};
45 assertWithMessage("Expected empty array")
46 .that(check.getAcceptableTokens())
47 .isEqualTo(acceptableTokens);
48 }
49
50 @Test
51 public void testDefaultTextBlockFormat() throws Exception {
52 final String[] expected = {
53 "13:37: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
54 "15:9: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
55 "24:33: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
56 "26:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
57 "36:36: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
58 "38:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
59 "41:17: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
60 "44:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
61 "47:16: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
62 "49:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
63 "55:32: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
64 "55:32: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
65 "63:42: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
66 "63:42: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
67 "70:16: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
68 "84:22: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
69 "85:22: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
70 "97:17: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
71 "99:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
72 "99:19: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
73 "101:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
74 };
75
76 verifyWithInlineConfigParser(
77 getPath("InputTextBlockGoogleStyleFormatting.java"), expected);
78 }
79
80 @Test
81 public void testDefaultTextBlockFormat1() throws Exception {
82 final String[] expected = {
83 "17:19: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
84 "19:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
85 "19:18: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
86 "21:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
87 "28:35: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
88 "28:35: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
89 "44:15: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
90 "48:15: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
91 "50:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
92 };
93
94 verifyWithInlineConfigParser(
95 getPath("InputTextBlockGoogleStyleFormatting1.java"), expected);
96 }
97
98 @Test
99 public void testDefaultTextBlockFormat2() throws Exception {
100 final String[] expected = {
101 "13:37: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
102 "14:14: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
103 "14:14: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
104 "21:32: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
105 "21:32: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
106 "28:33: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
107 "29:38: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
108 "29:38: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
109 "37:34: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
110 "37:34: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
111 "44:36: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
112 "45:31: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
113 "45:31: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
114 "51:17: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
115 "53:42: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
116 "53:42: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
117 "59:16: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
118 "60:32: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
119 "60:32: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
120 "69:32: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
121 "69:32: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
122 "77:42: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
123 "77:42: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
124 "84:16: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
125 "85:41: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
126 "85:41: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
127 "93:41: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
128 "93:41: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
129 "101:24: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
130 "101:24: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
131 };
132
133 verifyWithInlineConfigParser(
134 getPath("InputTextBlockGoogleStyleFormatting2.java"), expected);
135 }
136
137 @Test
138 public void testDefaultTextBlockFormat3() throws Exception {
139 final String[] expected = {
140 "18:17: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
141 "20:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
142 "20:19: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
143 "21:24: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
144 "21:24: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
145 "23:26: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
146 "23:26: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
147 "34:19: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
148 "35:24: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
149 "35:24: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
150 "35:29: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
151 "36:26: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
152 "36:26: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
153 "45:35: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
154 "45:35: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
155 "54:24: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
156 "54:24: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
157 "61:27: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
158 "61:27: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
159 "68:15: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
160 "69:22: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
161 "69:22: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
162 "77:22: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
163 "77:22: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
164 };
165
166 verifyWithInlineConfigParser(
167 getPath("InputTextBlockGoogleStyleFormatting3.java"), expected);
168 }
169
170 @Test
171 public void testDefaultTextBlockFormat4() throws Exception {
172 final String[] expected = {
173 "17:25: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
174 "26:29: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
175 "27:36: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
176 "27:36: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
177 "27:51: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
178 "29:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
179 "34:20: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
180 "37:44: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
181 "37:44: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
182 "47:17: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
183 "50:9: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
184 "59:16: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
185 "62:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
186 "64:15: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
187 "71:12: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
188 "76:16: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
189 "79:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
190 "91:16: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
191 "93:9: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
192 "95:12: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
193 "105:12: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
194 };
195 verifyWithInlineConfigParser(
196 getPath("InputTextBlockGoogleStyleFormatting4.java"), expected);
197 }
198
199 @Test
200 public void testDefaultTextBlockFormat5() throws Exception {
201 final String[] expected = {
202 "17:45: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
203 "19:9: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
204 "25:44: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
205 "27:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
206 "32:51: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
207 "34:26: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
208 "39:24: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
209 "43:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
210 "47:23: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
211 "49:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
212 "53:20: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
213 "55:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
214 "62:48: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
215 "65:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
216 "74:28: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
217 "76:9: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
218 "76:15: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
219 "78:9: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
220 };
221
222 verifyWithInlineConfigParser(
223 getPath("InputTextBlockGoogleStyleFormatting5.java"),
224 expected);
225 }
226
227 @Test
228 public void testTextBlockGoogleStyleFormattingWithTabs() throws Exception {
229 final String[] expected = {
230 "15:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
231 "18:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
232 "24:24: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
233 "27:33: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
234 "32:21: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
235 "39:66: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
236 "42:65: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
237 };
238
239 verifyWithInlineConfigParser(
240 getPath("InputTextBlockGoogleStyleFormattingWithTabs.java"),
241 expected);
242 }
243
244 @Test
245 public void testTextBlockFormatNotVerticallyAligned() throws Exception {
246 final String[] expected = {
247 "28:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
248 "33:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
249 "39:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
250 "44:1: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
251 "51:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
252 "69:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
253 "99:13: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
254 "102:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
255 "111:12: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
256 };
257
258 verifyWithInlineConfigParser(
259 getPath("InputTextBlockGoogleStyleFormattingNotVerticallyAligned.java"),
260 expected);
261 }
262
263 @Test
264 public void testDefaultTextBlockFormatWithIf() throws Exception {
265 final String[] expected = {
266 "16:18: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
267 "17:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
268 "24:13: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
269 "25:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
270 "25:18: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
271 "25:24: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
272 "26:19: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
273 "26:19: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
274 "36:20: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
275 "37:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
276 "37:18: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
277 "37:24: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
278 "38:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
279 "38:18: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
280 "51:18: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
281 "52:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
282 "52:24: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
283 "53:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
284 "53:18: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
285 "53:24: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
286 "54:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
287 "54:18: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
288 "64:19: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
289 "65:20: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
290 "65:20: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
291 "72:38: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
292 "73:20: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
293 "73:20: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
294 "80:33: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
295 "81:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
296 "81:18: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
297 "88:22: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
298 "89:18: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
299 "89:18: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
300 "96:21: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
301 "97:19: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
302 "97:19: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
303 "98:22: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
304 "99:27: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
305 "99:27: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
306 };
307
308 verifyWithInlineConfigParser(
309 getPath("InputTextBlockGoogleStyleFormatting6.java"), expected);
310 }
311
312 @Test
313 public void testDefaultTextBlockFormatWithNewKeyword() throws Exception {
314 final String[] expected = {
315 "13:42: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
316 "15:33: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
317 "21:31: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
318 "23:25: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
319 "25:55: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
320 "27:25: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
321 "36:70: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
322 "36:70: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
323 "46:21: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
324 "48:24: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
325 "60:36: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
326 "62:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
327 "66:23: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
328 "68:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
329 "74:25: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
330 "100:19: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
331 "102:21: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
332 };
333 verifyWithInlineConfigParser(
334 getPath("InputTextBlockGoogleStyleFormatting7.java"), expected);
335 }
336
337 @Test
338 public void testDefaultTextBlockFormatInCtor() throws Exception {
339 final String[] expected = {
340 "24:66: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
341 "24:66: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
342 "38:23: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
343 "40:17: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
344 "45:20: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
345 "53:14: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
346 };
347 verifyWithInlineConfigParser(
348 getPath("InputTextBlockGoogleStyleFormatting8.java"), expected);
349 }
350
351 @Test
352 public void testDefaultTextBlockFormatInAnnotations() throws Exception {
353 final String[] expected = {
354 "13:22: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
355 "14:19: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
356 "14:19: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
357 "21:22: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
358 "22:15: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
359 "22:15: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
360 "30:22: " + getCheckMessage(MSG_OPEN_QUOTES_ERROR),
361 "32:9: " + getCheckMessage(MSG_VERTICALLY_UNALIGNED),
362 };
363 verifyWithInlineConfigParser(
364 getPath("InputTextBlockGoogleStyleFormatting9.java"), expected);
365 }
366
367 @Test
368 public void testDefaultTextBlockFormatIndentationOfContent() throws Exception {
369 final String[] expected = {
370 "47:17: " + getCheckMessage(MSG_CLOSE_QUOTES_ERROR),
371 };
372 verifyWithInlineConfigParser(
373 getPath("InputTextBlockGoogleStyleFormatting10.java"), expected);
374 }
375 }