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  
30  public class LambdaParameterNameCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/naming/lambdaparametername";
35      }
36  
37      @Test
38      public void testGetRequiredTokens() {
39          final int[] expected = {
40              TokenTypes.LAMBDA,
41          };
42  
43          final LambdaParameterNameCheck check = new LambdaParameterNameCheck();
44          final int[] requiredTokens = check.getRequiredTokens();
45          assertWithMessage("Invalid required tokens")
46              .that(requiredTokens)
47              .isEqualTo(expected);
48      }
49  
50      @Test
51      public void testAcceptableTokens() {
52          final int[] expected = {
53              TokenTypes.LAMBDA,
54          };
55  
56          final LambdaParameterNameCheck check = new LambdaParameterNameCheck();
57          final int[] acceptableTokens = check.getAcceptableTokens();
58          assertWithMessage("Invalid acceptable tokens")
59              .that(acceptableTokens)
60              .isEqualTo(expected);
61      }
62  
63      @Test
64      public void testParametersInLambda() throws Exception {
65  
66          final String pattern = "^(id)|([a-z][a-z0-9][a-zA-Z0-9]+)$";
67  
68          final String[] expected = {
69              "15:68: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
70              "18:66: " + getCheckMessage(MSG_INVALID_PATTERN, "st", pattern),
71              "21:65: " + getCheckMessage(MSG_INVALID_PATTERN, "s1", pattern),
72              "22:65: " + getCheckMessage(MSG_INVALID_PATTERN, "s2", pattern),
73              "25:21: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
74          };
75          verifyWithInlineConfigParser(
76                  getPath("InputLambdaParameterName.java"), expected);
77      }
78  
79      @Test
80      public void testLambdaParameterNameSwitchExpression() throws Exception {
81  
82          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
83  
84          final String[] expected = {
85              "19:35: " + getCheckMessage(MSG_INVALID_PATTERN, "Word", pattern),
86              "31:35: " + getCheckMessage(MSG_INVALID_PATTERN, "Word", pattern),
87              "36:31: " + getCheckMessage(MSG_INVALID_PATTERN, "Word", pattern),
88              "46:35: " + getCheckMessage(MSG_INVALID_PATTERN, "Word", pattern),
89              "57:35: " + getCheckMessage(MSG_INVALID_PATTERN, "Word", pattern),
90          };
91  
92          verifyWithInlineConfigParser(
93                  getNonCompilablePath("InputLambdaParameterNameSwitchExpression.java"),
94              expected);
95      }
96  
97      @Test
98      public void testLambdaParameterNameUnnamed() throws Exception {
99  
100         final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
101 
102         final String[] expected = {
103             "30:36: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
104             "34:36: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
105             "37:36: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD_", pattern),
106         };
107 
108         verifyWithInlineConfigParser(
109                 getNonCompilablePath("InputLambdaParameterNameUnnamed.java"),
110             expected);
111     }
112 
113 }