Skip to content

Commit 55503c3

Browse files
Add GDSL/DSLD for ConditionalExtension's annotations (#1808)
Prior to this commit @requires, @IgnoreIf, @PendingFeatureIf, and @Retry didn't have any IDE support for the condition closure. Co-authored-by: Eric Milles <eric.milles@thomsonreuters.com>
1 parent 2b8021a commit 55503c3

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

spock-core/src/main/java/org/spockframework/runtime/extension/builtin/PreconditionContext.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
import spock.lang.IgnoreIf;
2525
import spock.lang.PendingFeatureIf;
2626
import spock.lang.Requires;
27+
import spock.lang.Specification;
2728
import spock.util.environment.Jvm;
2829
import spock.util.environment.OperatingSystem;
2930

3031
/**
3132
* The context (delegate) for a {@link Requires}, {@link IgnoreIf} or {@link PendingFeatureIf} condition.
3233
*/
33-
public class PreconditionContext {
34+
public class PreconditionContext<S extends Specification> {
3435
private final Object theSharedInstance;
3536
private final Object theInstance;
3637
private final Map<String, Object> dataVariables;
@@ -88,11 +89,11 @@ public Jvm getJvm() {
8889
* @since 2.0
8990
* @return the test instance
9091
*/
91-
public Object getInstance() {
92+
public S getInstance() {
9293
if (theInstance == null) {
9394
throw new InstanceContextException();
9495
}
95-
return theInstance;
96+
return (S) theInstance;
9697
}
9798

9899
/**
@@ -103,11 +104,11 @@ public Object getInstance() {
103104
* @since 2.1
104105
* @return the shared instance
105106
*/
106-
public Object getShared() {
107+
public S getShared() {
107108
if (theSharedInstance == null) {
108109
throw new SharedContextException();
109110
}
110-
return theSharedInstance;
111+
return (S) theSharedInstance;
111112
}
112113

113114
/**

spock-core/src/main/java/org/spockframework/runtime/extension/builtin/RetryConditionContext.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.spockframework.runtime.extension.builtin;
22

3+
import spock.lang.Specification;
4+
35
/**
46
* The context (delegate) for a {@link spock.lang.Retry} condition.
57
*/
6-
public class RetryConditionContext {
8+
public class RetryConditionContext<S extends Specification> {
79

810
private final Object instance;
911
private final Throwable failure;
@@ -27,8 +29,8 @@ public Throwable getFailure() {
2729
*
2830
* @return the current {@code Specification} instance
2931
*/
30-
public Object getInstance() {
31-
return instance;
32+
public S getInstance() {
33+
return (S) instance;
3234
}
3335

3436
}

spock-core/src/main/resources/dsld/spk.dsld renamed to spock-core/src/main/resources/dsld/spock_tests.dsld

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package dsld
1616

17+
import org.codehaus.groovy.ast.*
1718
import org.codehaus.groovy.ast.expr.*
1819

1920
assertVersion(groovyEclipse: "2.7.2") // tested against this version
@@ -50,3 +51,29 @@ contribute(
5051
}
5152
}
5253

54+
55+
// From https://issues.apache.org/jira/browse/GROOVY-9510
56+
// Properly resolve the delegatesTo for the closures of these extensions
57+
def hasPrecondition = { annotatedBy(name('spock.lang.Requires') | name('spock.lang.IgnoreIf') | name('spock.lang.PendingFeatureIf')) }
58+
59+
contribute(inClosure() & isThisType() & bind(classes: enclosingClass(subType('spock.lang.Specification')))
60+
& (enclosingClass(conditions: hasPrecondition()) | enclosingMethod(conditions: hasPrecondition()))) {
61+
for (AnnotationNode pre : conditions) {
62+
def condition = pre.getMember('value')
63+
if (condition.getCode().is(currentNode)) {
64+
delegateType = resolver.resolve("org.spockframework.runtime.extension.builtin.PreconditionContext<${classes[0].getName()}>")
65+
return
66+
}
67+
}
68+
}
69+
70+
contribute(inClosure() & isThisType() & bind(classes: enclosingClass(subType('spock.lang.Specification')))
71+
& (enclosingClass(annotations: annotatedBy('spock.lang.Retry')) | enclosingMethod(annotations: annotatedBy('spock.lang.Retry')))) {
72+
for (AnnotationNode retry : annotations) {
73+
def condition = retry.getMember('condition')
74+
if (condition.getCode().is(currentNode)) {
75+
delegateType = resolver.resolve("org.spockframework.runtime.extension.builtin.RetryConditionContext<${classes[0].getName()}>")
76+
return
77+
}
78+
}
79+
}

spock-core/src/main/resources/org/spockframework/idea/spock.gdsl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,14 @@ contributor(ctx, {
4545
})
4646

4747

48+
// From https://issues.apache.org/jira/browse/GROOVY-9510
49+
// Properly resolve the delegatesTo for the closures of these extensions
50+
def conditionalExtensions = ['spock.lang.Requires', 'spock.lang.IgnoreIf', 'spock.lang.PendingFeatureIf']
51+
.collect { annot -> context(ctype: "spock.lang.Specification", scope: closureScope(annotationName: annot))}
52+
contributor(conditionalExtensions, {
53+
delegatesTo(findClass('org.spockframework.runtime.extension.builtin.PreconditionContext'))
54+
})
55+
56+
contributor(context(ctype: "spock.lang.Specification", scope: closureScope(annotationName: 'spock.lang.Retry')), {
57+
delegatesTo(findClass('org.spockframework.runtime.extension.builtin.RetryConditionContext'))
58+
})

0 commit comments

Comments
 (0)