Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for selecting individual iterations #1376

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import org.spockframework.runtime.model.FeatureInfo;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

import org.junit.platform.engine.*;
import org.junit.platform.engine.discovery.*;
import org.junit.platform.engine.support.discovery.SelectorResolver;
import org.spockframework.runtime.model.IterationFilter;

import static org.junit.platform.engine.discovery.DiscoverySelectors.*;

Expand All @@ -21,12 +24,58 @@ public Resolution resolve(MethodSelector selector, Context context) {
|| methodName.equals(feature.getName());

DiscoverySelector parentSelector = selectClass(selector.getJavaClass());
return resolve(context, parentSelector, filter);
return resolveAllowingAllIndexes(context, parentSelector, filter);
}

Resolution resolve(Context context, DiscoverySelector parentSelector, Predicate<FeatureInfo> filter) {
return context.resolve(parentSelector).map(testDescriptor -> handle(testDescriptor, filter))
.map(descriptor -> Resolution.match(Match.partial(descriptor)))
@Override
public Resolution resolve(UniqueIdSelector selector, Context context) {
UniqueId uniqueId = selector.getUniqueId();
UniqueId.Segment lastSegment = uniqueId.getLastSegment();
if ("feature".equals(lastSegment.getType())) {
String methodName = lastSegment.getValue();
return resolveAllowingAllIndexes(
context,
selectUniqueId(uniqueId.removeLastSegment()),
feature -> methodName.equals(feature.getFeatureMethod().getReflection().getName())
);
}
if ("iteration".equals(lastSegment.getType())) {
int index = Integer.parseInt(lastSegment.getValue());
UniqueId featureMethodUniqueId = uniqueId.removeLastSegment();
String methodName = featureMethodUniqueId.getLastSegment().getValue();
return resolveWithIterationFilter(
context,
selectUniqueId(featureMethodUniqueId.removeLastSegment()),
feature -> methodName.equals(feature.getFeatureMethod().getReflection().getName()),
iterationFilter -> iterationFilter.allow(index)
);
}
return Resolution.unresolved();
}

private Resolution resolveAllowingAllIndexes(Context context, DiscoverySelector parentSelector, Predicate<FeatureInfo> featureFilter) {
return resolveWithIterationFilter(context, parentSelector, featureFilter, IterationFilter::allowAll);
}

private Resolution resolveWithIterationFilter(Context context, DiscoverySelector parentSelector, Predicate<FeatureInfo> featureFilter, Consumer<IterationFilter> iterationFilterAdjuster) {
return resolve(context,
parentSelector,
featureFilter,
testDescriptor -> {
((SpecNode) testDescriptor).getNodeInfo().getAllFeatures().stream()
.filter(featureFilter)
.findAny()
.map(FeatureInfo::getIterationFilter)
.ifPresent(iterationFilterAdjuster);
return Match.partial(testDescriptor);
}
);
}

private Resolution resolve(Context context, DiscoverySelector parentSelector, Predicate<FeatureInfo> filter, Function<TestDescriptor, Match> matchCreator) {
return context.resolve(parentSelector)
.map(testDescriptor -> handle(testDescriptor, filter))
.map(descriptor -> Resolution.match(matchCreator.apply(descriptor)))
.orElseGet(Resolution::unresolved);
}

Expand All @@ -41,25 +90,4 @@ private TestDescriptor handle(TestDescriptor testDescriptor, Predicate<FeatureIn
}
return null;
}

@Override
public Resolution resolve(UniqueIdSelector selector, Context context) {
UniqueId uniqueId = selector.getUniqueId();
UniqueId.Segment lastSegment = uniqueId.getLastSegment();
if ("feature".equals(lastSegment.getType())) {
String methodName = lastSegment.getValue();
return resolve(context,
selectUniqueId(uniqueId.removeLastSegment()),
feature -> methodName.equals(feature.getFeatureMethod().getReflection().getName()));
}
if ("iteration".equals(lastSegment.getType())) {
return resolve(selectUniqueId(uniqueId.removeLastSegment()), context);
// if (parent instanceof ParameterizedFeatureNode) {
// FeatureNode featureNode = (FeatureNode) parent;
// int iterationIndex = Integer.parseInt(lastSegment.getValue());
// // TODO Add iterationIndex as allowed index to featureNode
// }
}
return Resolution.unresolved();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ private void runIterations(SpockExecutionContext context, ParameterizedFeatureCh
if (context.getErrorInfoCollector().hasErrors()) {
return;
}
childExecutor.execute(iterationNode);
if (iterationInfo.getFeature().getIterationFilter().isAllowed(iterationInfo.getIterationIndex())) {
childExecutor.execute(iterationNode);
}

// no iterators => no data providers => only derived parameterizations => limit to one iteration
if (iterators.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class FeatureInfo extends SpecElementInfo<SpecInfo, AnnotatedElement> {
private MethodInfo dataProcessorMethod;
private NameProvider<IterationInfo> iterationNameProvider;
private final List<DataProviderInfo> dataProviders = new ArrayList<>();
private final IterationFilter iterationFilter = new IterationFilter();

private boolean reportIterations = true;

Expand Down Expand Up @@ -152,6 +153,10 @@ public void setIterationNameProvider(NameProvider<IterationInfo> provider) {
iterationNameProvider = provider;
}

public IterationFilter getIterationFilter() {
return iterationFilter;
}

/**
* Tells if any of the methods associated with this feature has the specified
* name in bytecode.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.spockframework.runtime.model;

import java.util.HashSet;
import java.util.Set;

public class IterationFilter {

private final Set<Integer> allowed = new HashSet<>();
private Mode mode = Mode.EXPLICIT;

public void allow(int index) {
if (this.mode == Mode.EXPLICIT) {
this.allowed.add(index);
}
}

public void allowAll() {
this.mode = Mode.ALLOW_ALL;
this.allowed.clear();
}

public boolean isAllowed(int index) {
return allowed.isEmpty() || allowed.contains(index);
}

private enum Mode {
EXPLICIT, ALLOW_ALL
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.spockframework.runtime.model

import spock.lang.Specification
import spock.lang.Subject

class IterationFilterSpec extends Specification {

@Subject
def filter = new IterationFilter()

def "allows all indexes by default"() {
expect:
filter.isAllowed(0)
filter.isAllowed(1)
}

def "allows only selected indexes after one has been allowed explicitly"() {
when:
filter.allow(0)

then:
filter.isAllowed(0)
!filter.isAllowed(1)
}

def "allows all indexes after configured to do so"() {
when:
filter.allow(0)
filter.allowAll()

then:
filter.isAllowed(0)
filter.isAllowed(1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,35 @@ void packageSelectorsAreResolved() {
.count());
}

@Test
void iterationsAreResolved() {
UniqueId featureMethodUniqueId = UniqueId.forEngine("spock")
.append("spec", UnrollTestCase.class.getName())
.append("feature", "$spock_feature_0_0");

execute(
selectUniqueId(featureMethodUniqueId.append("iteration", "1")),
stats -> stats.started(2).succeeded(1).failed(1)
);
execute(
asList(
selectUniqueId(featureMethodUniqueId.append("iteration", "0")),
selectUniqueId(featureMethodUniqueId.append("iteration", "2"))
),
stats -> stats.started(3).succeeded(3)
);
execute(
asList(
selectUniqueId(featureMethodUniqueId.append("iteration", "0")),
selectUniqueId(featureMethodUniqueId)
),
stats -> stats.started(4).succeeded(3).failed(1)
);
}

@Test
void verifyUnrollExample() {
execute(selectClass(UnrollTestCase.class), stats -> stats.started(13).succeeded(13));
execute(selectClass(UnrollTestCase.class), stats -> stats.started(13).succeeded(12).failed(1));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UnrollTestCase extends Specification {
where:
a | b | c
1 | 2 | 2
2 | 2 | 2
2 | 2 | 0
3 | 2 | 3
}

Expand Down