Issue #1289: 'GenericWhitespaceCheck' refactored, UT coverage improved

This commit is contained in:
Ruslan Diachenko 2015-07-16 00:42:12 +01:00 committed by Roman Ivanov
parent 28e8f33dfd
commit 6d3dbe2fd3
4 changed files with 64 additions and 24 deletions

View File

@ -1202,7 +1202,6 @@
<regex><pattern>.*.checks.whitespace.AbstractParenPadCheck</pattern><branchRate>88</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.whitespace.EmptyForInitializerPadCheck</pattern><branchRate>91</branchRate><lineRate>93</lineRate></regex>
<regex><pattern>.*.checks.whitespace.GenericWhitespaceCheck</pattern><branchRate>86</branchRate><lineRate>96</lineRate></regex>
<regex><pattern>.*.checks.whitespace.NoWhitespaceAfterCheck</pattern><branchRate>94</branchRate><lineRate>98</lineRate></regex>
<regex><pattern>.*.checks.whitespace.NoWhitespaceBeforeCheck</pattern><branchRate>90</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.whitespace.OperatorWrapCheck</pattern><branchRate>68</branchRate><lineRate>81</lineRate></regex>

View File

@ -115,13 +115,17 @@ public class GenericWhitespaceCheck extends Check {
@Override
public void visitToken(DetailAST ast) {
if (ast.getType() == TokenTypes.GENERIC_START) {
processStart(ast);
depth++;
}
else if (ast.getType() == TokenTypes.GENERIC_END) {
processEnd(ast);
depth--;
switch (ast.getType()) {
case TokenTypes.GENERIC_START:
processStart(ast);
depth++;
break;
case TokenTypes.GENERIC_END:
processEnd(ast);
depth--;
break;
default:
throw new IllegalArgumentException("Unknown type " + ast);
}
}
@ -134,7 +138,7 @@ public class GenericWhitespaceCheck extends Check {
final int before = ast.getColumnNo() - 1;
final int after = ast.getColumnNo() + 1;
if (0 <= before && Character.isWhitespace(line.charAt(before))
if (before >= 0 && Character.isWhitespace(line.charAt(before))
&& !Utils.whitespaceBefore(before, line)) {
log(ast.getLineNo(), before, WS_PRECEDED, ">");
}
@ -168,7 +172,7 @@ public class GenericWhitespaceCheck extends Check {
// should be whitespace if followed by & -+
//
final int indexOfAmp = line.indexOf('&', after);
if (indexOfAmp != -1
if (indexOfAmp >= 0
&& whitespaceBetween(after, indexOfAmp, line)) {
if (indexOfAmp - after == 0) {
log(ast.getLineNo(), after, WS_NOT_PRECEDED, "&");
@ -201,10 +205,9 @@ public class GenericWhitespaceCheck extends Check {
}
}
else if (!Character.isWhitespace(charAfter)
&& '(' != charAfter && ')' != charAfter
&& ',' != charAfter && '[' != charAfter
&& '.' != charAfter && ':' != charAfter
&& !isAfterMethodReference(ast)) {
&& charAfter != '(' && charAfter != ')'
&& charAfter != ',' && charAfter != '['
&& charAfter != '.' && charAfter != ':') {
log(ast.getLineNo(), after, WS_ILLEGAL_FOLLOW, ">");
}
}
@ -248,7 +251,7 @@ public class GenericWhitespaceCheck extends Check {
// ^ ^
// ws reqd ---+ +--- whitespace NOT required
//
if (0 <= before) {
if (before >= 0) {
// Detect if the first case
final DetailAST parent = ast.getParent();
final DetailAST grandparent = parent.getParent();

View File

@ -19,22 +19,25 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace;
import com.google.common.collect.Maps;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.WS_FOLLOWED;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.WS_ILLEGAL_FOLLOW;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.WS_NOT_PRECEDED;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.WS_PRECEDED;
import java.io.File;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.WS_FOLLOWED;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck
.WS_ILLEGAL_FOLLOW;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck
.WS_NOT_PRECEDED;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.GenericWhitespaceCheck.WS_PRECEDED;
import antlr.CommonHiddenStreamToken;
import com.google.common.collect.Maps;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public class GenericWhitespaceCheckTest
extends BaseCheckTestSupport {
@ -117,4 +120,24 @@ public class GenericWhitespaceCheckTest
+ "checkstyle/whitespace/"
+ "InputGenericWhitespaceMethodRef.java").getCanonicalPath(), expected);
}
@Test
public void testGetAcceptableTokens() {
GenericWhitespaceCheck genericWhitespaceCheckObj = new GenericWhitespaceCheck();
int[] actual = genericWhitespaceCheckObj.getAcceptableTokens();
int[] expected = new int[] {
TokenTypes.GENERIC_START,
TokenTypes.GENERIC_END,
};
Assert.assertNotNull(actual);
Assert.assertArrayEquals(expected, actual);
}
@Test(expected = IllegalArgumentException.class)
public void testWrongTokenType() {
GenericWhitespaceCheck genericWhitespaceCheckObj = new GenericWhitespaceCheck();
DetailAST ast = new DetailAST();
ast.initialize(new CommonHiddenStreamToken(TokenTypes.INTERFACE_DEF, "interface"));
genericWhitespaceCheckObj.visitToken(ast);
}
}

View File

@ -62,4 +62,19 @@ class InputGenericWhitespaceCheck implements Comparable<InputGenericWhitespaceCh
public static class IntEnumValueType3<E extends Enum<E> & IntEnum> {
}
public static class IntEnumValueType4<T extends Comparable<List<T>> & IntEnum> {
}
public void beforeAndAfter() {
List
<
Integer> x = new ArrayList<Integer
>();
List
<Integer> y = new ArrayList<Integer
>();
Map<Class<?>, Integer> a = new HashMap<Class<?>, Integer>();
Map<Class<?>, Integer> b = (Map<Class<?>, Integer>) a;
}
}