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.BooleanExpressionComplexityCheck.MSG_KEY;
24
25 import org.antlr.v4.runtime.CommonToken;
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32
33 public class BooleanExpressionComplexityCheckTest extends AbstractModuleTestSupport {
34
35 @Override
36 protected String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/metrics/booleanexpressioncomplexity";
38 }
39
40 @Test
41 public void test() throws Exception {
42
43 final String[] expected = {
44 "21:9: " + getCheckMessage(MSG_KEY, 4, 3),
45 "38:46: " + getCheckMessage(MSG_KEY, 4, 3),
46 "48:9: " + getCheckMessage(MSG_KEY, 6, 3),
47 "54:34: " + getCheckMessage(MSG_KEY, 4, 3),
48 "56:34: " + getCheckMessage(MSG_KEY, 4, 3),
49 };
50
51 verifyWithInlineConfigParser(
52 getPath("InputBooleanExpressionComplexity.java"), expected);
53 }
54
55 @Test
56 public void testNoBitwise() throws Exception {
57
58 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
59
60 verifyWithInlineConfigParser(
61 getPath("InputBooleanExpressionComplexity2.java"), expected);
62 }
63
64 @Test
65 public void testNullPointerException() throws Exception {
66
67 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
68
69 verifyWithInlineConfigParser(
70 getPath("InputBooleanExpressionComplexityNPE.java"), expected);
71 }
72
73 @Test
74 public void testWrongToken() {
75 final BooleanExpressionComplexityCheck booleanExpressionComplexityCheckObj =
76 new BooleanExpressionComplexityCheck();
77 final DetailAstImpl ast = new DetailAstImpl();
78 ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
79 try {
80 booleanExpressionComplexityCheckObj.visitToken(ast);
81 assertWithMessage("exception expected").fail();
82 }
83 catch (IllegalArgumentException ex) {
84 assertWithMessage("Invalid exception message")
85 .that(ex.getMessage())
86 .isEqualTo("Unknown type: interface[0x-1]");
87 }
88 }
89
90 @Test
91 public void testSmall() throws Exception {
92
93 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
94
95 verifyWithInlineConfigParser(
96 getPath("InputBooleanExpressionComplexitySmall.java"), expected);
97 }
98
99 @Test
100 public void testBooleanExpressionComplexityRecordsAndCompactCtors() throws Exception {
101
102 final int max = 3;
103
104 final String[] expected = {
105 "16:12: " + getCheckMessage(MSG_KEY, 4, max),
106 "24:23: " + getCheckMessage(MSG_KEY, 4, max),
107 "35:23: " + getCheckMessage(MSG_KEY, 4, max),
108 "45:27: " + getCheckMessage(MSG_KEY, 4, max),
109 };
110
111 verifyWithInlineConfigParser(
112 getNonCompilablePath(
113 "InputBooleanExpressionComplexityRecordsAndCompactCtors.java"),
114 expected);
115 }
116
117 @Test
118 public void testLeaves() throws Exception {
119
120 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
121
122 verifyWithInlineConfigParser(
123 getPath("InputBooleanExpressionComplexityLeaves.java"), expected);
124 }
125
126 @Test
127 public void testRecordLeaves() throws Exception {
128
129 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
130
131 verifyWithInlineConfigParser(
132 getNonCompilablePath("InputBooleanExpressionComplexityRecordLeaves.java"),
133 expected);
134 }
135
136 @Test
137 public void testWhenExpression() throws Exception {
138
139 final int max = 0;
140
141 final String[] expected = {
142 "17:21: " + getCheckMessage(MSG_KEY, 6, max),
143 "21:17: " + getCheckMessage(MSG_KEY, 6, max),
144 "25:27: " + getCheckMessage(MSG_KEY, 6, max),
145 "29:48: " + getCheckMessage(MSG_KEY, 1, max),
146 };
147
148 verifyWithInlineConfigParser(
149 getNonCompilablePath("InputBooleanExpressionComplexityWhenExpression.java"),
150 expected);
151 }
152
153 }