1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter.naming;
21
22 import java.io.File;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck;
32 import com.puppycrawl.tools.checkstyle.checks.naming.LambdaParameterNameCheck;
33
34 public class XpathRegressionLambdaParameterNameTest extends AbstractXpathTestSupport {
35
36 private final String checkName = LambdaParameterNameCheck.class.getSimpleName();
37
38 @Override
39 protected String getCheckName() {
40 return checkName;
41 }
42
43 @Override
44 protected String getPackageLocation() {
45 return "org/checkstyle/suppressionxpathfilter/naming/lambdaparametername";
46 }
47
48 @Test
49 public void testDefault() throws Exception {
50 final File fileToProcess =
51 new File(getPath("InputXpathLambdaParameterNameDefault.java"));
52
53 final DefaultConfiguration moduleConfig =
54 createModuleConfig(LambdaParameterNameCheck.class);
55 final String defaultPattern = "^([a-z][a-zA-Z0-9]*|_)$";
56
57 final String[] expectedViolation = {
58 "7:44: " + getCheckMessage(LambdaParameterNameCheck.class,
59 AbstractNameCheck.MSG_INVALID_PATTERN, "S", defaultPattern),
60 };
61
62 final List<String> expectedXpathQueries = Collections.singletonList(
63 "/COMPILATION_UNIT/CLASS_DEF"
64 + "[./IDENT[@text='InputXpathLambdaParameterNameDefault']]"
65 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
66 + "./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/IDENT[@text='S']"
67 );
68
69 runVerifications(moduleConfig, fileToProcess, expectedViolation,
70 expectedXpathQueries);
71 }
72
73 @Test
74 public void testNonDefaultPattern() throws Exception {
75 final File fileToProcess =
76 new File(getPath("InputXpathLambdaParameterNameNonDefaultPattern.java"));
77
78 final String nonDefaultPattern = "^_[a-zA-Z0-9]*$";
79
80 final DefaultConfiguration moduleConfig =
81 createModuleConfig(LambdaParameterNameCheck.class);
82 moduleConfig.addProperty("format", nonDefaultPattern);
83
84 final String[] expectedViolation = {
85 "7:45: " + getCheckMessage(LambdaParameterNameCheck.class,
86 AbstractNameCheck.MSG_INVALID_PATTERN, "s", nonDefaultPattern),
87 };
88
89 final List<String> expectedXpathQueries = Arrays.asList(
90 "/COMPILATION_UNIT/CLASS_DEF"
91 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
92 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
93 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS",
94
95 "/COMPILATION_UNIT/CLASS_DEF"
96 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
97 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
98 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
99 + "/PARAMETER_DEF[./IDENT[@text='s']]",
100
101 "/COMPILATION_UNIT/CLASS_DEF"
102 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
103 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST"
104 + "/VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
105 + "/PARAMETER_DEF[./IDENT[@text='s']]/MODIFIERS",
106
107 "/COMPILATION_UNIT/CLASS_DEF"
108 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
109 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
110 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
111 + "/PARAMETER_DEF[./IDENT[@text='s']]/TYPE",
112
113 "/COMPILATION_UNIT/CLASS_DEF"
114 + "[./IDENT[@text='InputXpathLambdaParameterNameNonDefaultPattern']]"
115 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
116 + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS"
117 + "/PARAMETER_DEF/IDENT[@text='s']"
118 );
119
120 runVerifications(moduleConfig, fileToProcess, expectedViolation,
121 expectedXpathQueries);
122 }
123 }