Skip to content

Commit

Permalink
Issue checkstyle#13345: Enable Local Final Variable Name Check Exampl…
Browse files Browse the repository at this point in the history
…s Test
  • Loading branch information
piyush-1234 authored and romani committed Nov 7, 2023
1 parent ab7ac57 commit 8780d60
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

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

import org.junit.jupiter.api.Disabled;
import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;

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

"17:17: " + getCheckMessage(MSG_INVALID_PATTERN, "VAR1", "^[a-z][a-zA-Z0-9]*$"),
"20:17: " + getCheckMessage(MSG_INVALID_PATTERN, "VAR2", "^[a-z][a-zA-Z0-9]*$"),
};

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

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

"18:17: " + getCheckMessage(MSG_INVALID_PATTERN, "var1", "^[A-Z][A-Z0-9]*$"),
"21:17: " + getCheckMessage(MSG_INVALID_PATTERN, "var2", "^[A-Z][A-Z0-9]*$"),
};

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

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

"19:17: " + getCheckMessage(MSG_INVALID_PATTERN, "scanner", "^[A-Z][A-Z0-9]*$"),
"23:30: " + getCheckMessage(MSG_INVALID_PATTERN, "ex", "^[A-Z][A-Z0-9]*$"),
};

verifyWithInlineConfigParser(getPath("Example3.txt"), expected);
verifyWithInlineConfigParser(getPath("Example3.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="LocalFinalVariableName">
<property name="format" value="^[a-z][a-zA-Z0-9]*$"/>
</module>
</module>
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.naming.localfinalvariablename;

// xdoc section -- start
class Example1{
void MyMethod() {
try {
final int VAR1 = 5; // violation
final int var1 = 10;
} catch (Exception ex) {
final int VAR2 = 15; // violation
final int var2 = 20;
}
}
}
// xdoc section -- end

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.naming.localfinalvariablename;

// xdoc section -- start
class MyClass {
class Example2 {
void MyMethod() {
try {
final int VAR1 = 5; // OK
final int var1 = 10; // violation, name 'var1' must match pattern "^[A-Z][A-Z0-9]*$"
final int VAR1 = 5;
final int var1 = 10; // violation
} catch (Exception ex) {
final int VAR2 = 15; // OK
final int var2 = 20; // violation, name 'var2' must match pattern "^[A-Z][A-Z0-9]*$"
final int VAR2 = 15;
final int var2 = 20; // violation
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="LocalFinalVariableName">
<property name="format" value="^[A-Z][A-Z0-9]*$"/>
<property name="tokens" value="PARAMETER_DEF,RESOURCE"/>
</module>
</module>
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.naming.localfinalvariablename;

import java.util.Scanner;

// xdoc section -- start
class Example3 {
void MyMethod() {
try(Scanner scanner = new Scanner(System.in)) { // violation

final int VAR1 = 5;
final int var1 = 10;
} catch (final Exception ex) { // violation

final int VAR2 = 15;
final int var2 = 20;
}
}
}
// xdoc section -- end

This file was deleted.

46 changes: 31 additions & 15 deletions src/xdocs/checks/naming/localfinalvariablename.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,25 @@
<source>
&lt;module name=&quot;Checker&quot;&gt;
&lt;module name=&quot;TreeWalker&quot;&gt;
&lt;module name=&quot;LocalFinalVariableName&quot;/&gt;
&lt;module name=&quot;LocalFinalVariableName&quot;&gt;
&lt;property name=&quot;format&quot; value=&quot;^[a-z][a-zA-Z0-9]*$&quot;/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</source>
<p id="Example1-code">Code Example:</p>
<source>
class Example1{
void MyMethod() {
try {
final int VAR1 = 5; // violation
final int var1 = 10;
} catch (Exception ex) {
final int VAR2 = 15; // violation
final int var2 = 20;
}
}
}
</source>
<p id="Example2-config">
An example of how to configure the check for names that are only upper case letters and
Expand All @@ -87,14 +103,14 @@
</source>
<p id="Example2-code">Code Example:</p>
<source>
class MyClass {
class Example2 {
void MyMethod() {
try {
final int VAR1 = 5; // OK
final int var1 = 10; // violation, name 'var1' must match pattern &quot;^[A-Z][A-Z0-9]*$&quot;
final int VAR1 = 5;
final int var1 = 10; // violation
} catch (Exception ex) {
final int VAR2 = 15; // OK
final int var2 = 20; // violation, name 'var2' must match pattern &quot;^[A-Z][A-Z0-9]*$&quot;
final int VAR2 = 15;
final int var2 = 20; // violation
}
}
}
Expand All @@ -115,16 +131,16 @@ class MyClass {
</source>
<p id="Example3-code">Code Example:</p>
<source>
class MyClass {
class Example3 {
void MyMethod() {
try(Scanner scanner = new Scanner()) { // violation, name 'scanner' must
// match pattern '^[A-Z][A-Z0-9]*$'
final int VAR1 = 5; // OK
final int var1 = 10; // OK
} catch (final Exception ex) { // violation, name 'ex'
// must match pattern '^[A-Z][A-Z0-9]*$'
final int VAR2 = 15; // OK
final int var2 = 20; // OK
try(Scanner scanner = new Scanner(System.in)) { // violation

final int VAR1 = 5;
final int var1 = 10;
} catch (final Exception ex) { // violation

final int VAR2 = 15;
final int var2 = 20;
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/xdocs/checks/naming/localfinalvariablename.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,28 @@
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example1.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example1.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example1-code">Code Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example1.java"/>
<param name="type" value="code"/>
</macro>
<p id="Example2-config">
An example of how to configure the check for names that are only upper case letters and
digits is:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example2.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example2.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example2-code">Code Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example2.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example2.java"/>
<param name="type" value="code"/>
</macro>
<p id="Example3-config">
Expand All @@ -91,13 +97,13 @@
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example3.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example3.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example3-code">Code Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example3.txt"/>
value="resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/Example3.java"/>
<param name="type" value="code"/>
</macro>
</subsection>
Expand Down

0 comments on commit 8780d60

Please sign in to comment.