hakandilek / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

Special case type constraint to just _ (underscore) #376

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
`as Foo` has a special meaning in an argument constraint, namely “argument of 
type Foo". The common way to use it is `_ as Set` (“any argument of type 
Foo”). Perhaps we should limit the special meaning to `_`, as I don't see 
much value in mixing a type constraint with equality or closure constraints. 
(from pniederw@gmail.com)

Original issue reported on code.google.com by quidr...@gmail.com on 13 Oct 2014 at 4:26

GoogleCodeExporter commented 9 years ago
I'm trying to match a Set as an argument, e.g.

    class Container {
        def call(Set<String> args) {
            println args
        }
    }

    def 'simple argument constraint'() {
        setup:
        def container = Mock(Container)

        when:
        container.call(['val1'] as Set)

        then:
        1 * container.call(['val1'] as Set)
    }

But it results in a failure:

Too few invocations for:

1 * container.call(['val1'] as Set)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * container.call(['val1'])

A little investigation shows that the constraint in TypeArgumentConstraint has 
the type correct as Set, but the constraint is an EqualArgumentConstraint with 
a List in it. I've tried some variations, like parentheses, but Spock is just 
too smart. I can assign the argument to a variable first or use the closure 
syntax for the argument, but I'd like to avoid doing it and keep it simple like 
above. Any suggestions?

Original comment by quidr...@gmail.com on 13 Oct 2014 at 4:27

GoogleCodeExporter commented 9 years ago
A good workaround is to use `['val1'].toSet()`, rather than `['val1'] as Set`.

Original comment by pnied...@gmail.com on 3 Mar 2015 at 2:56