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 = "^(?!var$|\\S*\\$)\\S+$";
78
79 final String[] expected = {
80 "57:13: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
81 "59:13: " + getCheckMessage(MSG_INVALID_PATTERN, "$amt", format),
82 "74:52: " + getCheckMessage(MSG_INVALID_PATTERN, "yield$text", format),
83 "74:74: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
84 };
85 verifyWithInlineConfigParser(
86 getNonCompilablePath("InputIllegalIdentifierName.java"), expected);
87 }
88
89 @Test
90 public void testIllegalIdentifierNameOpenTransitive() throws Exception {
91 final String format = "(?i)^(?!(record|yield|var|permits|sealed|open|transitive)$).+$";
92
93 final String[] expected = {
94 "21:25: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
95 "22:24: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
96 "28:13: " + getCheckMessage(MSG_INVALID_PATTERN, "open", format),
97 "30:21: " + getCheckMessage(MSG_INVALID_PATTERN, "transitive", format),
98 "45:9: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
99 "57:13: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
100 "59:13: " + getCheckMessage(MSG_INVALID_PATTERN, "record", format),
101 "61:16: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
102 "63:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Record", format),
103 "64:25: " + getCheckMessage(MSG_INVALID_PATTERN, "transitive", format),
104 "73:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Transitive", format),
105 "76:37: " + getCheckMessage(MSG_INVALID_PATTERN, "transitive", format),
106 "76:56: " + getCheckMessage(MSG_INVALID_PATTERN, "yield", format),
107 "76:72: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
108 };
109 verifyWithInlineConfigParser(
110 getNonCompilablePath("InputIllegalIdentifierNameOpenTransitive.java"), expected);
111 }
112
113 @Test
114 public void testIllegalIdentifierNameParameterReceiver() throws Exception {
115
116 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
117
118 verifyWithInlineConfigParser(
119 getNonCompilablePath("InputIllegalIdentifierNameParameterReceiver.java"),
120 expected);
121 }
122
123 @Test
124 public void testIllegalIdentifierNameUnderscore() throws Exception {
125 final String format = "(?i)^(?!(record|yield|var|permits|sealed|_)$).+$";
126
127 final String[] expected = {
128 "18:12: " + getCheckMessage(MSG_INVALID_PATTERN, "_", format),
129 };
130 verifyWithInlineConfigParser(
131 getNonCompilablePath("InputIllegalIdentifierNameUnderscore.java"), expected);
132 }
133
134 @Test
135 public void testIllegalIdentifierNameLambda() throws Exception {
136 final String format = "^(?!var$|\\S*\\$)\\S+$";
137
138 final String[] expected = {
139 "19:39: " + getCheckMessage(MSG_INVALID_PATTERN, "param$", format),
140 "20:40: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
141 "32:9: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
142 "42:47: " + getCheckMessage(MSG_INVALID_PATTERN, "te$t", format),
143 };
144 verifyWithInlineConfigParser(
145 getNonCompilablePath("InputIllegalIdentifierNameLambda.java"), expected);
146 }
147
148 @Test
149 public void testIllegalIdentifierNameUnnamedVariable() throws Exception {
150
151 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
152
153 verifyWithInlineConfigParser(
154 getNonCompilablePath("InputIllegalIdentifierNameUnnamedVariables.java"), expected);
155 }
156
157 @Test
158 public void testIllegalIdentifierNameRecordPattern() throws Exception {
159 final String format = "^(?!var$|\\S*\\$)\\S+$";
160
161 final String[] expected = {
162 "16:36: " + getCheckMessage(MSG_INVALID_PATTERN, "var", format),
163 "23:39: " + getCheckMessage(MSG_INVALID_PATTERN, "permit$", format),
164 };
165 verifyWithInlineConfigParser(
166 getNonCompilablePath("InputIllegalIdentifierNameRecordPattern.java"), expected);
167 }
168 }