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 "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() throws Exception {
164
165 try {
166 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
167
168 verifyWithInlineConfigParser(
169 getPath("InputOperatorWrap6.java"), expected);
170 assertWithMessage("exception expected").fail();
171 }
172 catch (CheckstyleException exc) {
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 + "Cannot set property 'option' to 'invalid_option'");
179 }
180 }
181
182 @Test
183 public void testTrimOptionProperty() throws Exception {
184 final String[] expected = {
185 "18:21: " + getCheckMessage(MSG_LINE_PREVIOUS, ":"),
186 "19:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "?"),
187 };
188 verifyWithInlineConfigParser(
189 getPath("InputOperatorWrapWithTrimOptionProperty.java"), expected);
190 }
191
192 @Test
193 public void testInstanceOfOperator() throws Exception {
194 final String[] expected = {
195 "17:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
196 "23:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
197 "35:23: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
198 "39:23: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
199 "49:33: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
200 "59:33: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
201 "72:15: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
202 "82:38: " + getCheckMessage(MSG_LINE_NEW, "instanceof"),
203 };
204 verifyWithInlineConfigParser(
205 getPath("InputOperatorWrapInstanceOfOperator.java"), expected);
206 }
207
208 @Test
209 public void testInstanceOfOperatorEndOfLine() throws Exception {
210 final String[] expected = {
211 "28:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
212 "43:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
213 "65:20: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
214 "78:17: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
215 "88:21: " + getCheckMessage(MSG_LINE_PREVIOUS, "instanceof"),
216 };
217 verifyWithInlineConfigParser(
218 getPath(
219 "InputOperatorWrapInstanceOfOperatorEndOfLine.java"), expected);
220 }
221
222 }