Skip to content

Commit 3015216

Browse files
AmitKumarDeoghoriaromani
authored andcommitted
Issue #13345: Enable examples tests for StringLiteralEqualityCheck
1 parent aadc377 commit 3015216

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/StringLiteralEqualityCheckExamplesTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919

2020
package com.puppycrawl.tools.checkstyle.checks.coding;
2121

22-
import org.junit.jupiter.api.Disabled;
2322
import org.junit.jupiter.api.Test;
2423

2524
import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
2625

27-
@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345")
2826
public class StringLiteralEqualityCheckExamplesTest extends AbstractExamplesModuleTestSupport {
2927
@Override
3028
protected String getPackageLocation() {
@@ -34,9 +32,11 @@ protected String getPackageLocation() {
3432
@Test
3533
public void testExample1() throws Exception {
3634
final String[] expected = {
37-
35+
"18:16: " + getCheckMessage(StringLiteralEqualityCheck.MSG_KEY, "=="),
36+
"20:19: " + getCheckMessage(StringLiteralEqualityCheck.MSG_KEY, "!="),
37+
"22:28: " + getCheckMessage(StringLiteralEqualityCheck.MSG_KEY, "=="),
3838
};
3939

40-
verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
40+
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
4141
}
4242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*xml
2+
<module name="Checker">
3+
<module name="TreeWalker">
4+
<module name="StringLiteralEquality"/>
5+
</module>
6+
</module>
7+
*/
8+
package com.puppycrawl.tools.checkstyle.checks.coding.stringliteralequality;
9+
10+
// xdoc section -- start
11+
class Example1 {
12+
String getName(){
13+
return "Y";
14+
}
15+
void InvalidExample(){
16+
String status = "pending";
17+
// violation below, 'Literal Strings should be compared using equals(), not '==''
18+
if (status == "done") {}
19+
// violation below, 'Literal Strings should be compared using equals(), not '!=''
20+
while (status != "done") {}
21+
// violation below, 'Literal Strings should be compared using equals(), not '==''
22+
boolean flag = (status == "done");
23+
boolean flag1 = (status.equals("done"));
24+
String name = "X";
25+
if (name == getName()) {}
26+
// OK, limitation that check cannot tell runtime type returned from method call
27+
}
28+
}
29+
// xdoc section -- end

src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.txt

-24
This file was deleted.

src/xdocs/checks/coding/stringliteralequality.xml

+18-14
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,24 @@ if (&quot;something&quot;.equals(x))
4848
Examples of violations:
4949
</p>
5050
<source>
51-
String status = &quot;pending&quot;;
52-
53-
if (status == &quot;done&quot;) {} // violation
54-
55-
while (status != &quot;done&quot;) {} // violation
56-
57-
boolean flag = (status == &quot;done&quot;); // violation
58-
59-
boolean flag = (status.equals(&quot;done&quot;)); // OK
60-
61-
String name = &quot;X&quot;;
62-
63-
if (name == getName()) {}
64-
// OK, limitation that check cannot tell runtime type returned from method call
51+
class Example1 {
52+
String getName(){
53+
return &quot;Y&quot;;
54+
}
55+
void InvalidExample(){
56+
String status = &quot;pending&quot;;
57+
// violation below, 'Literal Strings should be compared using equals(), not '==''
58+
if (status == &quot;done&quot;) {}
59+
// violation below, 'Literal Strings should be compared using equals(), not '!=''
60+
while (status != &quot;done&quot;) {}
61+
// violation below, 'Literal Strings should be compared using equals(), not '==''
62+
boolean flag = (status == &quot;done&quot;);
63+
boolean flag1 = (status.equals(&quot;done&quot;));
64+
String name = &quot;X&quot;;
65+
if (name == getName()) {}
66+
// OK, limitation that check cannot tell runtime type returned from method call
67+
}
68+
}
6569
</source>
6670
</subsection>
6771

src/xdocs/checks/coding/stringliteralequality.xml.template

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ if (&quot;something&quot;.equals(x))
3939
</p>
4040
<macro name="example">
4141
<param name="path"
42-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.txt"/>
42+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.java"/>
4343
<param name="type" value="config"/>
4444
</macro>
4545
<p id="Example1-code">
4646
Examples of violations:
4747
</p>
4848
<macro name="example">
4949
<param name="path"
50-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.txt"/>
50+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.java"/>
5151
<param name="type" value="code"/>
5252
</macro>
5353
</subsection>

0 commit comments

Comments
 (0)