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
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.
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)
3.0.4
Occurs in both Gradle and Idea compilation.
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.
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 || descriptionand{ 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 statementclosure || 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
or adding a "right border" like