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.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.UnusedLambdaParameterShouldBeUnnamedCheck.MSG_UNUSED_LAMBDA_PARAMETER;
24
25 import java.io.File;
26 import java.util.Collection;
27 import java.util.Optional;
28
29 import org.junit.jupiter.api.Test;
30
31 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32 import com.puppycrawl.tools.checkstyle.JavaParser;
33 import com.puppycrawl.tools.checkstyle.api.DetailAST;
34 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36
37 public class UnusedLambdaParameterShouldBeUnnamedCheckTest extends AbstractModuleTestSupport {
38
39 @Override
40 protected String getPackageLocation() {
41 return "com/puppycrawl/tools/checkstyle/checks/coding/unusedlambdaparametershouldbeunnamed";
42 }
43
44 @Test
45 public void testGetRequiredTokens() {
46 final UnusedLambdaParameterShouldBeUnnamedCheck checkObj =
47 new UnusedLambdaParameterShouldBeUnnamedCheck();
48 final int[] expected = {
49 TokenTypes.LAMBDA,
50 TokenTypes.IDENT,
51 };
52 assertWithMessage("Default required tokens are invalid")
53 .that(checkObj.getRequiredTokens())
54 .isEqualTo(expected);
55 assertWithMessage("Default acceptable tokens are invalid")
56 .that(checkObj.getAcceptableTokens())
57 .isEqualTo(expected);
58 assertWithMessage("Default tokens are invalid")
59 .that(checkObj.getDefaultTokens())
60 .isEqualTo(expected);
61 }
62
63 @Test
64 public void testSingleLambdaParameter() throws Exception {
65 final String[] expected = {
66 "18:45: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "character"),
67 "23:32: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "character"),
68 "26:32: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "character"),
69 "31:32: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "character"),
70 "34:32: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "Character"),
71 "40:32: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "character"),
72 "45:32: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "C"),
73 "51:45: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "s"),
74 "54:32: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "C"),
75 };
76 verifyWithInlineConfigParser(
77 getNonCompilablePath(
78 "InputUnusedLambdaParameterShouldBeUnnamedSingleLambdaParameter.java"),
79 expected);
80 }
81
82 @Test
83 public void testMultipleLambdaParameters() throws Exception {
84 final String[] expected = {
85 "18:56: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "x"),
86 "18:59: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
87 "26:24: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
88 "29:21: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "x"),
89 "33:21: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "x"),
90 "33:24: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
91 "39:21: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "X"),
92 "45:21: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "x"),
93 "45:24: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
94 "53:21: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "X"),
95 "83:21: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "x"),
96 "90:24: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
97 };
98 verifyWithInlineConfigParser(
99 getNonCompilablePath(
100 "InputUnusedLambdaParameterShouldBeUnnamedMultipleParameters.java"),
101 expected);
102 }
103
104 @Test
105 public void testNested() throws Exception {
106 final String[] expected = {
107 "17:56: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "x"),
108 "17:59: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
109 "21:51: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "z"),
110 "29:51: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "z"),
111 "45:51: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "z"),
112 "53:24: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
113 "55:61: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "z"),
114 "64:61: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "z"),
115 "64:64: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "w"),
116 "75:64: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "w"),
117 "85:73: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "y"),
118 "87:60: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "z"),
119 "94:28: " + getCheckMessage(MSG_UNUSED_LAMBDA_PARAMETER, "x"),
120 };
121 verifyWithInlineConfigParser(
122 getNonCompilablePath(
123 "InputUnusedLambdaParameterShouldBeUnnamedNested.java"),
124 expected);
125 }
126
127 @Test
128 public void testClearState() throws Exception {
129 final UnusedLambdaParameterShouldBeUnnamedCheck check =
130 new UnusedLambdaParameterShouldBeUnnamedCheck();
131
132 final DetailAST root = JavaParser.parseFile(
133 new File(getNonCompilablePath(
134 "InputUnusedLambdaParameterShouldBeUnnamedSingleLambdaParameter.java")),
135 JavaParser.Options.WITHOUT_COMMENTS);
136
137 final Optional<DetailAST> literalCatch = TestUtil.findTokenInAstByPredicate(root,
138 ast -> ast.getType() == TokenTypes.LAMBDA);
139
140 assertWithMessage("State is not cleared on beginTree")
141 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
142 literalCatch.orElseThrow(),
143 "lambdaParameters",
144 lambdaParameters -> {
145 return ((Collection<?>) lambdaParameters).isEmpty();
146 }))
147 .isTrue();
148 }
149 }