Skip to content

Commit

Permalink
[ISSUE-274] detect whether where: parameters are being executed and s…
Browse files Browse the repository at this point in the history
…o should be reference in the expect: block
  • Loading branch information
David Dawson committed Dec 28, 2013
1 parent ee265ea commit b52150b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@

package org.spockframework.compiler;

import java.util.List;

import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.expr.*;
import org.codehaus.groovy.ast.stmt.*;

import org.codehaus.groovy.ast.stmt.AssertStatement;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.syntax.Types;
import org.spockframework.compiler.model.*;
import org.spockframework.util.Nullable;

import java.util.Arrays;
import java.util.List;

/**
* Walks the statement and expression tree to:
* - rewrite explicit conditions,
Expand Down Expand Up @@ -89,13 +92,33 @@ public void visitDeclarationExpression(DeclarationExpression expr) {
protected void doVisitMethodCallExpression(final MethodCallExpression expr) {
super.doVisitMethodCallExpression(expr);

handleImplicitCallOnParam(expr);

boolean handled = handleMockCall(expr)
|| handleThrownCall(expr)
|| handleOldCall(expr)
|| handleInteractionBlockCall(expr)
|| forbidUseOfSuperInFixtureMethod(expr);
}

private void handleImplicitCallOnParam(final MethodCallExpression expr) {
String methodName = expr.getMethodAsString();

List<Parameter> params = Arrays.asList(resources.getCurrentMethod().getAst().getParameters());
boolean methodIsParam = false;

for(Parameter param : params) {
if (param.getName().equals(methodName)) {
methodIsParam = true;
}
}

if (methodIsParam) {
expr.setMethod(new ConstantExpression("call"));
expr.setObjectExpression(new VariableExpression(methodName));
}
}

private boolean handleInteraction(ExpressionStatement stat) {
InteractionRewriter rewriter = new InteractionRewriter(resources, getCurrentWithOrMockClosure());
ExpressionStatement interaction = rewriter.rewrite(stat);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.spockframework.runtime.condition
import org.spockframework.EmbeddedSpecification
import spock.lang.Issue

class ClosureAsDataValueCallSpec extends EmbeddedSpecification {

@Issue("http://issues.spockframework.org/detail?id=274")
def "multi line expression failss"() {
when:
runner.runSpecBody("""
def "multi line expression failing"() {
expect:
1 == wibble()
where:
wibble = { 1 }
}
""")

then:
notThrown(Exception)
}
}

0 comments on commit b52150b

Please sign in to comment.