Skip to content

Commit

Permalink
Issue #13345: Enable examples tests for StringLiteralEqualityCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
AmitKumarDeoghoria authored and romani committed Nov 6, 2024
1 parent aadc377 commit 3015216
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

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

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;

@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345")
public class StringLiteralEqualityCheckExamplesTest extends AbstractExamplesModuleTestSupport {
@Override
protected String getPackageLocation() {
Expand All @@ -34,9 +32,11 @@ protected String getPackageLocation() {
@Test
public void testExample1() throws Exception {
final String[] expected = {

"18:16: " + getCheckMessage(StringLiteralEqualityCheck.MSG_KEY, "=="),
"20:19: " + getCheckMessage(StringLiteralEqualityCheck.MSG_KEY, "!="),
"22:28: " + getCheckMessage(StringLiteralEqualityCheck.MSG_KEY, "=="),
};

verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="StringLiteralEquality"/>
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.coding.stringliteralequality;

// xdoc section -- start
class Example1 {
String getName(){
return "Y";
}
void InvalidExample(){
String status = "pending";
// violation below, 'Literal Strings should be compared using equals(), not '==''
if (status == "done") {}
// violation below, 'Literal Strings should be compared using equals(), not '!=''
while (status != "done") {}
// violation below, 'Literal Strings should be compared using equals(), not '==''
boolean flag = (status == "done");
boolean flag1 = (status.equals("done"));
String name = "X";
if (name == getName()) {}
// OK, limitation that check cannot tell runtime type returned from method call
}
}
// xdoc section -- end

This file was deleted.

32 changes: 18 additions & 14 deletions src/xdocs/checks/coding/stringliteralequality.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,24 @@ if (&quot;something&quot;.equals(x))
Examples of violations:
</p>
<source>
String status = &quot;pending&quot;;

if (status == &quot;done&quot;) {} // violation

while (status != &quot;done&quot;) {} // violation

boolean flag = (status == &quot;done&quot;); // violation

boolean flag = (status.equals(&quot;done&quot;)); // OK

String name = &quot;X&quot;;

if (name == getName()) {}
// OK, limitation that check cannot tell runtime type returned from method call
class Example1 {
String getName(){
return &quot;Y&quot;;
}
void InvalidExample(){
String status = &quot;pending&quot;;
// violation below, 'Literal Strings should be compared using equals(), not '==''
if (status == &quot;done&quot;) {}
// violation below, 'Literal Strings should be compared using equals(), not '!=''
while (status != &quot;done&quot;) {}
// violation below, 'Literal Strings should be compared using equals(), not '==''
boolean flag = (status == &quot;done&quot;);
boolean flag1 = (status.equals(&quot;done&quot;));
String name = &quot;X&quot;;
if (name == getName()) {}
// OK, limitation that check cannot tell runtime type returned from method call
}
}
</source>
</subsection>

Expand Down
4 changes: 2 additions & 2 deletions src/xdocs/checks/coding/stringliteralequality.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ if (&quot;something&quot;.equals(x))
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example1-code">
Examples of violations:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/stringliteralequality/Example1.java"/>
<param name="type" value="code"/>
</macro>
</subsection>
Expand Down

0 comments on commit 3015216

Please sign in to comment.