forked from spockframework/spock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Discovery to allow extensions to include excluded tests again...
Call RunContext.stop() on SpockEngineDescriptor.after() Co-authored-by: Marc Philipp <mail@marcphilipp.de>
- Loading branch information
1 parent
884bb9a
commit 5a35ab1
Showing
6 changed files
with
121 additions
and
120 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
121 changes: 38 additions & 83 deletions
121
spock-core/src/main/java/org/spockframework/runtime/MethodSelectorResolver.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 |
---|---|---|
@@ -1,110 +1,65 @@ | ||
package org.spockframework.runtime; | ||
|
||
import org.spockframework.runtime.model.*; | ||
import org.spockframework.runtime.model.FeatureInfo; | ||
|
||
import java.util.*; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
import org.junit.platform.engine.*; | ||
import org.junit.platform.engine.discovery.MethodSelector; | ||
import org.junit.platform.engine.discovery.UniqueIdSelector; | ||
import org.junit.platform.engine.discovery.*; | ||
import org.junit.platform.engine.support.discovery.SelectorResolver; | ||
|
||
import static java.util.Collections.*; | ||
import static org.junit.platform.engine.discovery.DiscoverySelectors.*; | ||
|
||
public class MethodSelectorResolver implements SelectorResolver { | ||
|
||
private static final Object[] EMPTY_ARGS = new Object[0]; | ||
|
||
@Override | ||
public Resolution resolve(MethodSelector selector, Context context) { | ||
return context | ||
.addToParent(() -> selectClass(selector.getJavaClass()), parent -> { | ||
if (parent instanceof SpecNode) { | ||
String methodName = selector.getMethodName(); | ||
Optional<SpockNode> node = toNode((SpecNode) parent, feature -> feature.getFeatureMethod().getReflection().getName().equals(methodName)); | ||
if (!node.isPresent()) { | ||
node = toNode((SpecNode) parent, feature -> methodName.equals(feature.getName())); | ||
} | ||
return node; | ||
} | ||
return Optional.empty(); | ||
}) | ||
.map(node -> Resolution.match(Match.exact(node, expansionCallback(node)))) | ||
.orElse(Resolution.unresolved()); | ||
|
||
String methodName = selector.getMethodName(); | ||
Predicate<FeatureInfo> filter = feature -> | ||
feature.getFeatureMethod().getReflection().getName().equals(methodName) | ||
|| methodName.equals(feature.getName()); | ||
|
||
DiscoverySelector parentSelector = selectClass(selector.getJavaClass()); | ||
return resolve(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))) | ||
.orElseGet(Resolution::unresolved); | ||
} | ||
|
||
private TestDescriptor handle(TestDescriptor testDescriptor, Predicate<FeatureInfo> filter) { | ||
if (testDescriptor instanceof SpecNode) { | ||
SpecNode specNode = (SpecNode) testDescriptor; | ||
long count = specNode.getSpecInfo().getAllFeaturesInExecutionOrder().stream() | ||
.filter(filter) | ||
.peek(featureInfo -> featureInfo.setExcluded(false)) | ||
.count(); | ||
return count == 0 ? null : testDescriptor; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Resolution resolve(UniqueIdSelector selector, Context context) { | ||
UniqueId uniqueId = selector.getUniqueId(); | ||
UniqueId.Segment lastSegment = uniqueId.getLastSegment(); | ||
if ("feature".equals(lastSegment.getType())) { | ||
return context | ||
.addToParent(() -> selectUniqueId(uniqueId.removeLastSegment()), parent -> { | ||
if (parent instanceof SpecNode) { | ||
String methodName = lastSegment.getValue(); | ||
return toNode((SpecNode) parent, feature -> methodName.equals(feature.getFeatureMethod().getReflection().getName())); | ||
} | ||
return Optional.empty(); | ||
}) | ||
.map(node -> Resolution.match(Match.exact(node, expansionCallback(node)))) | ||
.orElse(Resolution.unresolved()); | ||
String methodName = lastSegment.getValue(); | ||
return resolve(context, | ||
selectUniqueId(uniqueId.removeLastSegment()), | ||
feature -> methodName.equals(feature.getFeatureMethod().getReflection().getName())); | ||
} | ||
if ("iteration".equals(lastSegment.getType())) { | ||
return context | ||
.addToParent(() -> selectUniqueId(uniqueId.removeLastSegment()), parent -> { | ||
if (parent instanceof ParameterizedFeatureNode) { | ||
FeatureNode featureNode = (FeatureNode) parent; | ||
int iterationIndex = Integer.parseInt(lastSegment.getValue()); | ||
// TODO Add iterationIndex as allowed index to featureNode | ||
} | ||
return Optional.empty(); | ||
}) | ||
.map(node -> Resolution.match(Match.partial(node, expansionCallback(node)))) | ||
.orElse(Resolution.unresolved()); | ||
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(); | ||
} | ||
|
||
private Optional<SpockNode> toNode(SpecNode specNode, Predicate<FeatureInfo> predicate) { | ||
return specNode.getSpecInfo().getAllFeaturesInExecutionOrder().stream() | ||
.filter(predicate) | ||
.filter(feature -> !feature.isExcluded()) | ||
.findAny() | ||
.map(feature -> createNode(specNode, feature)); | ||
} | ||
|
||
private SpockNode createNode(SpecNode specNode, FeatureInfo feature) { | ||
if (feature.isParameterized()) { | ||
return describeParameterizedFeature(specNode.getUniqueId(), feature); | ||
} else { | ||
return describeSimpleFeature(specNode.getUniqueId(), feature); | ||
} | ||
} | ||
|
||
private FeatureNode describeParameterizedFeature(UniqueId parentId, FeatureInfo feature) { | ||
return new ParameterizedFeatureNode(toUniqueId(parentId, feature), feature); | ||
} | ||
|
||
private SpockNode describeSimpleFeature(UniqueId parentId, FeatureInfo feature) { | ||
IterationInfo iterationInfo = new IterationInfo(feature, EMPTY_ARGS, 1); | ||
iterationInfo.setName(feature.getName()); | ||
UniqueId uniqueId = toUniqueId(parentId, feature); | ||
IterationNode iterationNode = new IterationNode(toUniqueId(uniqueId, feature), iterationInfo); | ||
return new SimpleFeatureNode(uniqueId, feature, iterationNode); | ||
} | ||
|
||
private UniqueId toUniqueId(UniqueId parentId, FeatureInfo feature) { | ||
return parentId.append("feature", feature.getFeatureMethod().getReflection().getName()); | ||
} | ||
|
||
private Supplier<Set<? extends DiscoverySelector>> expansionCallback(TestDescriptor node) { | ||
return () -> { | ||
// TODO allow all iteration indexes | ||
return emptySet(); | ||
}; | ||
} | ||
|
||
} |
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
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
59 changes: 59 additions & 0 deletions
59
spock-core/src/main/java/org/spockframework/runtime/SpockEngineDiscoveryPostProcessor.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,59 @@ | ||
package org.spockframework.runtime; | ||
|
||
import org.spockframework.runtime.model.*; | ||
|
||
import org.junit.platform.engine.*; | ||
|
||
class SpockEngineDiscoveryPostProcessor { | ||
|
||
private static final Object[] EMPTY_ARGS = new Object[0]; | ||
|
||
SpockEngineDescriptor postProcessEngineDescriptor(UniqueId uniqueId, RunContext runContext, | ||
SpockEngineDescriptor engineDescriptor) { | ||
SpockEngineDescriptor processedEngineDescriptor = new SpockEngineDescriptor(uniqueId, runContext); | ||
engineDescriptor.getChildren().stream() | ||
.map(child -> processSpecNode(child, runContext)) | ||
.forEach(processedEngineDescriptor::addChild); | ||
return processedEngineDescriptor; | ||
} | ||
|
||
private SpockNode createNode(SpecNode specNode, FeatureInfo feature) { | ||
if (feature.isParameterized()) { | ||
return describeParameterizedFeature(specNode.getUniqueId(), feature); | ||
} else { | ||
return describeSimpleFeature(specNode.getUniqueId(), feature); | ||
} | ||
} | ||
|
||
private FeatureNode describeParameterizedFeature(UniqueId parentId, FeatureInfo feature) { | ||
return new ParameterizedFeatureNode(toUniqueId(parentId, feature), feature); | ||
} | ||
|
||
private SpockNode describeSimpleFeature(UniqueId parentId, FeatureInfo feature) { | ||
IterationInfo iterationInfo = new IterationInfo(feature, EMPTY_ARGS, 1); | ||
iterationInfo.setName(feature.getName()); | ||
UniqueId uniqueId = toUniqueId(parentId, feature); | ||
IterationNode iterationNode = new IterationNode(toUniqueId(uniqueId, feature), iterationInfo); | ||
return new SimpleFeatureNode(uniqueId, feature, iterationNode); | ||
} | ||
|
||
private UniqueId toUniqueId(UniqueId parentId, FeatureInfo feature) { | ||
return parentId.append("feature", feature.getFeatureMethod().getReflection().getName()); | ||
} | ||
|
||
private TestDescriptor processSpecNode(TestDescriptor child, RunContext runContext) { | ||
if (child instanceof SpecNode) { | ||
SpecNode specNode = (SpecNode) child; | ||
try { | ||
runContext.createExtensionRunner(specNode.getSpecInfo()).run(); | ||
} catch (Exception e) { | ||
return new ErrorSpecNode(specNode.getUniqueId(), specNode.getSpecInfo(), e); | ||
} | ||
specNode.getSpecInfo().getAllFeaturesInExecutionOrder().stream() | ||
.filter(featureInfo -> !featureInfo.isExcluded()) | ||
.map(featureInfo -> createNode(specNode, featureInfo)) | ||
.forEach(specNode::addChild); | ||
} | ||
return child; | ||
} | ||
} |