Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-274] Closure used as data value in where-block can't be called with method syntax #31

Merged
merged 1 commit into from
Dec 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}