This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#4395: increase coverage of pitest-checkstyle-utils p…
…rofile to 99%
- Loading branch information
Showing
13 changed files
with
475 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/test/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPositionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
// checkstyle: Checks Java source code for adherence to a set of rules. | ||
// Copyright (C) 2001-2017 the original author or authors. | ||
// | ||
// This library is free software; you can redistribute it and/or | ||
// modify it under the terms of the GNU Lesser General Public | ||
// License as published by the Free Software Foundation; either | ||
// version 2.1 of the License, or (at your option) any later version. | ||
// | ||
// This library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this library; if not, write to the Free Software | ||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
package com.puppycrawl.tools.checkstyle.utils; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
import org.junit.Test; | ||
|
||
import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
import com.puppycrawl.tools.checkstyle.internal.TestUtils; | ||
|
||
public class BlockCommentPositionTest { | ||
|
||
@Test | ||
public void testJavaDocsRecognition() throws Exception { | ||
final List<BlockCommentPositionTestMetadata> metadataList = Arrays.asList( | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnClass.java", | ||
BlockCommentPosition::isOnClass, 3), | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnMethod.java", | ||
BlockCommentPosition::isOnMethod, 3), | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnField.java", | ||
BlockCommentPosition::isOnField, 3), | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnum.java", | ||
BlockCommentPosition::isOnEnum, 3), | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnConstructor.java", | ||
BlockCommentPosition::isOnConstructor, 3), | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnInterface.java", | ||
BlockCommentPosition::isOnInterface, 3), | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnAnnotation.java", | ||
BlockCommentPosition::isOnAnnotationDef, 3), | ||
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnumMember.java", | ||
BlockCommentPosition::isOnEnumConstant, 2) | ||
); | ||
|
||
for (BlockCommentPositionTestMetadata metadata : metadataList) { | ||
final DetailAST ast = TestUtils.parseFile( | ||
new File(getPath(metadata.getFileName())) | ||
); | ||
final int matches = getJavadocsCount(ast, metadata.getFileName(), | ||
metadata.getValidation()); | ||
assertEquals(metadata.getMatchesNum(), matches); | ||
} | ||
} | ||
|
||
private static int getJavadocsCount(DetailAST detailAST, String filename, | ||
Function<DetailAST, Boolean> validation) { | ||
int matchFound = 0; | ||
DetailAST node = detailAST; | ||
while (node != null) { | ||
if (node.getType() == TokenTypes.BLOCK_COMMENT_BEGIN | ||
&& JavadocUtils.isJavadocComment(node)) { | ||
if (!validation.apply(node)) { | ||
throw new IllegalStateException("Position of comment isn't correct. " | ||
+ "File name is " + filename + ". " | ||
+ "Node line number is " + node.getLineNo()); | ||
} | ||
matchFound++; | ||
} | ||
matchFound += getJavadocsCount(node.getFirstChild(), filename, validation); | ||
node = node.getNextSibling(); | ||
} | ||
return matchFound; | ||
} | ||
|
||
private static String getPath(String filename) { | ||
return "src/test/resources/com/puppycrawl/tools/checkstyle/utils/blockcommentposition/" | ||
+ filename; | ||
} | ||
|
||
private static final class BlockCommentPositionTestMetadata { | ||
|
||
private final String fileName; | ||
private final Function<DetailAST, Boolean> validation; | ||
private final int matchesNum; | ||
|
||
private BlockCommentPositionTestMetadata(String fileName, Function<DetailAST, | ||
Boolean> validation, int matchesNum) { | ||
this.fileName = fileName; | ||
this.validation = validation; | ||
this.matchesNum = matchesNum; | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
public Function<DetailAST, Boolean> getValidation() { | ||
return validation; | ||
} | ||
|
||
public int getMatchesNum() { | ||
return matchesNum; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.