View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  }