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.OperatorWrapCheck.MSG_LINE_NEW;
24 import static com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck.MSG_LINE_PREVIOUS;
25 import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
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.utils.CommonUtil;
32
33 public class OperatorWrapCheckTest extends AbstractModuleTestSupport {
34
35 @Override
36 public String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/whitespace/operatorwrap";
38 }
39
40 @Test
41 public void testDefault()
42 throws Exception {
43 final String[] expected = {
44 "23:19: " + getCheckMessage(MSG_LINE_NEW, "+"),
45 "24:15: " + getCheckMessage(MSG_LINE_NEW, "-"),
46 "32:18: " + getCheckMessage(MSG_LINE_NEW, "&&"),
47 "54:30: " + getCheckMessage(MSG_LINE_NEW, "&"),
48 "67:31: " + getCheckMessage(MSG_LINE_NEW, "&"),
49 };
50 verifyWithInlineConfigParser(
51 getPath("InputOperatorWrap1.java"), expected);
52 }
53
54 @Test
55 public void testOpWrapEol()
56 throws Exception {
57 final String[] expected = {
58 "26:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "-"),
59 "30:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "&&"),
60 "35:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "&&"),
61 };
62 verifyWithInlineConfigParser(
63 getPath("InputOperatorWrap2.java"), expected);
64 }
65
66 @Test
67 public void testNonDefOpsDefault()
68 throws Exception {
69 final String[] expected = {
70 "37:33: " + getCheckMessage(MSG_LINE_NEW, "::"),
71 };
72 verifyWithInlineConfigParser(
73 getPath("InputOperatorWrap3.java"), expected);
74 }
75
76 @Test
77 public void testNonDefOpsWrapEol()
78 throws Exception {
79 final String[] expected = {
80 "35:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "::"),
81 "40:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "::"),
82 };
83 verifyWithInlineConfigParser(
84 getPath("InputOperatorWrap4.java"), expected);
85 }
86
87 @Test
88 public void testAssignEol()
89 throws Exception {
90 final String[] expected = {
91 "46:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "="),
92 };
93 verifyWithInlineConfigParser(
94 getPath("InputOperatorWrap5.java"), expected);
95 }
96
97 @Test
98 public void testEol() throws Exception {
99 final String[] expected = {
100 "21:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "="),
101 "22:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "*"),
102 "33:18: " + getCheckMessage(MSG_LINE_PREVIOUS, "="),
103 "38:21: " + getCheckMessage(MSG_LINE_PREVIOUS, ":"),
104 "39:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "?"),
105 "44:17: " + getCheckMessage(MSG_LINE_PREVIOUS, ":"),
106 "49:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "="),
107 "61:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "||"),
108 "62:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "||"),
109 };
110 verifyWithInlineConfigParser(
111 getPath("InputOperatorWrapEol.java"), expected);
112 }
113
114 @Test
115 public void testNl() throws Exception {
116 final String[] expected = {
117 "20:16: " + getCheckMessage(MSG_LINE_NEW, "="),
118 "21:19: " + getCheckMessage(MSG_LINE_NEW, "*"),
119 "32:23: " + getCheckMessage(MSG_LINE_NEW, "="),
120 "35:25: " + getCheckMessage(MSG_LINE_NEW, "?"),
121 "43:24: " + getCheckMessage(MSG_LINE_NEW, ":"),
122 "48:18: " + getCheckMessage(MSG_LINE_NEW, "="),
123 "60:27: " + getCheckMessage(MSG_LINE_NEW, "&&"),
124 "61:31: " + getCheckMessage(MSG_LINE_NEW, "&&"),
125 };
126 verifyWithInlineConfigParser(
127 getPath("InputOperatorWrapNl.java"), expected);
128 }
129
130 @Test
131 public void testArraysAssign() throws Exception {
132 final String[] expected = {
133 "18:22: " + getCheckMessage(MSG_LINE_NEW, "="),
134 "36:28: " + getCheckMessage(MSG_LINE_NEW, "="),
135 };
136
137 verifyWithInlineConfigParser(
138 getPath("InputOperatorWrapArrayAssign.java"), expected);
139 }
140
141 @Test
142 public void testGuardedPattern() throws Exception {
143 final String[] expected = {
144 "30:23: " + getCheckMessage(MSG_LINE_NEW, "&&"),
145 "31:40: " + getCheckMessage(MSG_LINE_NEW, "&&"),
146 "34:30: " + getCheckMessage(MSG_LINE_NEW, "&&"),
147 "35:40: " + getCheckMessage(MSG_LINE_NEW, "&&"),
148 "36:32: " + getCheckMessage(MSG_LINE_NEW, "&&"),
149 };
150
151 verifyWithInlineConfigParser(
152 getPath("InputOperatorWrapGuardedPatterns.java"), expected);
153 }
154
155 @Test
156 public void testTryWithResources() throws Exception {
157 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
158 verifyWithInlineConfigParser(
159 getPath("InputOperatorWrapTryWithResources.java"), expected);
160 }
161
162 @Test
163 public void testInvalidOption() {
164
165 final CheckstyleException exc =
166 getExpectedThrowable(CheckstyleException.class, () -> {
167 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
168
169 verifyWithInlineConfigParser(
170 getPath("InputOperatorWrap6.java"), expected);
171 });
172 assertWithMessage("Invalid exception message")
173 .that(exc.getMessage())
174 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker");
175 assertWithMessage("Invalid exception message")
176 .that(exc.getCause().getMessage())
177 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.checks."
178 + "whitespace.OperatorWrapCheck");
179 }
180
181 @Test
182 public void testTrimOptionProperty() throws Exception {
183 final String[] expected = {
184 "18:21: " + getCheckMessage(MSG_LINE_PREVIOUS, ":"),
185 "19:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "?"),
186 };
187 verifyWithInlineConfigParser(
188 getPath("InputOperatorWrapWithTrimOptionProperty.java"), expected);
189 }
190
191 @Test
192 public void testInstanceOfOperator() throws Exception {
193 final String[] expected = {
194 "17:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
195 "23:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
196 "35:23: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
197 "39:23: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
198 "49:33: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
199 "59:33: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
200 "72:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
201 "82:38: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
202 };
203 verifyWithInlineConfigParser(
204 getNonCompilablePath("InputOperatorWrapInstanceOfOperator.java"), expected);
205 }
206
207 @Test
208 public void testInstanceOfOperatorEndOfLine() throws Exception {
209 final String[] expected = {
210 "28:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
211 "43:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
212 "65:20: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
213 "78:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
214 "88:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
215 };
216 verifyWithInlineConfigParser(
217 getNonCompilablePath(
218 "InputOperatorWrapInstanceOfOperatorEndOfLine.java"), expected);
219 }
220
221 @Test
222 public void testLambdaEol() throws Exception {
223 final String[] expected = {
224 "20:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "->"),
225 "29:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "->"),
226 "38:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "->"),
227 "47:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "->"),
228 "60:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "->"),
229 "70:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "->"),
230 "81:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "->"),
231 };
232 verifyWithInlineConfigParser(
233 getPath("InputOperatorWrapLambda.java"), expected);
234 }
235
236 @Test
237 public void testLambdaNl() throws Exception {
238 final String[] expected = {
239 "28:28: " + getCheckMessage(MSG_LINE_NEW, "->"),
240 "33:36: " + getCheckMessage(MSG_LINE_NEW, "->"),
241 "36:43: " + getCheckMessage(MSG_LINE_NEW, "->"),
242 "56:20: " + getCheckMessage(MSG_LINE_NEW, "->"),
243 };
244 verifyWithInlineConfigParser(
245 getPath("InputOperatorWrapLambdaNl.java"), expected);
246 }
247
248 }