Skip to content

Commit

Permalink
Add GDSL/DSLD for ConditionalExtension's annotations (#1808)
Browse files Browse the repository at this point in the history
Prior to this commit @requires, @IgnoreIf, @PendingFeatureIf, and @Retry
didn't have any IDE support for the condition closure.

Co-authored-by: Eric Milles <[email protected]>
  • Loading branch information
leonard84 and eric-milles authored Oct 11, 2023
1 parent 2b8021a commit 55503c3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
import spock.lang.IgnoreIf;
import spock.lang.PendingFeatureIf;
import spock.lang.Requires;
import spock.lang.Specification;
import spock.util.environment.Jvm;
import spock.util.environment.OperatingSystem;

/**
* The context (delegate) for a {@link Requires}, {@link IgnoreIf} or {@link PendingFeatureIf} condition.
*/
public class PreconditionContext {
public class PreconditionContext<S extends Specification> {
private final Object theSharedInstance;
private final Object theInstance;
private final Map<String, Object> dataVariables;
Expand Down Expand Up @@ -88,11 +89,11 @@ public Jvm getJvm() {
* @since 2.0
* @return the test instance
*/
public Object getInstance() {
public S getInstance() {
if (theInstance == null) {
throw new InstanceContextException();
}
return theInstance;
return (S) theInstance;
}

/**
Expand All @@ -103,11 +104,11 @@ public Object getInstance() {
* @since 2.1
* @return the shared instance
*/
public Object getShared() {
public S getShared() {
if (theSharedInstance == null) {
throw new SharedContextException();
}
return theSharedInstance;
return (S) theSharedInstance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.spockframework.runtime.extension.builtin;

import spock.lang.Specification;

/**
* The context (delegate) for a {@link spock.lang.Retry} condition.
*/
public class RetryConditionContext {
public class RetryConditionContext<S extends Specification> {

private final Object instance;
private final Throwable failure;
Expand All @@ -27,8 +29,8 @@ public Throwable getFailure() {
*
* @return the current {@code Specification} instance
*/
public Object getInstance() {
return instance;
public S getInstance() {
return (S) instance;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package dsld

import org.codehaus.groovy.ast.*
import org.codehaus.groovy.ast.expr.*

assertVersion(groovyEclipse: "2.7.2") // tested against this version
Expand Down Expand Up @@ -50,3 +51,29 @@ contribute(
}
}


// From https://issues.apache.org/jira/browse/GROOVY-9510
// Properly resolve the delegatesTo for the closures of these extensions
def hasPrecondition = { annotatedBy(name('spock.lang.Requires') | name('spock.lang.IgnoreIf') | name('spock.lang.PendingFeatureIf')) }

contribute(inClosure() & isThisType() & bind(classes: enclosingClass(subType('spock.lang.Specification')))
& (enclosingClass(conditions: hasPrecondition()) | enclosingMethod(conditions: hasPrecondition()))) {
for (AnnotationNode pre : conditions) {
def condition = pre.getMember('value')
if (condition.getCode().is(currentNode)) {
delegateType = resolver.resolve("org.spockframework.runtime.extension.builtin.PreconditionContext<${classes[0].getName()}>")
return
}
}
}

contribute(inClosure() & isThisType() & bind(classes: enclosingClass(subType('spock.lang.Specification')))
& (enclosingClass(annotations: annotatedBy('spock.lang.Retry')) | enclosingMethod(annotations: annotatedBy('spock.lang.Retry')))) {
for (AnnotationNode retry : annotations) {
def condition = retry.getMember('condition')
if (condition.getCode().is(currentNode)) {
delegateType = resolver.resolve("org.spockframework.runtime.extension.builtin.RetryConditionContext<${classes[0].getName()}>")
return
}
}
}
11 changes: 11 additions & 0 deletions spock-core/src/main/resources/org/spockframework/idea/spock.gdsl
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,14 @@ contributor(ctx, {
})


// From https://issues.apache.org/jira/browse/GROOVY-9510
// Properly resolve the delegatesTo for the closures of these extensions
def conditionalExtensions = ['spock.lang.Requires', 'spock.lang.IgnoreIf', 'spock.lang.PendingFeatureIf']
.collect { annot -> context(ctype: "spock.lang.Specification", scope: closureScope(annotationName: annot))}
contributor(conditionalExtensions, {
delegatesTo(findClass('org.spockframework.runtime.extension.builtin.PreconditionContext'))
})

contributor(context(ctype: "spock.lang.Specification", scope: closureScope(annotationName: 'spock.lang.Retry')), {
delegatesTo(findClass('org.spockframework.runtime.extension.builtin.RetryConditionContext'))
})

0 comments on commit 55503c3

Please sign in to comment.