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

Unable to use closure as first parameter in parameterized tests #1269

Closed
szpak opened this issue Jan 29, 2021 · 2 comments
Closed

Unable to use closure as first parameter in parameterized tests #1269

szpak opened this issue Jan 29, 2021 · 2 comments

Comments

@szpak
Copy link
Member

szpak commented Jan 29, 2021

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.

@szpak szpak added the bug label Jan 29, 2021
@Vampire
Copy link
Member

Vampire commented Jan 29, 2021

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"    ;

@szpak
Copy link
Member Author

szpak commented Jan 29, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants