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