Skip to content

Commit

Permalink
Merge pull request #1116 from hcoles/feature/line_filter
Browse files Browse the repository at this point in the history
feature to exclude lines 0 and 1 from mutation
  • Loading branch information
hcoles committed Nov 21, 2022
2 parents 89c4aec + 3eb1e6d commit 332ffff
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.pitest.mutationtest.build.intercept.exclude;

import org.pitest.bytecode.analysis.ClassTree;
import org.pitest.mutationtest.build.InterceptorType;
import org.pitest.mutationtest.build.MutationInterceptor;
import org.pitest.mutationtest.engine.Mutater;
import org.pitest.mutationtest.engine.MutationDetails;

import java.util.Collection;
import java.util.stream.Collectors;

class FirstLineFilter implements MutationInterceptor {
@Override
public InterceptorType type() {
return InterceptorType.FILTER;
}

@Override
public void begin(ClassTree clazz) {

}

@Override
public Collection<MutationDetails> intercept(Collection<MutationDetails> mutations, Mutater unused) {
return mutations.stream()
.filter(m -> m.getLineNumber() > 1)
.collect(Collectors.toList());
}

@Override
public void end() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.pitest.mutationtest.build.intercept.exclude;

import org.pitest.mutationtest.build.InterceptorParameters;
import org.pitest.mutationtest.build.MutationInterceptor;
import org.pitest.mutationtest.build.MutationInterceptorFactory;
import org.pitest.plugin.Feature;

public class FirstLineInterceptorFactory implements MutationInterceptorFactory {

@Override
public String description() {
return "Filters mutants with line number <= 1";
}

@Override
public MutationInterceptor createInterceptor(InterceptorParameters params) {
return new FirstLineFilter();
}

@Override
public Feature provides() {
return Feature.named("nofirstline")
.withDescription(description())
.withOnByDefault(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ org.pitest.mutationtest.build.intercept.kotlin.KotlinFilterFactory
org.pitest.mutationtest.filter.LimitNumberOfMutationsPerClassFilterFactory
org.pitest.mutationtest.build.intercept.equivalent.EqualsPerformanceShortcutFilterFactory
org.pitest.mutationtest.build.intercept.equivalent.EquivalentReturnMutationFilter
org.pitest.mutationtest.build.intercept.exclude.FirstLineInterceptorFactory

org.pitest.plugin.export.MutantExportFactory

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.pitest.mutationtest.build.intercept.exclude;

import junit.framework.TestCase;
import org.junit.Test;
import org.pitest.mutationtest.engine.MutationDetails;
import org.pitest.mutationtest.engine.MutationDetailsMother;

import java.util.List;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

public class FirstLineFilterTest {

FirstLineFilter underTest = new FirstLineFilter();

@Test
public void doesNotFilterMutantsAfterFirstLine() {
List<MutationDetails> mutants = MutationDetailsMother.aMutationDetail()
.withLineNumber(2)
.build(10);

assertThat(underTest.intercept(mutants, null)).containsAll(mutants);
}

@Test
public void filtersMutantsOnLine1() {
MutationDetails line1 = MutationDetailsMother.aMutationDetail()
.withLineNumber(1)
.build();

MutationDetails line2 = MutationDetailsMother.aMutationDetail()
.withLineNumber(2)
.build();

assertThat(underTest.intercept(asList(line1, line2), null)).containsExactly(line2);
}

@Test
public void filtersMutantsOnLine0() {
MutationDetails line0 = MutationDetailsMother.aMutationDetail()
.withLineNumber(0)
.build();

MutationDetails line2 = MutationDetailsMother.aMutationDetail()
.withLineNumber(2)
.build();

assertThat(underTest.intercept(asList(line0, line2), null)).containsExactly(line2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.pitest.mutationtest.build.intercept.exclude;

import org.junit.Test;
import org.pitest.mutationtest.build.InterceptorType;
import org.pitest.verifier.interceptors.FactoryVerifier;

public class FirstLineInterceptorFactoryTest {

@Test
public void isOnChain() {
FactoryVerifier.confirmFactory(new FirstLineInterceptorFactory())
.isOnChain();
}

@Test
public void isOffByDefault() {
FactoryVerifier.confirmFactory(new FirstLineInterceptorFactory())
.isOffByDefault();
}


@Test
public void featureIsCalledNoFirstLine() {
FactoryVerifier.confirmFactory(new FirstLineInterceptorFactory())
.featureName().isEqualTo("nofirstline");
}

@Test
public void createsFilters() {
FactoryVerifier.confirmFactory(new FirstLineInterceptorFactory())
.createsInterceptorsOfType(InterceptorType.FILTER);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.pitest.verifier.interceptors;

import org.assertj.core.api.AbstractStringAssert;
import org.pitest.mutationtest.build.InterceptorParameters;
import org.pitest.mutationtest.build.InterceptorType;
import org.pitest.mutationtest.build.MutationInterceptorFactory;
import org.pitest.mutationtest.config.PluginServices;
import org.pitest.mutationtest.config.ReportOptions;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

public class FactoryVerifier {

private final MutationInterceptorFactory factory;
private final ReportOptions data;

public FactoryVerifier(MutationInterceptorFactory factory) {
this(factory, emptyOptions());
}

public FactoryVerifier(MutationInterceptorFactory factory, ReportOptions data) {
this.factory = factory;
this.data = data;
}

public static FactoryVerifier confirmFactory(MutationInterceptorFactory factory) {
return new FactoryVerifier(factory);
}

public void isOnChain() {
factoryIsOnChain(factory.getClass());
}

public FactoryVerifier withData(ReportOptions data) {
return new FactoryVerifier(factory, data);
}

public void createsInterceptorsOfType(InterceptorType type) {
assertThat(factory.createInterceptor(emptyParams(data)).type()).isEqualTo(type);
}

public void isOnByDefault() {
assertThat(factory.provides().isOnByDefault()).isTrue();
}

public void isOffByDefault() {
assertThat(factory.provides().isOnByDefault()).isFalse();
}

public AbstractStringAssert<?> featureName() {
return assertThat(factory.provides().name());
}

private static void factoryIsOnChain(Class<?> factory) {
List<Class<?>> allInterceptors = PluginServices.makeForContextLoader().findInterceptors().stream()
.map(MutationInterceptorFactory::getClass)
.collect(Collectors.toList());

assertThat(allInterceptors).contains(factory);
}

public static InterceptorParameters emptyParams(ReportOptions data) {
return new InterceptorParameters(null, data, null, null);
}

public static ReportOptions emptyOptions() {
ReportOptions data = new ReportOptions();
data.setSourceDirs(Collections.emptyList());
data.setReportDir("");
return data;
}

}

0 comments on commit 332ffff

Please sign in to comment.