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