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 IllegalIdentifierNameCheckTest extends AbstractModuleTestSupport {
32
33 @Override
34 protected String getPackageLocation() {
35 return "com/puppycrawl/tools/checkstyle/checks/naming/illegalidentifiername";
36 }
37
38 @Test
39 public void testGetAcceptableTokens() {
40 final IllegalIdentifierNameCheck illegalIdentifierNameCheck =
41 new IllegalIdentifierNameCheck();
42 final int[] expected = {
43 TokenTypes.CLASS_DEF,
44 TokenTypes.INTERFACE_DEF,
45 TokenTypes.ENUM_DEF,
46 TokenTypes.ANNOTATION_DEF,
47 TokenTypes.ANNOTATION_FIELD_DEF,
48 TokenTypes.PARAMETER_DEF,
49 TokenTypes.VARIABLE_DEF,
50 TokenTypes.METHOD_DEF,
51 TokenTypes.ENUM_CONSTANT_DEF,
52 TokenTypes.PATTERN_VARIABLE_DEF,
53 TokenTypes.RECORD_DEF,
54 TokenTypes.RECORD_COMPONENT_DEF,
55 TokenTypes.LAMBDA,
56 };
57
58 assertWithMessage("Default acceptable tokens are invalid")
59 .that(illegalIdentifierNameCheck.getAcceptableTokens())
60 .isEqualTo(expected);
61 }
62
63 @Test
64 public void testGetRequiredTokens() {
65 final IllegalIdentifierNameCheck illegalIdentifierNameCheck =
66 new IllegalIdentifierNameCheck();
67 final int[] expected = CommonUtil.EMPTY_INT_ARRAY;
68
69 assertWithMessage("Default required tokens are invalid")
70 .that(illegalIdentifierNameCheck.getRequiredTokens())
71 .isEqualTo(expected);
72 }
73
74 @Test
75 public void testIllegalIdentifierNameDefault() throws Exception {
76
77 final String format = "(?i)^(?!(record|yield|var|permits|sealed)$).+$";
78
79 final String[] expected = {
80 "21:25: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
81 "22:24: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
82 "28:13: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
83 "30:21: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
84 "45:9: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
85 "57:13: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
86 "59:13: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
87 "61:16: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
88 "63:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Record", format),
89 "64:25: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
90 "74:37: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
91 "74:52: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
92 "74:69: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
93 };
94 verifyWithInlineConfigParser(
95 getNonCompilablePath("InputIllegalIdentifierName.java"), expected);
96 }
97
98 @Test
99 public void testIllegalIdentifierNameOpenTransitive() throws Exception {
100 final String format = "(?i)^(?!(record|yield|var|permits|sealed|open|transitive)$).+$";
101
102 final String[] expected = {
103 "21:25: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
104 "22:24: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
105 "28:13: " + getCheckMessage(MSG_INVALID_PATTERN, "open", format),
106 "30:21: " + getCheckMessage(MSG_INVALID_PATTERN, "transitive", format),
107 "45:9: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
108 "57:13: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
109 "59:13: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
110 "61:16: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
111 "63:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Record", format),
112 "64:25: " + getCheckMessage(MSG_INVALID_PATTERN, "transitive", format),
113 "73:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Transitive", format),
114 "76:37: " + getCheckMessage(MSG_INVALID_PATTERN, "transitive", format),
115 "76:56: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
116 "76:72: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
117 };
118 verifyWithInlineConfigParser(
119 getNonCompilablePath("InputIllegalIdentifierNameOpenTransitive.java"), expected);
120 }
121
122 @Test
123 public void testIllegalIdentifierNameParameterReceiver() throws Exception {
124
125 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
126
127 verifyWithInlineConfigParser(
128 getNonCompilablePath("InputIllegalIdentifierNameParameterReceiver.java"),
129 expected);
130 }
131
132 @Test
133 public void testIllegalIdentifierNameUnderscore() throws Exception {
134 final String format = "(?i)^(?!(record|yield|var|permits|sealed|_)$).+$";
135
136 final String[] expected = {
137 "18:12: " + getCheckMessage(MSG_INVALID_PATTERN, "_", format),
138 };
139 verifyWithInlineConfigParser(
140 getNonCompilablePath("InputIllegalIdentifierNameUnderscore.java"), expected);
141 }
142
143 @Test
144 public void testIllegalIdentifierNameLambda() throws Exception {
145 final String format = "(?i)^(?!(record|yield|var|permits|sealed)$).+$";
146
147 final String[] expected = {
148 "19:39: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
149 "20:40: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
150 "32:9: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
151 "35:9: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
152 "42:47: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
153 };
154 verifyWithInlineConfigParser(
155 getNonCompilablePath("InputIllegalIdentifierNameLambda.java"), expected);
156 }
157
158 @Test
159 public void testIllegalIdentifierNameUnnamedVariable() throws Exception {
160
161 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
162
163 verifyWithInlineConfigParser(
164 getNonCompilablePath("InputIllegalIdentifierNameUnnamedVariables.java"), expected);
165 }
166
167 @Test
168 public void testIllegalIdentifierNameRecordPattern() throws Exception {
169 final String format = "(?i)^(?!(record|yield|var|permits|sealed)$).+$";
170
171 final String[] expected = {
172 "16:36: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
173 "17:47: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
174 "17:59: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
175 "17:74: " + getCheckMessage(MSG_INVALID_PATTERN, "sealed", format),
176 "26:28: " + getCheckMessage(MSG_INVALID_PATTERN, "permits", format),
177 "26:41: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
178 "30:39: " + getCheckMessage(MSG_INVALID_PATTERN, "permits", format),
179 };
180 verifyWithInlineConfigParser(
181 getNonCompilablePath("InputIllegalIdentifierNameRecordPattern.java"), expected);
182 }
183 }