spockframework / spockframework/spock

Computing return value with typed closure parameter does not properly work for Object... methods

Open
#1,041 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
3.6k
Forks
483
PR merge metrics
No merged PRs in 30d

Description

I stub a method that takes Object... as parameter, which is called addEventListener.
I wanted to perform some side-effects on the parameter, so I wanted to do this with a typed return value closure as usual like

1 * jda.addEventListener(_) >> { Object[] listener ->
    listener.each {
        it.doSideEffect()
    }
}

Unfortunately this does not work as I'm used to.
When the parameter is untyped I get

  • the listeners
  • in an object array (as given to the method)
  • in an array list

With the Object[] typed parameter I suddenly get one level more instead of less

  • the listeners
  • in an object array (as given to the method)
  • in an array list
  • in an object array

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the addEventListener stub with the typed Object[] closure parameter shown in the issue, then compare it with the untyped closure behavior. Trace how typed closure arguments are computed for Object... methods; done means the typed form receives the expected listener values without the extra Object[] nesting.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, java
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.