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.metrics;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30
31 public class CyclomaticComplexityCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity";
37 }
38
39 @Test
40 public void testSwitchBlockAsSingleDecisionPointSetToTrue() throws Exception {
41
42 final String[] expected = {
43 "14:5: " + getCheckMessage(MSG_KEY, 2, 0),
44 };
45
46 verifyWithInlineConfigParser(
47 getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
48 }
49
50 @Test
51 public void testSwitchBlockAsSingleDecisionPointSetToFalse() throws Exception {
52
53 final String[] expected = {
54 "14:5: " + getCheckMessage(MSG_KEY, 5, 0),
55 };
56
57 verifyWithInlineConfigParser(
58 getPath("InputCyclomaticComplexitySwitchBlocks2.java"), expected);
59 }
60
61 @Test
62 public void testEqualsMaxComplexity() throws Exception {
63
64 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
65
66 verifyWithInlineConfigParser(
67 getPath("InputCyclomaticComplexitySwitchBlocks3.java"), expected);
68 }
69
70 @Test
71 public void test() throws Exception {
72
73 final String[] expected = {
74 "15:5: " + getCheckMessage(MSG_KEY, 2, 0),
75 "20:17: " + getCheckMessage(MSG_KEY, 2, 0),
76 "32:5: " + getCheckMessage(MSG_KEY, 6, 0),
77 "45:5: " + getCheckMessage(MSG_KEY, 3, 0),
78 "55:5: " + getCheckMessage(MSG_KEY, 5, 0),
79 "73:5: " + getCheckMessage(MSG_KEY, 3, 0),
80 "86:5: " + getCheckMessage(MSG_KEY, 3, 0),
81 "98:5: " + getCheckMessage(MSG_KEY, 3, 0),
82 "110:5: " + getCheckMessage(MSG_KEY, 1, 0),
83 "114:13: " + getCheckMessage(MSG_KEY, 2, 0),
84 };
85
86 verifyWithInlineConfigParser(
87 getPath("InputCyclomaticComplexity.java"), expected);
88 }
89
90 @Test
91 public void testCyclomaticComplexityRecords() throws Exception {
92
93 final int max = 0;
94
95 final String[] expected = {
96 "17:9: " + getCheckMessage(MSG_KEY, 11, max),
97 "48:9: " + getCheckMessage(MSG_KEY, 11, max),
98 "83:5: " + getCheckMessage(MSG_KEY, 11, max),
99 "115:5: " + getCheckMessage(MSG_KEY, 11, max),
100 "148:5: " + getCheckMessage(MSG_KEY, 11, max),
101 };
102
103 verifyWithInlineConfigParser(
104 getNonCompilablePath("InputCyclomaticComplexityRecords.java"), expected);
105 }
106
107 @Test
108 public void testGetAcceptableTokens() {
109 final CyclomaticComplexityCheck cyclomaticComplexityCheckObj =
110 new CyclomaticComplexityCheck();
111 final int[] actual = cyclomaticComplexityCheckObj.getAcceptableTokens();
112 final int[] expected = {
113 TokenTypes.CTOR_DEF,
114 TokenTypes.METHOD_DEF,
115 TokenTypes.INSTANCE_INIT,
116 TokenTypes.STATIC_INIT,
117 TokenTypes.LITERAL_WHILE,
118 TokenTypes.LITERAL_DO,
119 TokenTypes.LITERAL_FOR,
120 TokenTypes.LITERAL_IF,
121 TokenTypes.LITERAL_SWITCH,
122 TokenTypes.LITERAL_CASE,
123 TokenTypes.LITERAL_CATCH,
124 TokenTypes.QUESTION,
125 TokenTypes.LAND,
126 TokenTypes.LOR,
127 TokenTypes.COMPACT_CTOR_DEF,
128 TokenTypes.LITERAL_WHEN,
129 };
130 assertWithMessage("Invalid acceptable tokens")
131 .that(actual)
132 .isEqualTo(expected);
133 }
134
135 @Test
136 public void testGetRequiredTokens() {
137 final CyclomaticComplexityCheck cyclomaticComplexityCheckObj =
138 new CyclomaticComplexityCheck();
139 final int[] actual = cyclomaticComplexityCheckObj.getRequiredTokens();
140 final int[] expected = {
141 TokenTypes.CTOR_DEF,
142 TokenTypes.METHOD_DEF,
143 TokenTypes.INSTANCE_INIT,
144 TokenTypes.STATIC_INIT,
145 TokenTypes.COMPACT_CTOR_DEF,
146 };
147 assertWithMessage("Invalid required tokens")
148 .that(actual)
149 .isEqualTo(expected);
150 }
151
152 @Test
153 public void testHighMax() throws Exception {
154 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
155
156 verifyWithInlineConfigParser(
157 getPath("InputCyclomaticComplexitySwitchBlocks4.java"), expected);
158 }
159
160 @Test
161 public void testDefaultMax() throws Exception {
162 final String[] expected = {
163 "14:5: " + getCheckMessage(MSG_KEY, 12, 10),
164 };
165
166 verifyWithInlineConfigParser(
167 getPath("InputCyclomaticComplexitySwitchBlocks5.java"), expected);
168 }
169
170 @Test
171 public void testWhenExpression() throws Exception {
172 final String[] expected = {
173 "13:4: " + getCheckMessage(MSG_KEY, 5, 0),
174 "19:4: " + getCheckMessage(MSG_KEY, 5, 0),
175 "28:4: " + getCheckMessage(MSG_KEY, 7, 0),
176 };
177 verifyWithInlineConfigParser(
178 getNonCompilablePath("InputCyclomaticComplexityWhenExpression.java"), expected);
179 }
180
181 @Test
182 public void testWhenExpressionSwitchAsSinglePoint() throws Exception {
183 final String[] expected = {
184 "14:5: " + getCheckMessage(MSG_KEY, 5, 0),
185 "20:5: " + getCheckMessage(MSG_KEY, 2, 0),
186 "29:5: " + getCheckMessage(MSG_KEY, 2, 0),
187 "39:5: " + getCheckMessage(MSG_KEY, 2, 0),
188 };
189 verifyWithInlineConfigParser(
190 getNonCompilablePath(
191 "InputCyclomaticComplexityWhenSwitchAsSinglePoint.java"), expected);
192 }
193
194 @Test
195 public void testSwitchBlockAsSingleDecisionPointWithNestedSwitch() throws Exception {
196 final String[] expected = {
197 "17:5: " + getCheckMessage(MSG_KEY, 2, 0),
198 "26:5: " + getCheckMessage(MSG_KEY, 2, 0),
199 "41:5: " + getCheckMessage(MSG_KEY, 2, 0),
200 };
201 verifyWithInlineConfigParser(
202 getNonCompilablePath(
203 "InputCyclomaticComplexitySwitchBlocks6.java"), expected);
204 }
205
206 }