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.ParameterAssignmentCheck.MSG_KEY;
24
25 import java.io.File;
26 import java.util.Collection;
27 import java.util.Optional;
28 import java.util.Set;
29
30 import org.junit.jupiter.api.Test;
31
32 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33 import com.puppycrawl.tools.checkstyle.JavaParser;
34 import com.puppycrawl.tools.checkstyle.api.DetailAST;
35 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
37 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
38
39 public class ParameterAssignmentCheckTest extends AbstractModuleTestSupport {
40
41 @Override
42 protected String getPackageLocation() {
43 return "com/puppycrawl/tools/checkstyle/checks/coding/parameterassignment";
44 }
45
46 @Test
47 public void testDefault()
48 throws Exception {
49 final String[] expected = {
50 "17:15: " + getCheckMessage(MSG_KEY, "field"),
51 "18:15: " + getCheckMessage(MSG_KEY, "field"),
52 "20:14: " + getCheckMessage(MSG_KEY, "field"),
53 "29:30: " + getCheckMessage(MSG_KEY, "field1"),
54 "46:31: " + getCheckMessage(MSG_KEY, "q"),
55 "49:39: " + getCheckMessage(MSG_KEY, "q"),
56 "51:34: " + getCheckMessage(MSG_KEY, "w"),
57 "56:41: " + getCheckMessage(MSG_KEY, "w"),
58 "59:49: " + getCheckMessage(MSG_KEY, "a"),
59 "62:11: " + getCheckMessage(MSG_KEY, "c"),
60 "63:11: " + getCheckMessage(MSG_KEY, "d"),
61 "73:15: " + getCheckMessage(MSG_KEY, "d"),
62 };
63 verifyWithInlineConfigParser(
64 getPath("InputParameterAssignmentWithUnchecked.java"),
65 expected);
66 }
67
68 @Test
69 public void testReceiverParameter() throws Exception {
70 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
71 verifyWithInlineConfigParser(
72 getPath("InputParameterAssignmentReceiver.java"), expected);
73 }
74
75 @Test
76 public void testEnhancedSwitch() throws Exception {
77 final String[] expected = {
78 "14:28: " + getCheckMessage(MSG_KEY, "a"),
79 "21:16: " + getCheckMessage(MSG_KEY, "result"),
80 };
81 verifyWithInlineConfigParser(
82 getNonCompilablePath("InputParameterAssignmentWithEnhancedSwitch.java"),
83 expected);
84 }
85
86 @Test
87 public void testTokensNotNull() {
88 final ParameterAssignmentCheck check = new ParameterAssignmentCheck();
89 assertWithMessage("Acceptable tokens should not be null")
90 .that(check.getAcceptableTokens())
91 .isNotNull();
92 assertWithMessage("Default tokens should not be null")
93 .that(check.getDefaultTokens())
94 .isNotNull();
95 assertWithMessage("Required tokens should not be null")
96 .that(check.getRequiredTokens())
97 .isNotNull();
98 }
99
100
101
102
103
104
105
106
107 @Test
108 @SuppressWarnings("unchecked")
109 public void testClearState() throws Exception {
110 final ParameterAssignmentCheck check = new ParameterAssignmentCheck();
111 final Optional<DetailAST> methodDef = TestUtil.findTokenInAstByPredicate(
112 JavaParser.parseFile(new File(getPath("InputParameterAssignmentReceiver.java")),
113 JavaParser.Options.WITHOUT_COMMENTS),
114 ast -> ast.getType() == TokenTypes.METHOD_DEF);
115
116 assertWithMessage("Ast should contain METHOD_DEF")
117 .that(methodDef.isPresent())
118 .isTrue();
119 assertWithMessage("State is not cleared on beginTree")
120 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.orElseThrow(),
121 "parameterNamesStack",
122 parameterNamesStack -> ((Collection<Set<String>>) parameterNamesStack).isEmpty()))
123 .isTrue();
124 }
125
126 }