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

Commit

Permalink
Browse files Browse the repository at this point in the history
Issue checkstyle#5003: increase coverage of pitest-checks-metrics to 97%
  • Loading branch information
Nimfadora committed Sep 21, 2017
1 parent 28c374b commit 915eb94
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
Expand Up @@ -132,7 +132,6 @@ public final void setExcludedClasses(String... excludedClasses) {
* @param from array representing regular expressions of classes to ignore.
*/
public void setExcludeClassesRegexps(String... from) {
excludeClassesRegexps.clear();
excludeClassesRegexps.addAll(Arrays.stream(from.clone())
.map(CommonUtils::createPattern)
.collect(Collectors.toSet()));
Expand Down
Expand Up @@ -24,6 +24,8 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Set;

import org.junit.Test;

import antlr.CommonHiddenStreamToken;
Expand Down Expand Up @@ -198,4 +200,14 @@ public void testEmptyRegularExpression() throws Exception {

verify(checkConfig, getPath("InputClassDataAbstractionCoupling.java"), expected);
}

@Test
public void getDefaultTokenNames() throws Exception {
final ClassDataAbstractionCouplingCheck check = new ClassDataAbstractionCouplingCheck();
final Set<String> defaultTokenNames = check.getTokenNames();

assertEquals("Unexpected default tokens number", 1, defaultTokenNames.size());
assertEquals("Unexpected default token name", "LITERAL_NEW",
defaultTokenNames.iterator().next());
}
}
Expand Up @@ -64,6 +64,17 @@ public void testSwitchBlockAsSingleDecisionPointSetToFalse() throws Exception {
verify(checkConfig, getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
}

@Test
public void testEqualsMaxComplexity() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(CyclomaticComplexityCheck.class);
checkConfig.addAttribute("max", "5");

final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
}

@Test
public void test() throws Exception {
final DefaultConfiguration checkConfig =
Expand Down
Expand Up @@ -70,6 +70,19 @@ public void test() throws Exception {
verify(checkConfig, getPath("InputJavaNCSS.java"), expected);
}

@Test
public void testEqualToMax() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(JavaNCSSCheck.class);

checkConfig.addAttribute("methodMaximum", "12");
checkConfig.addAttribute("classMaximum", "22");
checkConfig.addAttribute("fileMaximum", "39");

final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputJavaNCSS.java"), expected);
}

@Test
public void testDefaultConfiguration() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(JavaNCSSCheck.class);
Expand Down
Expand Up @@ -21,17 +21,25 @@

import static com.puppycrawl.tools.checkstyle.checks.metrics.NPathComplexityCheck.MSG_KEY;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;

import org.junit.Assert;
import org.junit.Test;

import antlr.CommonHiddenStreamToken;
import com.google.common.collect.ImmutableMap;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.Context;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.internal.TestUtils;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

// -@cs[AbbreviationAsWordInName] Can't change check name
Expand Down Expand Up @@ -104,6 +112,65 @@ public void testIntegerOverflow() throws Exception {
verify(checkConfig, getPath("InputNPathComplexityOverflow.java"), expected);
}

@Test
public void testMultipleInputs() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(NPathComplexityCheck.class);

checkConfig.addAttribute("max", "6");
final long largerThanMaxInt = 3_486_784_401L;

final List<String> expectedFirstInput = Arrays.asList(
"65:5: " + getCheckMessage(MSG_KEY, 15, 6),
"90:5: " + getCheckMessage(MSG_KEY, 11, 6),
"100:5: " + getCheckMessage(MSG_KEY, 8, 6),
"113:5: " + getCheckMessage(MSG_KEY, 120, 6),
"135:5: " + getCheckMessage(MSG_KEY, 21, 6),
"148:5: " + getCheckMessage(MSG_KEY, 35, 6),
"156:5: " + getCheckMessage(MSG_KEY, 25, 6)
);
final List<String> expectedSecondInput = Collections.singletonList(
"13:5: " + getCheckMessage(MSG_KEY, largerThanMaxInt, 6)
);
final String firstInput = getPath("InputNPathComplexity.java");
final String secondInput = getPath("InputNPathComplexityOverflow.java");
final File[] inputs = {new File(firstInput), new File(secondInput)};

verify(createChecker(checkConfig), inputs, ImmutableMap.of(
firstInput, expectedFirstInput, secondInput, expectedSecondInput
));
}

@Test
@SuppressWarnings("unchecked")
public void testStatefulFieldsClearedOnBeginTree1() throws Exception {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.LITERAL_ELSE);

final NPathComplexityCheck check = new NPathComplexityCheck();
Assert.assertTrue("Stateful field is not cleared after beginTree",
TestUtils.isStatefulFieldClearedDuringBeginTree(check, ast, "rangeValues",
rangeValues -> ((Collection<Context>) rangeValues).isEmpty()));
Assert.assertTrue("Stateful field is not cleared after beginTree",
TestUtils.isStatefulFieldClearedDuringBeginTree(check, ast, "expressionValues",
expressionValues -> ((Collection<Context>) expressionValues).isEmpty()));
}

@Test
@SuppressWarnings("unchecked")
public void testStatefulFieldsClearedOnBeginTree2() throws Exception {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.LITERAL_RETURN);
ast.setLineNo(5);
final DetailAST child = new DetailAST();
child.setType(TokenTypes.SEMI);
ast.addChild(child);

final NPathComplexityCheck check = new NPathComplexityCheck();
Assert.assertTrue("Stateful field is not cleared after beginTree",
TestUtils.isStatefulFieldClearedDuringBeginTree(check, ast, "isAfterValues",
isAfterValues -> ((Collection<Context>) isAfterValues).isEmpty()));
}

@Test
public void testDefaultConfiguration() throws Exception {
final DefaultConfiguration checkConfig =
Expand Down

0 comments on commit 915eb94

Please sign in to comment.