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.naming;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
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 MemberNameCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/naming/membername";
37 }
38
39 @Test
40 public void testGetRequiredTokens() {
41 final MemberNameCheck checkObj = new MemberNameCheck();
42 final int[] expected = {TokenTypes.VARIABLE_DEF};
43 assertWithMessage("Default required tokens are invalid")
44 .that(checkObj.getRequiredTokens())
45 .isEqualTo(expected);
46 }
47
48 @Test
49 public void testSpecifiedOne()
50 throws Exception {
51
52 final String pattern = "^m[A-Z][a-zA-Z0-9]*$";
53
54 final String[] expected = {
55 "41:17: " + getCheckMessage(MSG_INVALID_PATTERN, "badMember", pattern),
56 };
57 verifyWithInlineConfigParser(
58 getPath("InputMemberNameSimpleOne.java"), expected);
59 }
60
61 @Test
62 public void testSpecifiedTwo()
63 throws Exception {
64
65 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
66 verifyWithInlineConfigParser(
67 getPath("InputMemberNameSimpleTwo.java"), expected);
68 }
69
70 @Test
71 public void testSpecifiedThree()
72 throws Exception {
73
74 final String pattern = "^m[A-Z][a-zA-Z0-9]*$";
75
76 final String[] expected = {
77 "50:17: " + getCheckMessage(MSG_INVALID_PATTERN, "someMember", pattern),
78 };
79 verifyWithInlineConfigParser(
80 getPath("InputMemberNameSimpleThree.java"), expected);
81 }
82
83 @Test
84 public void testInnerClass()
85 throws Exception {
86
87 final String pattern = "^[a-z][a-zA-Z0-9]*$";
88
89 final String[] expected = {
90 "63:25: " + getCheckMessage(MSG_INVALID_PATTERN, "ABC", pattern),
91 };
92 verifyWithInlineConfigParser(
93 getPath("InputMemberNameInner.java"), expected);
94 }
95
96 @Test
97 public void testDefaults() throws Exception {
98
99 final String pattern = "^[a-z][a-zA-Z0-9]*$";
100
101 final String[] expected = {
102 "21:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
103 "22:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
104 "23:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
105 "24:17: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
106 };
107 verifyWithInlineConfigParser(
108 getPath("InputMemberName.java"), expected);
109 }
110
111 @Test
112 public void testUnderlined() throws Exception {
113
114 final String pattern = "^_[a-z]*$";
115
116 final String[] expected = {
117 "16:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
118 "17:19: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
119 "18:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
120 "19:17: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
121 };
122 verifyWithInlineConfigParser(
123 getPath("InputMemberName2.java"), expected);
124 }
125
126 @Test
127 public void testPublicOnly() throws Exception {
128
129 final String pattern = "^_[a-z]*$";
130
131 final String[] expected = {
132 "16:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
133 };
134 verifyWithInlineConfigParser(
135 getPath("InputMemberName3.java"), expected);
136 }
137
138 @Test
139 public void testProtectedOnly() throws Exception {
140
141 final String pattern = "^_[a-z]*$";
142
143 final String[] expected = {
144 "17:19: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
145 };
146 verifyWithInlineConfigParser(
147 getPath("InputMemberName4.java"), expected);
148 }
149
150 @Test
151 public void testPackageOnly() throws Exception {
152
153 final String pattern = "^_[a-z]*$";
154
155 final String[] expected = {
156 "18:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
157 };
158 verifyWithInlineConfigParser(
159 getPath("InputMemberName5.java"), expected);
160 }
161
162 @Test
163 public void testPrivateOnly() throws Exception {
164
165 final String pattern = "^_[a-z]*$";
166
167 final String[] expected = {
168 "19:17: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
169 };
170 verifyWithInlineConfigParser(
171 getPath("InputMemberName6.java"), expected);
172 }
173
174 @Test
175 public void testNotPrivate() throws Exception {
176
177 final String pattern = "^[a-z][a-zA-Z0-9]*$";
178
179 final String[] expected = {
180 "21:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
181 "22:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
182 "23:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
183 };
184 verifyWithInlineConfigParser(
185 getPath("InputMemberName7.java"), expected);
186 }
187
188 @Test
189 public void memberNameExtended() throws Exception {
190
191 final String pattern = "^[a-z][a-z0-9][a-zA-Z0-9]*$";
192
193 final String[] expected = {
194 "19:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
195 "20:19: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
196 "21:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
197 "22:17: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
198 "24:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
199 "25:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
200 "26:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
201 "27:17: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
202 "30:20: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
203 "31:23: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
204 "32:13: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
205 "33:21: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
206 "35:20: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
207 "36:23: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
208 "37:13: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
209 "38:21: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
210 "42:20: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
211 "43:23: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
212 "44:13: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
213 "45:21: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
214 "47:20: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
215 "48:23: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
216 "49:13: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
217 "50:21: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
218 "74:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
219 "75:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
220 "76:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
221 "77:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
222 "79:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
223 "80:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
224 "81:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
225 "82:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
226 };
227 verifyWithInlineConfigParser(
228 getPath("InputMemberNameExtended.java"), expected);
229 }
230
231 @Test
232 public void testGetAcceptableTokens() {
233 final MemberNameCheck memberNameCheckObj = new MemberNameCheck();
234 final int[] actual = memberNameCheckObj.getAcceptableTokens();
235 final int[] expected = {
236 TokenTypes.VARIABLE_DEF,
237 };
238 assertWithMessage("Default acceptable tokens are invalid")
239 .that(actual)
240 .isEqualTo(expected);
241 }
242
243 }