Skip to content

Commit

Permalink
Issue checkstyle#14937: Migrated section 4.2.2 Declared When Needed t…
Browse files Browse the repository at this point in the history
…o chapter wise testing
  • Loading branch information
Zopsss authored and romani committed Jun 26, 2024
1 parent f792773 commit 0ba52f3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import org.junit.jupiter.api.Test;

import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck;

public class VariableDeclarationUsageDistanceTest extends AbstractGoogleModuleTestSupport {
public class DeclaredWhenNeededTest extends AbstractGoogleModuleTestSupport {

@Override
protected String getPackageLocation() {
Expand All @@ -34,24 +32,8 @@ protected String getPackageLocation() {

@Test
public void testArrayTypeStyle() throws Exception {
final String msgExt = "variable.declaration.usage.distance.extend";
final Class<VariableDeclarationUsageDistanceCheck> clazz =
VariableDeclarationUsageDistanceCheck.class;

final String[] expected = {
"71:9: " + getCheckMessage(clazz, msgExt, "count", 4, 3),
"219:9: " + getCheckMessage(clazz, msgExt, "t", 5, 3),
"483:9: " + getCheckMessage(clazz, msgExt, "myOption", 7, 3),
"495:9: " + getCheckMessage(clazz, msgExt, "myOption", 6, 3),
"508:9: " + getCheckMessage(clazz, msgExt, "count", 4, 3),
};

final Configuration checkConfig =
getModuleConfig("VariableDeclarationUsageDistance");
final String filePath = getPath("InputVariableDeclarationUsageDistanceCheck.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
final String filePath = getPath("InputDeclaredWhenNeeded.java");
verifyWithConfigParser(new String[] {"VariableDeclarationUsageDistance"}, filePath);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.google.checkstyle.test.chapter4formatting.rule4822variabledistance;
import java.util.*;
public class InputVariableDeclarationUsageDistanceCheck {
public class InputDeclaredWhenNeeded {

private static int test1 = 0;

Expand Down Expand Up @@ -34,15 +34,15 @@ public class InputVariableDeclarationUsageDistanceCheck {
a = b; // DECLARATION OF VARIABLE 'a' SHOULD BE HERE (distance = 2)
}

public InputVariableDeclarationUsageDistanceCheck(int test1) {
public InputDeclaredWhenNeeded(int test1) {
int temp = -1;
this.test1 = test1;
temp = test1; // DECLARATION OF VARIABLE 'temp' SHOULD BE HERE (distance = 2)
}

public boolean testMethod() {
int temp = 7;
new InputVariableDeclarationUsageDistanceCheck(2);
new InputDeclaredWhenNeeded(2);
String.valueOf(temp); // DECLARATION OF VARIABLE 'temp' SHOULD BE HERE (distance = 2)
boolean result = false;
String str = "";
Expand All @@ -68,7 +68,8 @@ public void testMethod2() {
}

public void testMethod3() {
int count; //warn
int count;
// violation above 'Distance between variable 'count' .* first usage is 4, but allowed 3.*'
int a = 3;
int b = 3;
a = a + b;
Expand Down Expand Up @@ -216,7 +217,8 @@ public void testMethod13() {

public void testMethod14() {
Session s = openSession();
Transaction t = s.beginTransaction(); //warn
Transaction t = s.beginTransaction();
// violation above 'Distance between variable 't' .* first usage is 5, but allowed 3.'
A a = new A();
E d1 = new E();
C1 c = new C1();
Expand Down Expand Up @@ -480,7 +482,8 @@ public void testIssue32_8(Writer w1, Writer w2, Writer w3) {

public void testIssue32_9() {
Options options = new Options();
Option myOption = null; //warn
Option myOption = null;
// violation above 'Distance between variable 'myOption' .* usage is 7, but allowed 3.'
options.addBindFile(null);
options.addBindFile(null);
options.addBindFile(null);
Expand All @@ -492,7 +495,8 @@ public void testIssue32_9() {

public void testIssue32_10() {
Options options = new Options();
Option myOption = null; //warn
Option myOption = null;
// violation above 'Distance between variable 'myOption' .* usage is 6, but allowed 3.'
options.addBindFile(null);
options.addBindFile(null);
options.addBindFile(null);
Expand All @@ -505,7 +509,8 @@ public void testIssue32_10() {
public int testIssue32_11(String toDir)
throws Exception
{
int count = 0; // warn
int count = 0;
// violation above 'Distance between variable 'count' .* first usage is 4, but allowed 3.'
String[] files = {};

System.identityHashCode("Data archival started");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public class XdocsPagesTest {
"4.5.2 Indent continuation lines at least +4 spaces",
"4.6.1 Vertical Whitespace",
"4.6.2 Horizontal whitespace",
"4.8.2.2 Declared when needed",
"4.8.3.2 No C-style array declarations",
"4.8.4.1 Indentation",
"4.8.4.3 Presence of the default label",
Expand Down
4 changes: 2 additions & 2 deletions src/xdocs/google_style.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@
</a>
</td>
<td>
<a href="styleguides/google-java-style-20180523/javaguide.html#s4.8.2.2-variables-limited-scope">
<a href="styleguides/google-java-style-20220203/javaguide.html#s4.8.2.2-variables-limited-scope">
4.8.2.2 Declared when needed
</a>
</td>
Expand All @@ -1185,7 +1185,7 @@
<td>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%20path%3A**%2Fgoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+VariableDeclarationUsageDistance">
config</a><br/>
<a href="https://github.com/checkstyle/checkstyle/blob/master/src/it/java/com/google/checkstyle/test/chapter4formatting/rule4822variabledistance/VariableDeclarationUsageDistanceTest.java">
<a href="https://github.com/checkstyle/checkstyle/blob/master/src/it/java/com/google/checkstyle/test/chapter4formatting/rule4822variabledistance/DeclaredWhenNeededTest.java">
test</a>
</td>
</tr>
Expand Down

0 comments on commit 0ba52f3

Please sign in to comment.