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.MethodLengthCheck.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 MethodLengthCheckTest extends AbstractModuleTestSupport {
32
33 @Override
34 protected String getPackageLocation() {
35 return "com/puppycrawl/tools/checkstyle/checks/sizes/methodlength";
36 }
37
38 @Test
39 public void testGetRequiredTokens() {
40 final MethodLengthCheck checkObj = new MethodLengthCheck();
41 assertWithMessage("MethodLengthCheck#getRequiredTokens should return empty array "
42 + "by default")
43 .that(checkObj.getRequiredTokens())
44 .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
45 }
46
47 @Test
48 public void testGetAcceptableTokens() {
49 final MethodLengthCheck methodLengthCheckObj =
50 new MethodLengthCheck();
51 final int[] actual = methodLengthCheckObj.getAcceptableTokens();
52 final int[] expected = {
53 TokenTypes.METHOD_DEF,
54 TokenTypes.CTOR_DEF,
55 TokenTypes.COMPACT_CTOR_DEF,
56 };
57
58 assertWithMessage("Default acceptable tokens are invalid")
59 .that(actual)
60 .isEqualTo(expected);
61 }
62
63 @Test
64 public void testItOne() throws Exception {
65 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
66 verifyWithInlineConfigParser(
67 getPath("InputMethodLengthSimpleOne.java"), expected);
68 }
69
70 @Test
71 public void testItTwo() throws Exception {
72 final String[] expected = {
73 "17:5: " + getCheckMessage(MSG_KEY, 20, 19, "longMethod"),
74 };
75 verifyWithInlineConfigParser(
76 getPath("InputMethodLengthSimpleTwo.java"), expected);
77 }
78
79 @Test
80 public void testCountEmptyIsFalse() throws Exception {
81 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
82 verifyWithInlineConfigParser(
83 getPath("InputMethodLengthCountEmptyIsFalse.java"), expected);
84 }
85
86 @Test
87 public void testCountEmptyIsFalseTwo() throws Exception {
88 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89 verifyWithInlineConfigParser(
90 getPath("InputMethodLengthCountEmptyIsFalseTwo.java"), expected);
91 }
92
93 @Test
94 public void testCountEmptyIsFalseThree() throws Exception {
95 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
96 verifyWithInlineConfigParser(
97 getPath("InputMethodLengthCountEmptyIsFalseThree.java"), expected);
98 }
99
100 @Test
101 public void testWithComments() throws Exception {
102 final String[] expected = {
103 "35:5: " + getCheckMessage(MSG_KEY, 8, 7, "visit"),
104 };
105 verifyWithInlineConfigParser(
106 getPath("InputMethodLengthComments.java"), expected);
107 }
108
109 @Test
110 public void testCountEmpty() throws Exception {
111 final int max = 2;
112 final String[] expected = {
113 "24:5: " + getCheckMessage(MSG_KEY, 3, max, "AA"),
114 "41:5: " + getCheckMessage(MSG_KEY, 3, max, "threeLines"),
115 "45:5: " + getCheckMessage(MSG_KEY, 3, max, "threeLinesAndComments"),
116 "53:5: " + getCheckMessage(MSG_KEY, 3, max, "threeLinesWrap"),
117 "63:5: " + getCheckMessage(MSG_KEY, 10, max, "m2"),
118 };
119 verifyWithInlineConfigParser(
120 getPath("InputMethodLengthCountEmptySmallSize.java"), expected);
121 }
122
123 @Test
124 public void testAbstractOne() throws Exception {
125 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
126 verifyWithInlineConfigParser(
127 getPath("InputMethodLengthModifierOne.java"), expected);
128 }
129
130 @Test
131 public void testAbstractTwo() throws Exception {
132 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
133 verifyWithInlineConfigParser(
134 getPath("InputMethodLengthModifierTwo.java"), expected);
135 }
136
137 @Test
138 public void testTextBlocks() throws Exception {
139 final int max = 2;
140
141 final String[] expected = {
142 "14:5: " + getCheckMessage(MSG_KEY, 21, max, "longEmptyTextBlock"),
143 "43:5: " + getCheckMessage(MSG_KEY, 10, max, "textBlock2"),
144 "57:5: " + getCheckMessage(MSG_KEY, 8, max, "textBlockWithIndent"),
145 "66:5: " + getCheckMessage(MSG_KEY, 12, max, "textBlockNoIndent"),
146 };
147
148 verifyWithInlineConfigParser(
149 getNonCompilablePath("InputMethodLengthTextBlocksCountEmpty.java"),
150 expected);
151 }
152
153 @Test
154 public void testRecordsAndCompactCtors() throws Exception {
155
156 final int max = 2;
157
158 final String[] expected = {
159 "26:9: " + getCheckMessage(MSG_KEY, 6, max, "MyTestRecord2"),
160 "35:9: " + getCheckMessage(MSG_KEY, 5, max, "foo"),
161 "44:9: " + getCheckMessage(MSG_KEY, 7, max, "MyTestRecord4"),
162 "65:9: " + getCheckMessage(MSG_KEY, 15, max, "m"),
163 "68:17: " + getCheckMessage(MSG_KEY, 8, max, "R76"),
164 };
165
166 verifyWithInlineConfigParser(
167 getNonCompilablePath("InputMethodLengthRecordsAndCompactCtors.java"),
168 expected);
169 }
170
171 @Test
172 public void testRecordsAndCompactCtorsCountEmpty() throws Exception {
173 final int max = 2;
174
175 final String[] expected = {
176 "25:9: " + getCheckMessage(MSG_KEY, 3, max, "MyTestRecord2"),
177 "32:9: " + getCheckMessage(MSG_KEY, 3, max, "foo"),
178 "38:9: " + getCheckMessage(MSG_KEY, 3, max, "MyTestRecord4"),
179 "55:9: " + getCheckMessage(MSG_KEY, 13, max, "m"),
180 "58:17: " + getCheckMessage(MSG_KEY, 8, max, "R76"),
181 };
182
183 verifyWithInlineConfigParser(
184 getNonCompilablePath("InputMethodLengthCompactCtorsCountEmpty.java"),
185 expected);
186 }
187
188 }