Issue #3702: Allow single character names in local variables, method and catch-blocks parameters names in accordance with Google Style Guide

This commit is contained in:
Andrei Selkin 2017-02-09 23:21:50 +03:00 committed by rnveach
parent 614b7647d8
commit 3e541237a1
9 changed files with 71 additions and 97 deletions

View File

@ -22,29 +22,40 @@ package com.google.checkstyle.test.chapter5naming.rule51identifiernames;
import java.io.File;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.Test;
import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
public class CatchParameterNameTest extends BaseCheckTestSupport {
private static Configuration checkConfig;
private static String format;
@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter5naming" + File.separator + "rule51identifiernames"
+ File.separator + fileName);
}
@BeforeClass
public static void setConfigurationBuilder() throws CheckstyleException {
checkConfig = getCheckConfig("CatchParameterName");
format = checkConfig.getAttribute("format");
}
@Test
public void catchParameterNameTest() throws Exception {
final Configuration checkConfig = getCheckConfig("CatchParameterName");
final String msgKey = "name.invalidPattern";
final String format = "^[a-z][a-z0-9][a-zA-Z0-9]*$";
final String[] expected = {
"6:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "e", format),
"24:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "t", format),
"47:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "iException", format),
"50:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "x", format),
"50:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "ex_1", format),
"53:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "eX", format),
"56:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "eXX", format),
"59:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "x_y_z", format),
"62:28: " + getCheckMessage(checkConfig.getMessages(), msgKey, "Ex", format),
};
final String filePath = getPath("InputCatchParameterName.java");

View File

@ -34,10 +34,8 @@ import com.puppycrawl.tools.checkstyle.api.Configuration;
public class ParameterNameTest extends BaseCheckTestSupport {
private static final String MSG_KEY = "name.invalidPattern";
private static String genealFormat;
private static String pubFormat;
private static Configuration generalConfig;
private static Configuration pubConfig;
private static String format;
private static Configuration config;
@Override
protected String getPath(String fileName) throws IOException {
@ -49,63 +47,34 @@ public class ParameterNameTest extends BaseCheckTestSupport {
public static void setConfigurationBuilder() throws CheckstyleException {
final List<Configuration> configs = getCheckConfigs("ParameterName");
Assert.assertEquals(configs.size(), 2);
Assert.assertEquals(configs.size(), 1);
generalConfig = configs.get(0);
Assert.assertEquals(generalConfig.getAttribute("accessModifiers"),
"protected, package, private");
genealFormat = generalConfig.getAttribute("format");
pubConfig = configs.get(1);
Assert.assertEquals(pubConfig.getAttribute("accessModifiers"), "public");
pubFormat = pubConfig.getAttribute("format");
config = configs.get(0);
format = config.getAttribute("format");
}
@Test
public void generalParameterNameTest() throws Exception {
final String[] expected = {
"8:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "$arg1", genealFormat),
"9:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "ar$g2", genealFormat),
"10:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "arg3$", genealFormat),
"11:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "a_rg4", genealFormat),
"12:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "_arg5", genealFormat),
"13:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "arg6_", genealFormat),
"14:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "aArg7", genealFormat),
"15:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "aArg8", genealFormat),
"16:21: "
+ getCheckMessage(generalConfig.getMessages(), MSG_KEY, "aar_g", genealFormat),
"10:21: " + getCheckMessage(config.getMessages(), MSG_KEY, "bB", format),
"33:22: " + getCheckMessage(config.getMessages(), MSG_KEY, "llll_llll", format),
"34:21: " + getCheckMessage(config.getMessages(), MSG_KEY, "bB", format),
"64:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "$arg1", format),
"65:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "ar$g2", format),
"66:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "arg3$", format),
"67:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "a_rg4", format),
"68:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "_arg5", format),
"69:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "arg6_", format),
"70:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "aArg7", format),
"71:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "aArg8", format),
"72:13: " + getCheckMessage(config.getMessages(), MSG_KEY, "aar_g", format),
};
final String filePath = getPath("InputParameterNameSimpleGeneral.java");
final String filePath = getPath("InputParameterName.java");
final Integer[] warnList = getLinesWithWarn(filePath);
verify(generalConfig, filePath, expected, warnList);
verify(config, filePath, expected, warnList);
}
@Test
public void pubParameterNameTest() throws Exception {
final String[] expected = {
"10:21: " + getCheckMessage(pubConfig.getMessages(), MSG_KEY, "bB", pubFormat),
"33:22: " + getCheckMessage(pubConfig.getMessages(), MSG_KEY, "llll_llll", pubFormat),
"34:21: " + getCheckMessage(pubConfig.getMessages(), MSG_KEY, "bB", pubFormat),
"44:23: " + getCheckMessage(pubConfig.getMessages(), MSG_KEY, "p", pubFormat),
"53:31: " + getCheckMessage(pubConfig.getMessages(), MSG_KEY, "p", pubFormat),
"58:44: " + getCheckMessage(pubConfig.getMessages(), MSG_KEY, "p", pubFormat),
};
final String filePath = getPath("InputParameterNameSimplePub.java");
final Integer[] warnList = getLinesWithWarn(filePath);
verify(pubConfig, filePath, expected, warnList);
}
}

View File

@ -51,7 +51,6 @@ public class LocalVariableNameTest extends BaseCheckTestSupport {
public void localVariableNameTest() throws Exception {
final String[] expected = {
"26:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "a", format),
"27:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aA", format),
"28:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "a1_a", format),
"29:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "A_A", format),
@ -74,7 +73,6 @@ public class LocalVariableNameTest extends BaseCheckTestSupport {
public void oneCharTest() throws Exception {
final String[] expected = {
"15:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "i", format),
"21:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "I_ndex", format),
"45:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "i_ndex", format),
"49:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "ii_i1", format),

View File

@ -3,7 +3,7 @@ package com.google.checkstyle.test.chapter5naming.rule51identifiernames;
public class InputCatchParameterName {
{
try {
} catch (Exception e) { // warn
} catch (Exception e) { // ok
}
try {
} catch (Exception ex) { // ok
@ -21,7 +21,7 @@ public class InputCatchParameterName {
} catch (Exception noWorries) { // ok
}
try {
} catch (Throwable t) { // warn
} catch (Throwable t) { // ok
}
try {
throw new InterruptedException("interruptedException");
@ -47,7 +47,19 @@ public class InputCatchParameterName {
} catch (Exception iException) { // warn
}
try {
} catch (Exception x) { // warn
} catch (Exception ex_1) { // warn
}
try {
} catch (Exception eX) { // warn
}
try {
} catch (Exception eXX) { // warn
}
try {
} catch (Exception x_y_z) { // warn
}
try {
} catch (Exception Ex) { // warn
}
}
}

View File

@ -2,7 +2,7 @@ package com.google.checkstyle.test.chapter5naming.rule526parameternames;
import java.io.*;
class InputSimple2
class InputParameterName
{
/** Some more Javadoc. */
@ -41,7 +41,7 @@ class InputParameterNameSimplePub
public void a(int par, int parA) {}
/** Invalid: public and one char long */
public void b(int p) {} //warn
public void b(int p) {}
/** Valid: private and one char long. */
private void c(int p) {}
@ -50,13 +50,26 @@ class InputParameterNameSimplePub
private void d(int param) {
new Object() {
/** Invalid: public and one char long. */
public void e(int p) { } //warn
public void e(int p) { }
};
}
/** Invalid: public constructor and one char long */
public InputParameterNameSimplePub(int p) { } // warn
public InputParameterNameSimplePub(int p) { }
/** Valid: private constructor and one char long */
private InputParameterNameSimplePub(float p) { }
void toManyArgs(
int $arg1, //warn
int ar$g2, //warn
int arg3$, //warn
int a_rg4, //warn
int _arg5, //warn
int arg6_, //warn
int aArg7, //warn
int aArg8, //warn
int aar_g) //warn
{}
}

View File

@ -1,19 +0,0 @@
package com.google.checkstyle.test.chapter5naming.rule526parameternames;
import java.io.*;
final class InputSimple
{
void toManyArgs(
int $arg1, //warn
int ar$g2, //warn
int arg3$, //warn
int a_rg4, //warn
int _arg5, //warn
int arg6_, //warn
int aArg7, //warn
int aArg8, //warn
int aar_g) //warn
{}
}

View File

@ -12,7 +12,7 @@ class InputOneCharInitVarName
//some code
}
int i = 0; //warn
int i = 0; // ok
for(int index = 1; index < 10; index++) { //ok
//some code

View File

@ -23,7 +23,7 @@ final class InputSimple
private void localVariables()
{
//bad examples
int a; //warn
int a;
int aA; //warn
int a1_a; //warn
int A_A; //warn

View File

@ -108,28 +108,18 @@
value="Member name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="ParameterName">
<property name="id" value="ParameterNameNonPublic"/>
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<property name="accessModifiers" value="protected, package, private"/>
<message key="name.invalidPattern"
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="ParameterName">
<property name="id" value="ParameterNamePublic"/>
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="accessModifiers" value="public"/>
<message key="name.invalidPattern"
value="Parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="CatchParameterName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Catch parameter name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="LocalVariableName">
<property name="tokens" value="VARIABLE_DEF"/>
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="allowOneCharVarInForLoop" value="true"/>
<property name="format" value="^[a-z]([a-z0-9][a-zA-Z0-9]*)?$"/>
<message key="name.invalidPattern"
value="Local variable name ''{0}'' must match pattern ''{1}''."/>
</module>