Skip to content

Commit aaa0262

Browse files
committed
Review Feedback Vol. 1
1 parent 56a0c00 commit aaa0262

File tree

14 files changed

+50
-32
lines changed

14 files changed

+50
-32
lines changed

docs/release_notes.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ will now require that the spec is annotated with <<parallel_execution.adoc#isola
7171

7272
=== Misc
7373

74-
* Fix handling of @Verify and @VerifyAll spockIssue:2150[]
74+
* Fix handling of `@Verify` and `@VerifyAll` spockIssue:2150[]
7575
* Fix handling of condition method calls spockPull:2162[]
7676
* Fix vararg handling in SpyStatic spockIssue:2161[]
7777
* Fix incompatibility with JUnit 6 in OSGi environment spockIssue:2231[]

spock-core/src/main/java/org/spockframework/compiler/AstNodeCache.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public class AstNodeCache {
4848
public final ClassNode DataVariableMultiplication = ClassHelper.makeWithoutCaching(DataVariableMultiplication.class);
4949
public final ClassNode DataVariableMultiplicationFactor = ClassHelper.makeWithoutCaching(DataVariableMultiplicationFactor.class);
5050
public final ClassNode BlockInfo = ClassHelper.makeWithoutCaching(BlockInfo.class);
51-
public final ClassNode Verify = ClassHelper.makeWithoutCaching(Verify.class);
52-
public final ClassNode VerifyAll = ClassHelper.makeWithoutCaching(VerifyAll.class);
5351
public final ClassNode Void = ClassHelper.makeWithoutCaching(void.class);
5452

5553
public final MethodNode Specification_GetSpecificationContext =

spock-core/src/main/java/org/spockframework/compiler/DeepBlockRewriter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,17 @@ private boolean handleInteraction(InteractionRewriter rewriter, ExpressionStatem
184184
// would also want to enforce this for when-blocks, but unfortunately it's not that uncommon
185185
// for projects to have interactions in when-blocks (Gradle, Tapestry). Before we enforce this,
186186
// we should at least support multiple setup-blocks.
187-
if ((block instanceof ExpectBlock) || (block instanceof VerifyBlock)) {
187+
if (block instanceof ExpectBlock) {
188188
resources.getErrorReporter().error(stat, "Interactions are not allowed in '%s' blocks. " +
189189
"Put them before the '%s' block or into a 'then' block.", block.getName(), block.getName());
190190
return true;
191191
}
192192

193+
if (block instanceof VerifyBlock) {
194+
resources.getErrorReporter().error(stat, "Interactions are not allowed in '@Verify' and '@VerifyAll' methods.");
195+
return true;
196+
}
197+
193198
replaceVisitedStatementWith(interaction);
194199
interactionFound = true;
195200
return true;

spock-core/src/main/java/org/spockframework/compiler/SpecParser.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import org.codehaus.groovy.ast.*;
2525
import org.codehaus.groovy.ast.expr.ConstantExpression;
2626
import org.codehaus.groovy.ast.stmt.Statement;
27+
import spock.lang.Verify;
28+
import spock.lang.VerifyAll;
2729

30+
import static org.spockframework.compiler.AstUtil.hasAnnotation;
2831
import static org.spockframework.util.Identifiers.*;
2932

3033
/**
@@ -67,7 +70,7 @@ public void visitField(FieldNode gField) {
6770
if (gField.isStatic()) return;
6871

6972
Field field = new Field(spec, gField, fieldCount++);
70-
field.setShared(AstUtil.hasAnnotation(gField, Shared.class));
73+
field.setShared(hasAnnotation(gField, Shared.class));
7174
field.setOwner(owner);
7275
spec.getFields().add(field);
7376
}
@@ -181,26 +184,24 @@ private void buildFeatureMethod(MethodNode method) {
181184
}
182185

183186
private boolean isVerifyMethod(MethodNode method) {
184-
boolean hasVerifyAnnotation = !method.getAnnotations(nodeCache.Verify).isEmpty();
185-
boolean hasVerifyAllAnnotation = !method.getAnnotations(nodeCache.VerifyAll).isEmpty();
187+
boolean hasVerifyAnnotation = hasAnnotation(method, Verify.class);
188+
boolean hasVerifyAllAnnotation = hasAnnotation(method, VerifyAll.class);
186189
if (hasVerifyAnnotation && hasVerifyAllAnnotation) {
187-
errorReporter.error(method, "Method '%s' cannot be annotated with both @Verify and @VerifyAll", method.getName());
190+
errorReporter.error(method, "Method '%s' cannot be annotated with both @Verify and @VerifyAll.", method.getName());
188191
return false;
189192
}
190193
return hasVerifyAnnotation || hasVerifyAllAnnotation;
191194
}
192195

193196
private void buildVerifyMethod(MethodNode method) {
194-
if (!method.isVoidMethod() && !method.isDynamicReturnType()) {
195-
errorReporter.error("Verification helper method '%s' must have a void or dynamic return type.", method.getName());
196-
}
197+
VerifyMethod.verifyReturnType(method, errorReporter);
197198
method.setReturnType(nodeCache.Void);
198199

199200
Method verify;
200-
if (method.getAnnotations(nodeCache.Verify).isEmpty()) {
201-
verify = new VerifyAllMethod(spec, method);
202-
} else {
201+
if (hasAnnotation(method, Verify.class)) {
203202
verify = new VerifyMethod(spec, method);
203+
} else {
204+
verify = new VerifyAllMethod(spec, method);
204205
}
205206
spec.getMethods().add(verify);
206207
}

spock-core/src/main/java/org/spockframework/compiler/SpecialMethodCall.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class SpecialMethodCall implements ISpecialMethodCall {
4747
private final boolean conditionBlock;
4848
private final AstNodeCache nodeCache;
4949

50+
public SpecialMethodCall(String methodName, AstNodeCache nodeCache) {
51+
this(methodName, null, null, null, null, null, false, nodeCache);
52+
}
53+
5054
public SpecialMethodCall(String methodName, Expression inferredName, Expression inferredType,
5155
MethodCallExpression methodCallExpr, @Nullable BinaryExpression binaryExpr,
5256
@Nullable ClosureExpression closureExpr, boolean conditionBlock, AstNodeCache nodeCache) {

spock-core/src/main/java/org/spockframework/compiler/condition/BaseVerifyMethodTransform.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
import org.codehaus.groovy.transform.ASTTransformation;
2020
import org.spockframework.compiler.*;
2121
import org.spockframework.compiler.model.Method;
22+
import org.spockframework.compiler.model.VerifyMethod;
23+
import spock.lang.Verify;
24+
import spock.lang.VerifyAll;
25+
26+
import static org.spockframework.compiler.AstUtil.hasAnnotation;
2227

2328
abstract class BaseVerifyMethodTransform implements ASTTransformation {
2429

@@ -37,27 +42,25 @@ public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
3742
if (!(node instanceof MethodNode)) {
3843
continue;
3944
}
45+
MethodNode methodNode = (MethodNode) node;
4046

41-
if (((MethodNode) node).getDeclaringClass().isDerivedFrom(nodeCache.Specification)) {
47+
if (methodNode.getDeclaringClass().isDerivedFrom(nodeCache.Specification)) {
4248
// SpecRewriter already handles the method, so don't transform it again
4349
continue;
4450
}
4551

46-
boolean hasVerifyAnnotation = !((MethodNode) node).getAnnotations(nodeCache.Verify).isEmpty();
47-
boolean hasVerifyAllAnnotation = !((MethodNode) node).getAnnotations(nodeCache.VerifyAll).isEmpty();
48-
if (hasVerifyAnnotation && hasVerifyAllAnnotation) {
49-
errorReporter.error(node, "Method '%s' cannot be annotated with both @Verify and @VerifyAll", ((MethodNode) node).getName());
52+
if (hasAnnotation(node, Verify.class) && hasAnnotation(node, VerifyAll.class)) {
53+
errorReporter.error(node, "Method '%s' cannot be annotated with both @Verify and @VerifyAll.", methodNode.getName());
5054
}
5155

52-
processVerificationHelperMethod(createVerifyMethod((MethodNode) node), errorReporter, sourceLookup);
56+
processVerificationHelperMethod(createVerifyMethod(methodNode), errorReporter, sourceLookup);
5357
}
5458
}
5559
}
5660

5761
private void processVerificationHelperMethod(Method method, ErrorReporter errorReporter, SourceLookup sourceLookup) {
5862
MethodNode methodAst = method.getAst();
59-
if (!methodAst.isVoidMethod() && !methodAst.isDynamicReturnType()) {
60-
errorReporter.error("Verification helper method '%s' must have a void or dynamic return type.", method.getName());
63+
if (!VerifyMethod.verifyReturnType(methodAst, errorReporter)) {
6164
return;
6265
}
6366

spock-core/src/main/java/org/spockframework/compiler/condition/VerifyAllMethodRewriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void defineErrorCollector(boolean methodHasCondition, boolean methodHasDeepNonGr
3939

4040
@Override
4141
ISpecialMethodCall createSpecialMethodCall() {
42-
return new SpecialMethodCall(Identifiers.VERIFY_ALL, null, null, null,
43-
null, null, false, resources.getAstNodeCache());
42+
return new SpecialMethodCall(Identifiers.VERIFY_ALL, resources.getAstNodeCache());
4443
}
4544
}

spock-core/src/main/java/org/spockframework/compiler/condition/VerifyMethodRewriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void defineErrorCollector(boolean methodHasCondition, boolean methodHasDeepNonGr
3939

4040
@Override
4141
ISpecialMethodCall createSpecialMethodCall() {
42-
return new SpecialMethodCall(Identifiers.WITH, null, null, null,
43-
null, null, false, resources.getAstNodeCache());
42+
return new SpecialMethodCall(Identifiers.WITH, resources.getAstNodeCache());
4443
}
4544
}

spock-core/src/main/java/org/spockframework/compiler/model/VerifyMethod.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.codehaus.groovy.ast.MethodNode;
2020
import org.codehaus.groovy.ast.stmt.Statement;
2121
import org.spockframework.compiler.AstUtil;
22+
import org.spockframework.compiler.ErrorReporter;
2223

2324
import java.util.List;
2425

@@ -38,4 +39,12 @@ public VerifyMethod(Spec parent, MethodNode code) {
3839
stats.clear();
3940
addBlock(block);
4041
}
42+
43+
public static boolean verifyReturnType(MethodNode method, ErrorReporter errorReporter) {
44+
if (!method.isVoidMethod() && !method.isDynamicReturnType()) {
45+
errorReporter.error("Verification helper method '%s' must have a void or dynamic return type.", method.getName());
46+
return false;
47+
}
48+
return true;
49+
}
4150
}

spock-specs/src/test/groovy/org/spockframework/smoke/ast/condition/BaseVerifyMethodsAstSpec.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class SpecificAssertions extends BaseAssertions {
275275
snapshotter.assertThat(result.source).matchesSnapshot()
276276
}
277277

278-
def "fails when verification method has a non-void or -dynamic return value"() {
278+
def "fails when verification method has a non-void or non-dynamic return value"() {
279279
when:
280280
compiler.compile("""
281281
class Assertions {
@@ -292,7 +292,7 @@ class Assertions {
292292
e.message.contains("Verification helper method 'isPositiveWithReturn' must have a void or dynamic return type.")
293293
}
294294

295-
def "fails when verification method has a non-void or -dynamic return value in Spec"() {
295+
def "fails when verification method has a non-void or non-dynamic return value in Spec"() {
296296
when:
297297
compiler.compileSpecBody("""
298298
@${annotation.name}

0 commit comments

Comments
 (0)