Forgus / spock

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

cannot use assignment operator in and or then block #311

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is the error eclipse is displaying to me when I try to use the = operator 
in either an "and" or a "then" block

Groovy:Expected a condition, but found an assignment. Did you intend to write 
'==' ? @ line 58, column 13.

What steps will reproduce the problem? If possible, provide source code and
stack trace.

def "test and/then assignment"() {
    when:
        def x = 3
    then:
        x == 3
        x = 4
    and:
        x == 4
        x = 5
    and:
        x == 5

What version of Spock and Groovy are you using?
spock 0.7, groovy 2.0.7

Please provide any additional information below. You can also assign
labels.

I can work around by doing something like this, so why not just allow it?

def updateX(x, y) {
    x = y
}

def "test and/then assignment"() {
    when:
        def x = 3
    then:
        x == 3
        updateX x, 4
    and:
        x == 4
        updateX x, 5
    and:
        x == 5

Original issue reported on code.google.com by ccoo...@co3sys.com on 25 Apr 2013 at 9:34

GoogleCodeExporter commented 8 years ago
Writing `=` instead of `==` in a then-block is a common source of error. Also, 
a then-block is not supposed to contain arbitrary code. That's why assignments 
are not allowed. Note that you can have multiple when-then's.

Original comment by pnied...@gmail.com on 26 Apr 2013 at 1:04

GoogleCodeExporter commented 8 years ago
(Also, variable declarations are allowed in then-blocks.)

Original comment by pnied...@gmail.com on 26 Apr 2013 at 1:05