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
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32 public class OperatorWrapCheckTest
33 extends AbstractModuleTestSupport {
34
35 @Override
36 protected 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 "52:30: " + getCheckMessage(MSG_LINE_NEW, "&&"),
145 "54:31: " + getCheckMessage(MSG_LINE_NEW, "&&"),
146 "62:30: " + getCheckMessage(MSG_LINE_NEW, "&&"),
147 "64:31: " + getCheckMessage(MSG_LINE_NEW, "&&"),
148 "71:23: " + getCheckMessage(MSG_LINE_NEW, "&&"),
149 "72:43: " + getCheckMessage(MSG_LINE_NEW, "&&"),
150 "75:30: " + getCheckMessage(MSG_LINE_NEW, "&&"),
151 "76:44: " + getCheckMessage(MSG_LINE_NEW, "&&"),
152 "77:34: " + getCheckMessage(MSG_LINE_NEW, "&&"),
153 };
154
155 verifyWithInlineConfigParser(
156 getNonCompilablePath("InputOperatorWrapGuardedPatterns.java"), expected);
157 }
158
159 @Test
160 public void testTryWithResources() throws Exception {
161 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
162 verifyWithInlineConfigParser(
163 getPath("InputOperatorWrapTryWithResources.java"), expected);
164 }
165
166 @Test
167 public void testInvalidOption() throws Exception {
168
169 try {
170 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
171
172 verifyWithInlineConfigParser(
173 getPath("InputOperatorWrap6.java"), expected);
174 assertWithMessage("exception expected").fail();
175 }
176 catch (CheckstyleException ex) {
177 assertWithMessage("Invalid exception message")
178 .that(ex.getMessage())
179 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
180 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
181 + "whitespace.OperatorWrapCheck - "
182 + "Cannot set property 'option' to 'invalid_option'");
183 }
184 }
185
186 @Test
187 public void testTrimOptionProperty() throws Exception {
188 final String[] expected = {
189 "18:21: " + getCheckMessage(MSG_LINE_PREVIOUS, ":"),
190 "19:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "?"),
191 };
192 verifyWithInlineConfigParser(
193 getPath("InputOperatorWrapWithTrimOptionProperty.java"), expected);
194 }
195
196 @Test
197 public void testInstanceOfOperator() throws Exception {
198 final String[] expected = {
199 "17:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
200 "23:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
201 "35:23: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
202 "39:23: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
203 "49:33: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
204 "59:33: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
205 "72:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
206 "82:38: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
207 };
208 verifyWithInlineConfigParser(
209 getNonCompilablePath("InputOperatorWrapInstanceOfOperator.java"), expected);
210 }
211
212 @Test
213 public void testInstanceOfOperatorEndOfLine() throws Exception {
214 final String[] expected = {
215 "28:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
216 "43:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
217 "65:20: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
218 "78:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
219 "88:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
220 };
221 verifyWithInlineConfigParser(
222 getNonCompilablePath(
223 "InputOperatorWrapInstanceOfOperatorEndOfLine.java"), expected);
224 }
225
226 }