Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Issue checkstyle#5003: increased coverage of pitest-checks-whitespace…
Browse files Browse the repository at this point in the history
… to 98%
  • Loading branch information
Nimfadora committed Aug 25, 2017
1 parent e51c3b6 commit 48c4f05
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -2044,7 +2044,7 @@
<targetTests>
<param>com.puppycrawl.tools.checkstyle.checks.whitespace.*</param>
</targetTests>
<mutationThreshold>95</mutationThreshold>
<mutationThreshold>98</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
<threads>${pitest.plugin.threads}</threads>
Expand Down
Expand Up @@ -328,7 +328,7 @@ else if (!hasEmptyLineAfter(ast)) {
*/
private void processMultipleLinesInside(DetailAST ast) {
final int astType = ast.getType();
if (isClassMemberBlock(astType)) {
if (astType != TokenTypes.CLASS_DEF && isClassMemberBlock(astType)) {
final List<Integer> emptyLines = getEmptyLines(ast);
final List<Integer> emptyLinesToLog = getEmptyLinesToLog(emptyLines);

Expand Down Expand Up @@ -360,12 +360,14 @@ private List<Integer> getEmptyLines(DetailAST ast) {
final DetailAST lastToken = ast.getLastChild().getLastChild();
int lastTokenLineNo = 0;
if (lastToken != null) {
lastTokenLineNo = lastToken.getLineNo();
// -1 as count starts from 0
// -2 as last token line cannot be empty, because it is a RCURLY
lastTokenLineNo = lastToken.getLineNo() - 2;
}
final List<Integer> emptyLines = new ArrayList<>();
final FileContents fileContents = getFileContents();

for (int lineNo = ast.getLineNo(); lineNo < lastTokenLineNo; lineNo++) {
for (int lineNo = ast.getLineNo(); lineNo <= lastTokenLineNo; lineNo++) {
if (fileContents.lineIsBlank(lineNo)) {
emptyLines.add(lineNo);
}
Expand All @@ -380,7 +382,7 @@ private List<Integer> getEmptyLines(DetailAST ast) {
*/
private static List<Integer> getEmptyLinesToLog(List<Integer> emptyLines) {
final List<Integer> emptyLinesToLog = new ArrayList<>();
if (emptyLines.size() > 1) {
if (emptyLines.size() >= 2) {
int previousEmptyLineNo = emptyLines.get(0);
for (int emptyLineNo : emptyLines) {
if (previousEmptyLineNo + 1 == emptyLineNo) {
Expand Down
Expand Up @@ -150,7 +150,7 @@ private void processEnd(DetailAST ast) {
final int after = ast.getColumnNo() + 1;

if (before >= 0 && Character.isWhitespace(line.charAt(before))
&& !CommonUtils.hasWhitespaceBefore(before, line)) {
&& !containsWhitespaceBefore(before, line)) {
log(ast.getLineNo(), before, MSG_WS_PRECEDED, CLOSE_ANGLE_BRACKET);
}

Expand Down Expand Up @@ -183,7 +183,7 @@ private void processNestedGenerics(DetailAST ast, String line, int after) {
// should be whitespace if followed by & -+
//
final int indexOfAmp = line.indexOf('&', after);
if (indexOfAmp >= 0
if (indexOfAmp >= 1
&& containsWhitespaceBetween(after, indexOfAmp, line)) {
if (indexOfAmp - after == 0) {
log(ast.getLineNo(), after, MSG_WS_NOT_PRECEDED, "&");
Expand Down Expand Up @@ -271,7 +271,7 @@ private void processStart(DetailAST ast) {
}
// Whitespace not required
else if (Character.isWhitespace(line.charAt(before))
&& !CommonUtils.hasWhitespaceBefore(before, line)) {
&& !containsWhitespaceBefore(before, line)) {
log(ast.getLineNo(), before, MSG_WS_PRECEDED, OPEN_ANGLE_BRACKET);
}
}
Expand Down Expand Up @@ -302,6 +302,18 @@ private static boolean containsWhitespaceBetween(int fromIndex, int toIndex, Str
return result;
}

/**
* Returns whether the specified string contains only whitespace up to specified index.
*
* @param before the index to start the search from. Inclusive
* @param line the index to finish the search. Exclusive
* @return {@code true} if there are only whitespaces,
* false if there is nothing before or some other characters
*/
private static boolean containsWhitespaceBefore(int before, String line) {
return before != 0 && CommonUtils.hasWhitespaceBefore(before, line);
}

/**
* Checks whether given character is valid to be right after generic ends.
* @param charAfter character to check
Expand Down
Expand Up @@ -108,12 +108,12 @@ public void visitToken(DetailAST ast) {
final String line = getLine(ast.getLineNo() - 1);
final int before = ast.getColumnNo() - 1;

if ((before < 0 || Character.isWhitespace(line.charAt(before)))
if ((before == -1 || Character.isWhitespace(line.charAt(before)))
&& !isInEmptyForInitializer(ast)) {

boolean flag = !allowLineBreaks;
// verify all characters before '.' are whitespace
for (int i = 0; !flag && i < before; i++) {
for (int i = 0; !flag && i <= before - 1; i++) {
if (!Character.isWhitespace(line.charAt(i))) {
flag = true;
break;
Expand Down
Expand Up @@ -165,6 +165,31 @@ public void testPrePreviousLineEmptiness() throws Exception {
getPath("InputEmptyLineSeparatorPrePreviousLineEmptiness.java"), expected);
}

@Test
public void testPrePreviousLineIsEmpty() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
checkConfig.addAttribute("allowMultipleEmptyLines", "false");
final String[] expected = {
"3: " + getCheckMessage(MSG_MULTIPLE_LINES, "package"),
};
verify(checkConfig,
getPath("InputEmptyLineSeparatorPrePreviousLineIsEmpty.java"), expected);
}

@Test
public void testPreviousLineEmptiness() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
checkConfig.addAttribute("allowMultipleEmptyLinesInsideClassMembers", "false");
final String[] expected = {
"11: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
"16: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
"22: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
"31: " + getCheckMessage(MSG_MULTIPLE_LINES_INSIDE),
};
verify(checkConfig,
getPath("InputEmptyLineSeparatorPreviousLineEmptiness.java"), expected);
}

@Test
public void testDisAllowMultipleEmptyLinesInsideClassMembers() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(EmptyLineSeparatorCheck.class);
Expand Down
Expand Up @@ -95,6 +95,23 @@ public void testDefault() throws Exception {
verify(checkConfig, getPath("InputGenericWhitespaceDefault.java"), expected);
}

@Test
public void testAtTheStartOfTheLine() throws Exception {
final String[] expected = {
"10:1: " + getCheckMessage(MSG_WS_PRECEDED, ">"),
"12:1: " + getCheckMessage(MSG_WS_PRECEDED, "<"),
};
verify(checkConfig, getPath("InputGenericWhitespaceAtStartOfTheLine.java"), expected);
}

@Test
public void testNestedGeneric() throws Exception {
final String[] expected = {
"11:2: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "&"),
};
verify(checkConfig, getPath("InputGenericWhitespaceNested.java"), expected);
}

@Test
public void testList() throws Exception {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
Expand Down
Expand Up @@ -95,4 +95,23 @@ public void testMethodReference() throws Exception {
};
verify(checkConfig, getPath("InputNoWhitespaceBeforeMethodRef.java"), expected);
}

@Test
public void testDotAtTheStartOfTheLine() throws Exception {
checkConfig.addAttribute("tokens", "DOT");
final String[] expected = {
"2:1: " + getCheckMessage(MSG_KEY, "."),
};
verify(checkConfig, getPath("InputNoWhitespaceBeforeAtStartOfTheLine.java"), expected);
}

@Test
public void testMethodRefAtTheStartOfTheLine() throws Exception {
checkConfig.addAttribute("tokens", "METHOD_REF");
checkConfig.addAttribute("allowLineBreaks", "yes");
final String[] expected = {
"14:2: " + getCheckMessage(MSG_KEY, "::"),
};
verify(checkConfig, getPath("InputNoWhitespaceBeforeAtStartOfTheLine.java"), expected);
}
}
Expand Up @@ -435,4 +435,14 @@ public void testLambdaCheckOnlyWithSpace() throws Exception {
};
verify(checkConfig, getPath("InputParenPadLambdaOnlyWithSpace.java"), expected);
}

@Test
public void testLambdaCheckOnlyWithSpace1() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
checkConfig.addAttribute("option", PadOption.SPACE.toString());
final String[] expected = {
"5:2: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
};
verify(checkConfig, getPath("InputParenPadStartOfTheLine.java"), expected);
}
}
Expand Up @@ -111,6 +111,15 @@ public void testSimpleInput()
verify(checkConfig, getPath("InputWhitespaceAroundSimple.java"), expected);
}

@Test
public void testStartOfTheLine()
throws Exception {
final String[] expected = {
"5:2: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "{"),
};
verify(checkConfig, getPath("InputWhitespaceAroundStartOfTheLine.java"), expected);
}

@Test
public void testBraces()
throws Exception {
Expand Down
@@ -0,0 +1,6 @@


package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator;

public class InputEmptyLineSeparatorPrePreviousLineIsEmpty {
}
@@ -0,0 +1,34 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator;

public class InputEmptyLineSeparatorPreviousLineEmptiness {
private static final int MULTIPLICATOR;

private int base;

static {
MULTIPLICATOR = 5;


}

{


base = 33;
}

public InputEmptyLineSeparatorPreviousLineEmptiness(int base) {


this.base = base;
}

public InputEmptyLineSeparatorPreviousLineEmptiness() {
}

public static int increment(int value) {


return value * MULTIPLICATOR + 1;
}
}
@@ -0,0 +1,15 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace.genericwhitespace;

import java.util.Collections;

class InputGenericWhitespaceAtStartOfTheLine {

public String getConstructor(Class<?>... parameterTypes)
{
Collections.<Object
>emptySet();
Collections.
<Object>emptySet();
return "pitest makes me cry";
}
}
@@ -0,0 +1,12 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace.genericwhitespace;

public class InputGenericWhitespaceNested {
interface IntEnum { /*inner enum*/}

interface NumberEnum<T> { /*inner enum*/}

static class IntEnumValue implements IntEnum, NumberEnum<Integer> {}

static class IntEnumValueType<E extends Enum<E
>& IntEnum & NumberEnum<E>> {}
}
@@ -0,0 +1,16 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace
.nowhitespacebefore;

import java.util.function.Supplier;

public class InputNoWhitespaceBeforeAtStartOfTheLine {
public static class A {
private A() {
}
}

public <V> void methodName(V value) {
Supplier<?> t =
A ::new;
}
}
@@ -0,0 +1,8 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace.parenpad;

public class InputParenPadStartOfTheLine {
public String checkSmth( String
a) {
return a + 1;
}
}
@@ -0,0 +1,8 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace.whitespacearound;

public class InputWhitespaceAroundStartOfTheLine {
public void checkSmth(
){
final int SOMETHING = 1;
}
}

0 comments on commit 48c4f05

Please sign in to comment.