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 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 "26: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 }