Skip to content

Commit

Permalink
Issue checkstyle#13345: Enable examples tests for SimplifyBooleanExpr…
Browse files Browse the repository at this point in the history
…essionCheck
  • Loading branch information
AmitKumarDeoghoria authored and romani committed Nov 6, 2024
1 parent c572129 commit 6bf58b7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 58 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 SimplifyBooleanExpressionCheckExamplesTest extends AbstractExamplesModuleTestSupport {
@Override
protected String getPackageLocation() {
Expand All @@ -34,9 +32,14 @@ protected String getPackageLocation() {
@Test
public void testExample1() throws Exception {
final String[] expected = {

"18:9: " + getCheckMessage(SimplifyBooleanExpressionCheck.MSG_KEY),
"19:11: " + getCheckMessage(SimplifyBooleanExpressionCheck.MSG_KEY),
"21:11: " + getCheckMessage(SimplifyBooleanExpressionCheck.MSG_KEY),
"22:13: " + getCheckMessage(SimplifyBooleanExpressionCheck.MSG_KEY),
"24:12: " + getCheckMessage(SimplifyBooleanExpressionCheck.MSG_KEY),
"27:23: " + getCheckMessage(SimplifyBooleanExpressionCheck.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,31 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="SimplifyBooleanExpression"/>
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.coding.simplifybooleanexpression;

// xdoc section -- start
class Example1 {
void InvalidExample() {
boolean a=true;
boolean b=true;
Object c=null;
Object d=null;
Object e=null;
if (!false) {}; // violation, can be simplified to true
if (a == true) {}; // violation, can be simplified to a
if (a == b) {};
if (a == false) {}; // violation, can be simplified to !a
if (!(a != true)) {}; // violation, can be simplified to a
e = (a || b) ? c : d;
e = (a || false) ? c : d; // violation, can be simplified to a
e = (a && b) ? c : d;
int s = 12;
boolean m = s > 1 ? true : false; // violation, can be simplified to s > 1
boolean f = c == null ? false : c.equals(d);
}
}
// xdoc section -- end

This file was deleted.

32 changes: 14 additions & 18 deletions src/xdocs/checks/coding/simplifybooleanexpression.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,25 @@
</source>
<p id="Example1-code">Example:</p>
<source>
public class Test {

public void bar() {

boolean a, b;
Foo c, d, e;

if (!false) {}; // violation, can be simplified to true

if (a == true) {}; // violation, can be simplified to a
if (a == b) {}; // OK
if (a == false) {}; // violation, can be simplified to !a
class Example1 {
void InvalidExample() {
boolean a=true;
boolean b=true;
Object c=null;
Object d=null;
Object e=null;
if (!false) {}; // violation, can be simplified to true
if (a == true) {}; // violation, can be simplified to a
if (a == b) {};
if (a == false) {}; // violation, can be simplified to !a
if (!(a != true)) {}; // violation, can be simplified to a

e = (a || b) ? c : d; // OK
e = (a || b) ? c : d;
e = (a || false) ? c : d; // violation, can be simplified to a
e = (a &amp;&amp; b) ? c : d; // OK

e = (a &amp;&amp; b) ? c : d;
int s = 12;
boolean m = s &gt; 1 ? true : false; // violation, can be simplified to s &gt; 1
boolean f = c == null ? false : c.someMethod(); // OK
boolean f = c == null ? false : c.equals(d);
}

}
</source>
</subsection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/simplifybooleanexpression/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/simplifybooleanexpression/Example1.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example1-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/simplifybooleanexpression/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/simplifybooleanexpression/Example1.java"/>
<param name="type" value="code"/>
</macro>
</subsection>
Expand Down

0 comments on commit 6bf58b7

Please sign in to comment.