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

This commit is contained in:
Ruslan Diachenko 2015-07-15 21:22:02 +01:00
parent f7309c4404
commit 28e8f33dfd
5 changed files with 45 additions and 12 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.EmptyLineSeparatorCheck</pattern><branchRate>95</branchRate><lineRate>98</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>

View File

@ -265,9 +265,7 @@ public class EmptyLineSeparatorCheck extends Check {
* @param astType token Type
*/
private void processImport(DetailAST ast, DetailAST nextToken, int astType) {
if (astType != nextToken.getType() && !hasEmptyLineAfter(ast)
|| ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)
&& ast.getPreviousSibling() == null) {
if (astType != nextToken.getType() && !hasEmptyLineAfter(ast)) {
log(nextToken.getLineNo(), MSG_SHOULD_BE_SEPARATED, nextToken.getText());
}
if (hasNotAllowedTwoEmptyLinesBefore(ast)) {

View File

@ -19,14 +19,15 @@
package com.puppycrawl.tools.checkstyle.checks.whitespace;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED;
import org.junit.Assert;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck
.MSG_MULTIPLE_LINES;
import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyLineSeparatorCheck
.MSG_SHOULD_BE_SEPARATED;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public class EmptyLineSeparatorCheckTest
extends BaseCheckTestSupport {
@ -109,9 +110,38 @@ public class EmptyLineSeparatorCheckTest
DefaultConfiguration checkConfig = createCheckConfig(EmptyLineSeparatorCheck.class);
checkConfig.addAttribute("allowMultipleEmptyLines", "false");
final String[] expected = {
"1: " + getCheckMessage(MSG_SHOULD_BE_SEPARATED, "import"),
};
verify(checkConfig, getPath("whitespace/InputEmptyLineSeparatorMultipleImportEmptyClass.java"), expected);
}
@Test
public void testGetAcceptableTokens() {
EmptyLineSeparatorCheck emptyLineSeparatorCheckObj = new EmptyLineSeparatorCheck();
int[] actual = emptyLineSeparatorCheckObj.getAcceptableTokens();
int[] expected = new int[] {
TokenTypes.PACKAGE_DEF,
TokenTypes.IMPORT,
TokenTypes.CLASS_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.STATIC_INIT,
TokenTypes.INSTANCE_INIT,
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.VARIABLE_DEF,
};
Assert.assertNotNull(actual);
Assert.assertArrayEquals(expected, actual);
}
@Test
public void testPrePreviousLineEmpiness() throws Exception {
DefaultConfiguration checkConfig = createCheckConfig(EmptyLineSeparatorCheck.class);
checkConfig.addAttribute("allowMultipleEmptyLines", "false");
final String[] expected = {
};
verify(checkConfig, getPath("whitespace/InputPrePreviousLineEmptiness.java"), expected);
}
}

View File

@ -1,4 +1,4 @@
package com.puppycrawl.tools.checkstyle.whitespace;
package com.puppycrawl.tools.checkstyle.whitespace;import java.util.List;
import java.util.Calendar;
import java.util.Date;

View File

@ -0,0 +1,6 @@
package com.puppycrawl.tools.checkstyle.whitespace;
public class InputPrePreviousLineEmptiness {
}