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 PatternVariableNameCheckTest
31 extends AbstractModuleTestSupport {
32
33 @Override
34 protected String getPackageLocation() {
35 return "com/puppycrawl/tools/checkstyle/checks/naming/patternvariablename";
36 }
37
38 @Test
39 public void testGetAcceptableTokens() {
40 final PatternVariableNameCheck patternVariableNameCheck = new PatternVariableNameCheck();
41 final int[] expected = {TokenTypes.PATTERN_VARIABLE_DEF};
42
43 assertWithMessage("Default acceptable tokens are invalid")
44 .that(patternVariableNameCheck.getAcceptableTokens())
45 .isEqualTo(expected);
46 }
47
48 @Test
49 public void testDefault() throws Exception {
50
51 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
52
53 final String[] expected = {
54 "18:39: " + getCheckMessage(MSG_INVALID_PATTERN, "OTHER", pattern),
55 "28:34: " + getCheckMessage(MSG_INVALID_PATTERN, "Count", pattern),
56 "43:36: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
57 "44:42: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
58 "47:34: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
59 "48:43: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
60 "60:37: " + getCheckMessage(MSG_INVALID_PATTERN, "INTEGER", pattern),
61 "66:43: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing1", pattern),
62 "70:41: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing2", pattern),
63 };
64 verifyWithInlineConfigParser(
65 getNonCompilablePath(
66 "InputPatternVariableNameEnhancedInstanceofTestDefault.java"),
67 expected);
68 }
69
70 @Test
71 public void testPatternVariableNameNoSingleChar() throws Exception {
72
73 final String pattern = "^[a-z][a-zA-Z0-9]+$";
74
75 final String[] expected = {
76 "18:39: " + getCheckMessage(MSG_INVALID_PATTERN, "OTHER", pattern),
77 "23:33: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
78 "28:34: " + getCheckMessage(MSG_INVALID_PATTERN, "Count", pattern),
79 "43:36: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
80 "44:42: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
81 "46:34: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
82 "47:43: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
83 "49:57: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
84 "56:48: " + getCheckMessage(MSG_INVALID_PATTERN, "a", pattern),
85 "57:39: " + getCheckMessage(MSG_INVALID_PATTERN, "x", pattern),
86 "58:43: " + getCheckMessage(MSG_INVALID_PATTERN, "y", pattern),
87 "60:37: " + getCheckMessage(MSG_INVALID_PATTERN, "INTEGER", pattern),
88 "65:43: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing1", pattern),
89 "69:41: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing2", pattern),
90 "74:38: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
91 "75:36: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
92 "76:37: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
93 "83:41: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
94 };
95
96 verifyWithInlineConfigParser(
97 getNonCompilablePath(
98 "InputPatternVariableNameEnhancedInstanceofNoSingleChar.java"),
99 expected);
100 }
101
102 @Test
103 public void testPatternVariableNameUnnamed() throws Exception {
104
105 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
106
107 final String[] expected = {
108 "17:33: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
109 "19:33: " + getCheckMessage(MSG_INVALID_PATTERN, "_s", pattern),
110 "22:33: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
111 "29:25: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
112 "32:25: " + getCheckMessage(MSG_INVALID_PATTERN, "_s", pattern),
113 "40:67: " + getCheckMessage(MSG_INVALID_PATTERN, "_Color", pattern),
114 "45:59: " + getCheckMessage(MSG_INVALID_PATTERN, "_Color", pattern),
115 "51:76: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
116 };
117
118 verifyWithInlineConfigParser(
119 getNonCompilablePath(
120 "InputPatternVariableNameUnnamed.java"),
121 expected);
122 }
123
124 @Test
125 public void testPatternVariableNameRecordPattern() throws Exception {
126
127 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
128
129 final String[] expected = {
130 "15:36: " + getCheckMessage(MSG_INVALID_PATTERN, "XX", pattern),
131 "15:44: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
132 "20:49: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
133 "25:28: " + getCheckMessage(MSG_INVALID_PATTERN, "XX", pattern),
134 "25:36: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
135 "31:41: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
136 };
137
138 verifyWithInlineConfigParser(
139 getNonCompilablePath(
140 "InputPatternVariableNameRecordPattern.java"),
141 expected);
142 }
143 }