+= and -= no longer rejected in assertion statement in Groovy 3

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

+= and -= no longer rejected in assertion statement in Groovy 3

szpak
Hi,

Playing with the failures which Groovy 3 brought to Spock tests itself,
I noticed that Groovy 3 no longer rejects += and -= if used in an
assertion statement:

> def a = 1
> assert a += 1 //passes
> assert a == 2 //passes

While it behaves consistently, I wonder if that change it was purposely
(it was failing in Groovy 2) or the check should be enhanced:
https://github.com/apache/groovy/commit/a17b9eb85835a5eaa306a893a0a8bcd51a49e299#diff-05fe8708ce3207739a2cb39a97426e8dR345
?

Marcin

--
https://blog.solidsoft.info/ - Working code is not enough
Reply | Threaded
Open this post in threaded view
|

Re: += and -= no longer rejected in assertion statement in Groovy 3

Daniel.Sun
Hi Marcin,

    Thanks for your reporting the issue. Would you mind to submit a JIRA
ticket to track it?

Cheers,
Daniel.Sun



-----
Apache Groovy committer & PMC member
Blog: http://blog.sunlan.me 
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

Re: += and -= no longer rejected in assertion statement in Groovy 3

szpak
On 2020-01-08 14:41, Daniel.Sun wrote:
> Hi Marcin,
>
>     Thanks for your reporting the issue. Would you mind to submit a JIRA
> ticket to track it?

Done: https://issues.apache.org/jira/browse/GROOVY-9360

Marcin

--
https://blog.solidsoft.info/ - Working code is not enough
Reply | Threaded
Open this post in threaded view
|

Re: += and -= no longer rejected in assertion statement in Groovy 3

sbglasius
In reply to this post by szpak
Hi Marcin,

I think it makes sense, that it does not fail, as a +=1 returns 2 which by Groovys asBoolean is true. I am more inclined to say that Groovy 2.x had a bug, that was accepted as a compile error. 

Just my two cents worth.

Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


Den ons. 8. jan. 2020 kl. 12.16 skrev Marcin Zajączkowski <[hidden email]>:
Hi,

Playing with the failures which Groovy 3 brought to Spock tests itself,
I noticed that Groovy 3 no longer rejects += and -= if used in an
assertion statement:

> def a = 1
> assert a += 1 //passes
> assert a == 2 //passes

While it behaves consistently, I wonder if that change it was purposely
(it was failing in Groovy 2) or the check should be enhanced:
https://github.com/apache/groovy/commit/a17b9eb85835a5eaa306a893a0a8bcd51a49e299#diff-05fe8708ce3207739a2cb39a97426e8dR345
?

Marcin

--
https://blog.solidsoft.info/ - Working code is not enough
MG
Reply | Threaded
Open this post in threaded view
|

Re: += and -= no longer rejected in assertion statement in Groovy 3

MG
Hmmm - to me an assertion should not have a side effect, so Groovy 2.x rejecting code like that, where the side effect is easily detected, seems proper to me...
(The fact that Groovy has a way of interpreting the non-Boolean result as Boolean through the Groovy-truth rules you mention is of course necessary to allow this statement to compile; that does however not necessarily make it sufficient)

Cheers,
mg

On 08/01/2020 22:06, Søren Berg Glasius wrote:
Hi Marcin,

I think it makes sense, that it does not fail, as a +=1 returns 2 which by Groovys asBoolean is true. I am more inclined to say that Groovy 2.x had a bug, that was accepted as a compile error. 

Just my two cents worth.

Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


Den ons. 8. jan. 2020 kl. 12.16 skrev Marcin Zajączkowski <[hidden email]>:
Hi,

Playing with the failures which Groovy 3 brought to Spock tests itself,
I noticed that Groovy 3 no longer rejects += and -= if used in an
assertion statement:

> def a = 1
> assert a += 1 //passes
> assert a == 2 //passes

While it behaves consistently, I wonder if that change it was purposely
(it was failing in Groovy 2) or the check should be enhanced:
https://github.com/apache/groovy/commit/a17b9eb85835a5eaa306a893a0a8bcd51a49e299#diff-05fe8708ce3207739a2cb39a97426e8dR345
?

Marcin

--
https://blog.solidsoft.info/ - Working code is not enough

Reply | Threaded
Open this post in threaded view
|

Re: += and -= no longer rejected in assertion statement in Groovy 3

Daniel.Sun
In Java, `int a = 0; assert a+=1` will fail to compile because expecting
boolean value, but `int a = 0; assert 0 == (a+=1)` is OK, so I think we
forbidden assignments seems not correct...

Cheers,
Daniel.Sun



-----
Apache Groovy committer & PMC member
Blog: http://blog.sunlan.me 
Twitter: @daniel_sun

--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

Re: += and -= no longer rejected in assertion statement in Groovy 3

Jochen Theodorou


Am 12.01.20 um 09:09 schrieb Daniel.Sun:
> In Java, `int a = 0; assert a+=1` will fail to compile because expecting
> boolean value, but `int a = 0; assert 0 == (a+=1)` is OK, so I think we
> forbidden assignments seems not correct...

a long time ago we decided to no allow assignments in certain places
where a boolean is to be expected. That is the after the assert and in
the boolean part of the if. As soon as it gets down to a nested
expression we stop, but top-level we want to forbid it.  Meaning
"if (a+=1)" and "assert a=1" should fail

bye Jochen