Spock: Unable to use closure as first parameter in parameterized tests

Created on 29 Jan 2021  路  2Comments  路  Source: spockframework/spock

Issue description

Using closure as the first parameter in a parameterized feature/test generates:

/home/.../ClosureInWhereBug.groovy:11:24
Groovyc: Header of data table may only contain variable names

How to reproduce

class ClosureInWhereBug extends Specification {

    def "should allow for closure as first parameter in parameterized tests"() {
        expect:
            closure() == 1
        where:
            closure || description
            { 1 }   || "case 1"
    }
}

Closure as the second parameter works, so it's just a minor issue. Nevertheless, maybe it could be fixed without much effort.

Additional Environment information

Java/JDK

openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment 20.9 (build 15.0.1+9)
OpenJDK 64-Bit Server VM 20.9 (build 15.0.1+9, mixed mode, sharing)

Groovy version

3.0.4

Build tool version

Occurs in both Gradle and Idea compilation.

bug won't fix

Most helpful comment

This is not a bug, but a syntax limitation by Groovy.
The Spock magic depends on the getting from your example two expression statements,closure || description and { 1 } || "case 1".
But the Groovy parser sees the "closure" not as stand-alone closure but as argument to the method description, so what it sees is one expression statement closure || description({ 1 }) || "case 1" and there is not much we can do about that.

What you as test author can do is using syntax tricks, like adding a dummy column

_ | closure || description
_ | { 1 }   || "case 1"

or adding a "right border" like

closure || description ;
{ 1 }   || "case 1"    ;

All 2 comments

This is not a bug, but a syntax limitation by Groovy.
The Spock magic depends on the getting from your example two expression statements,closure || description and { 1 } || "case 1".
But the Groovy parser sees the "closure" not as stand-alone closure but as argument to the method description, so what it sees is one expression statement closure || description({ 1 }) || "case 1" and there is not much we can do about that.

What you as test author can do is using syntax tricks, like adding a dummy column

_ | closure || description
_ | { 1 }   || "case 1"

or adding a "right border" like

closure || description ;
{ 1 }   || "case 1"    ;

Thanks @Vampire - I like the syntax with a "right border" :). Closing as a limitation of Groovy syntax.

Was this page helpful?
0 / 5 - 0 ratings