Skip to content

Commit

Permalink
Issue checkstyle#13345: Enable examples tests for AnonInnerLengthCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
AmitKumarDeoghoria authored and romani committed Nov 28, 2024
1 parent 7e5c3c6 commit c54f4ca
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

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

import org.junit.jupiter.api.Disabled;
import static com.puppycrawl.tools.checkstyle.checks.sizes.AnonInnerLengthCheck.MSG_KEY;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345")
public class AnonInnerLengthCheckExamplesTest extends AbstractExamplesModuleTestSupport {
@Override
protected String getPackageLocation() {
Expand All @@ -33,19 +34,16 @@ protected String getPackageLocation() {

@Test
public void testExample1() throws Exception {
final String[] expected = {

};

verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
}

@Test
public void testExample2() throws Exception {
final String[] expected = {

"23:30: " + getCheckMessage(MSG_KEY, 9, 7),
};

verifyWithInlineConfigParser(getPath("Example2.txt"), expected);
verifyWithInlineConfigParser(getPath("Example2.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="AnonInnerLength"/>
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.sizes.anoninnerlength;

// xdoc section -- start
class Example1 {
void testMethod() {
Runnable shortAnonClass = new Runnable() {
@Override
public void run() {
System.out.println("Short anonymous class.");
}
};
shortAnonClass.run();

Runnable longAnonClass = new Runnable() {
@Override
public void run() {
System.out.println("This is a lengthy anonymous class.");
System.out.println("It has too many lines of code.");
System.out.println("Exceeding the length limit.");
System.out.println("This would trigger the AnonInnerLength rule.");
}
};
longAnonClass.run();
}
}
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="AnonInnerLength">
<property name="max" value="7"/>
</module>
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.sizes.anoninnerlength;

// xdoc section -- start
class Example2 {
void testMethod() {
Runnable shortAnonClass = new Runnable() {
@Override
public void run() {
System.out.println("Short anonymous class.");
}
};
shortAnonClass.run();
// violation below, 'Anonymous inner class length is 9 lines (max allowed is 7)'
Runnable longAnonClass = new Runnable() {
@Override
public void run() {
System.out.println("This is a lengthy anonymous class.");
System.out.println("It has too many lines of code.");
System.out.println("Exceeding the length limit.");
System.out.println("This would trigger the AnonInnerLength rule.");
}
};
longAnonClass.run();
}
}
// xdoc section -- end

This file was deleted.

56 changes: 53 additions & 3 deletions src/xdocs/checks/sizes/anoninnerlength.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,77 @@

<subsection name="Examples" id="Examples">
<p id="Example1-config">
To configure the check to accept anonymous classes with up to 20 lines:
To configure the check to accept anonymous classes with up to 20 lines by default:
</p>
<source>
&lt;module name=&quot;Checker&quot;&gt;
&lt;module name=&quot;TreeWalker&quot;&gt;
&lt;module name=&quot;AnonInnerLength&quot;/&gt;
&lt;/module&gt;
&lt;/module&gt;
</source>
<p id="Example1-code">Example1:</p>
<source>
class Example1 {
void testMethod() {
Runnable shortAnonClass = new Runnable() {
@Override
public void run() {
System.out.println(&quot;Short anonymous class.&quot;);
}
};
shortAnonClass.run();

Runnable longAnonClass = new Runnable() {
@Override
public void run() {
System.out.println(&quot;This is a lengthy anonymous class.&quot;);
System.out.println(&quot;It has too many lines of code.&quot;);
System.out.println(&quot;Exceeding the length limit.&quot;);
System.out.println(&quot;This would trigger the AnonInnerLength rule.&quot;);
}
};
longAnonClass.run();
}
}
</source>
<p id="Example2-config">
To configure the check to accept anonymous classes with up to 60 lines:
To configure the check to accept anonymous classes with up to 7 lines:
</p>
<source>
&lt;module name=&quot;Checker&quot;&gt;
&lt;module name=&quot;TreeWalker&quot;&gt;
&lt;module name=&quot;AnonInnerLength&quot;&gt;
&lt;property name=&quot;max&quot; value=&quot;60&quot;/&gt;
&lt;property name=&quot;max&quot; value=&quot;7&quot;/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</source>
<p id="Example2-code">Example2:</p>
<source>
class Example2 {
void testMethod() {
Runnable shortAnonClass = new Runnable() {
@Override
public void run() {
System.out.println(&quot;Short anonymous class.&quot;);
}
};
shortAnonClass.run();
// violation below, 'Anonymous inner class length is 9 lines (max allowed is 7)'
Runnable longAnonClass = new Runnable() {
@Override
public void run() {
System.out.println(&quot;This is a lengthy anonymous class.&quot;);
System.out.println(&quot;It has too many lines of code.&quot;);
System.out.println(&quot;Exceeding the length limit.&quot;);
System.out.println(&quot;This would trigger the AnonInnerLength rule.&quot;);
}
};
longAnonClass.run();
}
}
</source>
</subsection>

<subsection name="Example of Usage" id="Example_of_Usage">
Expand Down
20 changes: 16 additions & 4 deletions src/xdocs/checks/sizes/anoninnerlength.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,33 @@

<subsection name="Examples" id="Examples">
<p id="Example1-config">
To configure the check to accept anonymous classes with up to 20 lines:
To configure the check to accept anonymous classes with up to 20 lines by default:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/sizes/anoninnerlength/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/sizes/anoninnerlength/Example1.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example1-code">Example1:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/sizes/anoninnerlength/Example1.java"/>
<param name="type" value="code"/>
</macro>
<p id="Example2-config">
To configure the check to accept anonymous classes with up to 60 lines:
To configure the check to accept anonymous classes with up to 7 lines:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/sizes/anoninnerlength/Example2.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/sizes/anoninnerlength/Example2.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example2-code">Example2:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/sizes/anoninnerlength/Example2.java"/>
<param name="type" value="code"/>
</macro>
</subsection>

<subsection name="Example of Usage" id="Example_of_Usage">
Expand Down

0 comments on commit c54f4ca

Please sign in to comment.