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 LocalVariableNameCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/naming/localvariablename";
37 }
38
39 @Test
40 public void testGetAcceptableTokens() {
41 final LocalVariableNameCheck localVariableNameCheck = new LocalVariableNameCheck();
42 final int[] expected = {TokenTypes.VARIABLE_DEF};
43
44 assertWithMessage("Default acceptable tokens are invalid")
45 .that(localVariableNameCheck.getAcceptableTokens())
46 .isEqualTo(expected);
47 }
48
49 @Test
50 public void testDefault()
51 throws Exception {
52
53 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
54
55 final String[] expected = {
56 "122:13: " + getCheckMessage(MSG_INVALID_PATTERN, "ABC", pattern),
57 "133:18: " + getCheckMessage(MSG_INVALID_PATTERN, "I", pattern),
58 "135:20: " + getCheckMessage(MSG_INVALID_PATTERN, "InnerBlockVariable", pattern),
59 "210:21: " + getCheckMessage(MSG_INVALID_PATTERN, "O", pattern),
60 };
61 verifyWithInlineConfigParser(
62 getPath("InputLocalVariableName.java"), expected);
63 }
64
65 @Test
66 public void testInnerClass()
67 throws Exception {
68 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
69 verifyWithInlineConfigParser(
70 getPath("InputLocalVariableNameInnerClass.java"), expected);
71 }
72
73 @Test
74 public void testLoopVariables()
75 throws Exception {
76
77 final String pattern = "^[a-z]{2,}[a-zA-Z0-9]*$";
78
79 final String[] expected = {
80 "19:29: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
81 "22:17: " + getCheckMessage(MSG_INVALID_PATTERN, "A", pattern),
82 "24:21: " + getCheckMessage(MSG_INVALID_PATTERN, "i", pattern),
83 "30:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Index", pattern),
84 "47:32: " + getCheckMessage(MSG_INVALID_PATTERN, "a", pattern),
85 "50:32: " + getCheckMessage(MSG_INVALID_PATTERN, "B", pattern),
86 };
87 verifyWithInlineConfigParser(
88 getPath("InputLocalVariableNameOneCharInitVarName.java"), expected);
89 }
90
91 @Test
92 public void testUnnamedVariables() throws Exception {
93 final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
94
95 final String[] expected = {
96 "16:13: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
97 "17:13: " + getCheckMessage(MSG_INVALID_PATTERN, "_result", pattern),
98 "33:22: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
99 };
100 verifyWithInlineConfigParser(
101 getNonCompilablePath("InputLocalVariableNameUnnamedVariables.java"), expected);
102
103 }
104
105 }