Skip to content

Commit f39eea7

Browse files
Add support for IterationSelector to run individual iterations (#1379)
Co-authored-by: Leonard Brünings <leonard.bruenings@gradle.com>
1 parent 49bd1dd commit f39eea7

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

spock-core/src/main/java/org/spockframework/runtime/MethodSelectorResolver.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public Resolution resolve(MethodSelector selector, Context context) {
2020

2121
String methodName = selector.getMethodName();
2222
Predicate<FeatureInfo> filter = feature ->
23-
feature.getFeatureMethod().getReflection().getName().equals(methodName)
24-
|| methodName.equals(feature.getName());
23+
reflectionNameOrMethodNameEquals(methodName, feature);
2524

2625
DiscoverySelector parentSelector = selectClass(selector.getJavaClass());
2726
return resolveAllowingAllIndexes(context, parentSelector, filter);
@@ -53,6 +52,21 @@ public Resolution resolve(UniqueIdSelector selector, Context context) {
5352
return Resolution.unresolved();
5453
}
5554

55+
@Override
56+
public Resolution resolve(IterationSelector selector, Context context) {
57+
if (selector.getParentSelector() instanceof MethodSelector) {
58+
MethodSelector methodSelector = (MethodSelector) selector.getParentSelector();
59+
60+
return resolveWithIterationFilter(
61+
context,
62+
selectClass(methodSelector.getJavaClass()),
63+
feature -> reflectionNameOrMethodNameEquals(methodSelector.getMethodName(), feature),
64+
iterationFilter -> selector.getIterationIndices().forEach(iterationFilter::allow)
65+
);
66+
}
67+
return SelectorResolver.super.resolve(selector, context);
68+
}
69+
5670
private Resolution resolveAllowingAllIndexes(Context context, DiscoverySelector parentSelector, Predicate<FeatureInfo> featureFilter) {
5771
return resolveWithIterationFilter(context, parentSelector, featureFilter, IterationFilter::allowAll);
5872
}
@@ -90,4 +104,9 @@ private TestDescriptor handle(TestDescriptor testDescriptor, Predicate<FeatureIn
90104
}
91105
return null;
92106
}
107+
108+
private static boolean reflectionNameOrMethodNameEquals(String methodName, FeatureInfo feature) {
109+
return feature.getFeatureMethod().getReflection().getName().equals(methodName)
110+
|| methodName.equals(feature.getName());
111+
}
93112
}

spock-testkit/src/test/groovy/spock/platform/SpockHelloWorldTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package spock.platform;
22

3+
import org.junit.platform.engine.discovery.MethodSelector;
34
import org.junit.platform.launcher.Launcher;
45
import org.junit.platform.launcher.TestPlan;
56
import org.junit.platform.launcher.core.LauncherConfig;
@@ -71,7 +72,7 @@ void packageSelectorsAreResolved() {
7172
}
7273

7374
@Test
74-
void iterationsAreResolved() {
75+
void iterationsAreResolvedViaUniqueIdSelector() {
7576
UniqueId featureMethodUniqueId = UniqueId.forEngine("spock")
7677
.append("spec", UnrollTestCase.class.getName())
7778
.append("feature", "$spock_feature_0_0");
@@ -96,6 +97,37 @@ void iterationsAreResolved() {
9697
);
9798
}
9899

100+
@Test
101+
void iterationsAreResolvedViaIterationSelector() {
102+
UniqueId featureMethodUniqueId = UniqueId.forEngine("spock")
103+
.append("spec", UnrollTestCase.class.getName())
104+
.append("feature", "$spock_feature_0_0");
105+
MethodSelector methodSelector = selectMethod(UnrollTestCase.class, "unrollMe");
106+
107+
execute(
108+
selectIteration(methodSelector, 1),
109+
stats -> stats.started(2).succeeded(1).failed(1)
110+
);
111+
execute(
112+
selectIteration(methodSelector, 0, 2),
113+
stats -> stats.started(3).succeeded(3)
114+
);
115+
execute(
116+
asList(
117+
selectIteration(methodSelector, 1),
118+
selectUniqueId(featureMethodUniqueId)
119+
),
120+
stats -> stats.started(4).succeeded(3).failed(1)
121+
);
122+
execute(
123+
asList(
124+
selectIteration(methodSelector, 0),
125+
selectUniqueId(featureMethodUniqueId.append("iteration", "2"))
126+
),
127+
stats -> stats.started(3).succeeded(3)
128+
);
129+
}
130+
99131
@Test
100132
void verifyUnrollExample() {
101133
execute(selectClass(UnrollTestCase.class), stats -> stats.started(13).succeeded(12).failed(1));

0 commit comments

Comments
 (0)