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.sizes;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.sizes.ExecutableStatementCountCheck.MSG_KEY;
24
25 import java.util.Collection;
26
27 import org.antlr.v4.runtime.CommonToken;
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
32 import com.puppycrawl.tools.checkstyle.api.Context;
33 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
35 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
36
37 public class ExecutableStatementCountCheckTest
38 extends AbstractModuleTestSupport {
39
40 @Override
41 protected String getPackageLocation() {
42 return "com/puppycrawl/tools/checkstyle/checks/sizes/executablestatementcount";
43 }
44
45 @Test
46 @SuppressWarnings("unchecked")
47 public void testStatefulFieldsClearedOnBeginTree() {
48 final DetailAstImpl ast = new DetailAstImpl();
49 ast.setType(TokenTypes.STATIC_INIT);
50 final ExecutableStatementCountCheck check = new ExecutableStatementCountCheck();
51 assertWithMessage("Stateful field is not cleared after beginTree")
52 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "contextStack",
53 contextStack -> ((Collection<Context>) contextStack).isEmpty()))
54 .isTrue();
55 }
56
57 @Test
58 public void testMaxZero() throws Exception {
59
60 final String[] expected = {
61 "12:5: " + getCheckMessage(MSG_KEY, 3, 0),
62 "15:17: " + getCheckMessage(MSG_KEY, 1, 0),
63 "25:5: " + getCheckMessage(MSG_KEY, 2, 0),
64 "35:5: " + getCheckMessage(MSG_KEY, 1, 0),
65 "42:5: " + getCheckMessage(MSG_KEY, 3, 0),
66 "56:5: " + getCheckMessage(MSG_KEY, 2, 0),
67 "66:5: " + getCheckMessage(MSG_KEY, 2, 0),
68 "75:5: " + getCheckMessage(MSG_KEY, 2, 0),
69 "84:5: " + getCheckMessage(MSG_KEY, 2, 0),
70 "87:13: " + getCheckMessage(MSG_KEY, 1, 0),
71 "98:29: " + getCheckMessage(MSG_KEY, 1, 0),
72 };
73
74 verifyWithInlineConfigParser(
75 getPath("InputExecutableStatementCountMaxZero.java"), expected);
76 }
77
78 @Test
79 public void testMethodDef() throws Exception {
80
81 final String[] expected = {
82 "12:5: " + getCheckMessage(MSG_KEY, 3, 0),
83 "15:17: " + getCheckMessage(MSG_KEY, 1, 0),
84 "25:5: " + getCheckMessage(MSG_KEY, 2, 0),
85 "35:5: " + getCheckMessage(MSG_KEY, 1, 0),
86 "42:5: " + getCheckMessage(MSG_KEY, 3, 0),
87 "60:13: " + getCheckMessage(MSG_KEY, 1, 0),
88 };
89
90 verifyWithInlineConfigParser(
91 getPath("InputExecutableStatementCountMethodDef.java"), expected);
92 }
93
94 @Test
95 public void testCtorDef() throws Exception {
96
97 final String[] expected = {
98 "12:5: " + getCheckMessage(MSG_KEY, 2, 0),
99 "22:5: " + getCheckMessage(MSG_KEY, 2, 0),
100 };
101
102 verifyWithInlineConfigParser(
103 getPath("InputExecutableStatementCountCtorDef.java"), expected);
104 }
105
106 @Test
107 public void testStaticInit() throws Exception {
108
109 final String[] expected = {
110 "13:5: " + getCheckMessage(MSG_KEY, 2, 0),
111 };
112
113 verifyWithInlineConfigParser(
114 getPath("InputExecutableStatementCountStaticInit.java"), expected);
115 }
116
117 @Test
118 public void testInstanceInit() throws Exception {
119
120 final String[] expected = {
121 "13:5: " + getCheckMessage(MSG_KEY, 2, 0),
122 };
123
124 verifyWithInlineConfigParser(
125 getPath("InputExecutableStatementCountInstanceInit.java"), expected);
126 }
127
128 @Test
129 public void testVisitTokenWithWrongTokenType() {
130 final ExecutableStatementCountCheck checkObj =
131 new ExecutableStatementCountCheck();
132 final DetailAstImpl ast = new DetailAstImpl();
133 ast.initialize(
134 new CommonToken(TokenTypes.ENUM, "ENUM"));
135 try {
136 checkObj.visitToken(ast);
137 assertWithMessage("exception expected").fail();
138 }
139 catch (IllegalStateException ex) {
140 assertWithMessage("Invalid exception message")
141 .that(ex.getMessage())
142 .isEqualTo("ENUM[0x-1]");
143 }
144 }
145
146 @Test
147 public void testLeaveTokenWithWrongTokenType() {
148 final ExecutableStatementCountCheck checkObj =
149 new ExecutableStatementCountCheck();
150 final DetailAstImpl ast = new DetailAstImpl();
151 ast.initialize(
152 new CommonToken(TokenTypes.ENUM, "ENUM"));
153 try {
154 checkObj.leaveToken(ast);
155 assertWithMessage("exception expected").fail();
156 }
157 catch (IllegalStateException ex) {
158 assertWithMessage("Invalid exception message")
159 .that(ex.getMessage())
160 .isEqualTo("ENUM[0x-1]");
161 }
162 }
163
164 @Test
165 public void testDefaultConfiguration() throws Exception {
166
167 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
168 verifyWithInlineConfigParser(
169 getPath("InputExecutableStatementCountDefaultConfig.java"), expected);
170 }
171
172 @Test
173 public void testExecutableStatementCountRecords() throws Exception {
174
175 final int max = 1;
176
177 final String[] expected = {
178 "15:9: " + getCheckMessage(MSG_KEY, 3, max),
179 "24:9: " + getCheckMessage(MSG_KEY, 3, max),
180 "33:9: " + getCheckMessage(MSG_KEY, 3, max),
181 "41:9: " + getCheckMessage(MSG_KEY, 4, max),
182 "51:9: " + getCheckMessage(MSG_KEY, 6, max),
183 "65:17: " + getCheckMessage(MSG_KEY, 6, max),
184 };
185
186 verifyWithInlineConfigParser(
187 getNonCompilablePath("InputExecutableStatementCountRecords.java"),
188 expected);
189 }
190
191 @Test
192 public void testExecutableStatementCountLambdas() throws Exception {
193
194 final int max = 1;
195
196 final String[] expected = {
197 "16:22: " + getCheckMessage(MSG_KEY, 6, max),
198 "25:22: " + getCheckMessage(MSG_KEY, 2, max),
199 "26:26: " + getCheckMessage(MSG_KEY, 2, max),
200 "30:26: " + getCheckMessage(MSG_KEY, 4, max),
201 };
202
203 verifyWithInlineConfigParser(
204 getPath("InputExecutableStatementCountLambdas.java"), expected);
205 }
206
207 }