Skip to content

Commit

Permalink
Issue checkstyle#13345: Enable examples tests for ModifiedControlVari…
Browse files Browse the repository at this point in the history
…ableCheck
  • Loading branch information
AmitKumarDeoghoria authored and romani committed Nov 6, 2024
1 parent 88b5440 commit f58f26f
Show file tree
Hide file tree
Showing 7 changed files with 75 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 ModifiedControlVariableCheckExamplesTest extends AbstractExamplesModuleTestSupport {
@Override
protected String getPackageLocation() {
Expand All @@ -34,18 +32,19 @@ protected String getPackageLocation() {
@Test
public void testExample1() throws Exception {
final String[] expected = {

"14:8: " + getCheckMessage(ModifiedControlVariableCheck.MSG_KEY, "i"),
"18:11: " + getCheckMessage(ModifiedControlVariableCheck.MSG_KEY, "arg"),
};

verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
}

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

"16:8: " + getCheckMessage(ModifiedControlVariableCheck.MSG_KEY, "i"),
};

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

// xdoc section -- start
class Example1 {
void InvalidExample() {
for(int i=0;i < 8;i++) {
i++; // violation, control variable modified
}
String args1[]={"Coding", "block"};
for (String arg: args1) {
arg = arg.trim(); // violation, control variable modified
}
}
}
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="ModifiedControlVariable">
<property name="skipEnhancedForLoopVariable" value="true"/>
</module>
</module>
</module>
*/
package com.puppycrawl.tools.checkstyle.checks.coding.modifiedcontrolvariable;

// xdoc section -- start
class Example2 {
void InvalidExample() {
for(int i=0;i < 8;i++) {
i++; // violation, control variable modified
}
String args1[]={"Coding", "block"};
for (String arg: args1) {
arg = arg.trim();
}
}
}
// xdoc section -- end

This file was deleted.

32 changes: 20 additions & 12 deletions src/xdocs/checks/coding/modifiedcontrolvariable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,16 @@ for (int a[]={0};a[0] &lt; 10;a[0]++) {
Example:
</p>
<source>
for(int i=0;i &lt; 8;i++) {
i++; // violation, control variable modified
}
String args1[]={&quot;Coding&quot;, &quot;block&quot;};
for (String arg: args1) {
arg = arg.trim(); // violation, control variable modified
class Example1 {
void InvalidExample() {
for(int i=0;i &lt; 8;i++) {
i++; // violation, control variable modified
}
String args1[]={&quot;Coding&quot;, &quot;block&quot;};
for (String arg: args1) {
arg = arg.trim(); // violation, control variable modified
}
}
}
</source>
<p>
Expand All @@ -111,12 +115,16 @@ for (String arg: args1) {
</source>
<p id="Example2-code">Example:</p>
<source>
for(int i=0;i &lt; 8;i++) {
i++; // violation, control variable modified
}
String args1[]={&quot;Coding&quot;, &quot;block&quot;};
for (String arg: args1) {
arg = arg.trim(); // ok
class Example2 {
void InvalidExample() {
for(int i=0;i &lt; 8;i++) {
i++; // violation, control variable modified
}
String args1[]={&quot;Coding&quot;, &quot;block&quot;};
for (String arg: args1) {
arg = arg.trim();
}
}
}
</source>
</subsection>
Expand Down
8 changes: 4 additions & 4 deletions src/xdocs/checks/coding/modifiedcontrolvariable.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ for (int a[]={0};a[0] &lt; 10;a[0]++) {
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/modifiedcontrolvariable/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/modifiedcontrolvariable/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/modifiedcontrolvariable/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/modifiedcontrolvariable/Example1.java"/>
<param name="type" value="code"/>
</macro>
<p>
Expand All @@ -83,13 +83,13 @@ for (int a[]={0};a[0] &lt; 10;a[0]++) {
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/modifiedcontrolvariable/Example2.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/modifiedcontrolvariable/Example2.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example2-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/modifiedcontrolvariable/Example2.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/modifiedcontrolvariable/Example2.java"/>
<param name="type" value="code"/>
</macro>
</subsection>
Expand Down

0 comments on commit f58f26f

Please sign in to comment.