groovy assert statement behaves like a method and not like a statement

If I use an assertion for a correct groovy statement and I leave out the parenthesis then 'assert' throws a MultipleCompilationErrorsException with an 'unexpected token' message.

assert (and at least if) seems to behave like a method instead of a statement, since nested function calls don't accept leaving out parenthesis.

So when I have a correct example and I use it as an assertion I get an error. I expect assert to accept correct groovy statements if they are correct without an assert.

I did not find this problem described in Stackoverflow, Groovy JIRA or via internet search.

assert null == println('foo')

or

println 'foo'

both output foo (as expected), but

assert null == println 'foo'

throws

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script_from_command_line: 1: unexpected token: foo @ line 1, column 24.
   assert null == println 'foo'
                          ^

1 error

Upvotes: 0

Views: 644

Answers (1)

Bhanuchander Udhayakumar
Bhanuchander Udhayakumar

Reputation: 1621

FYI : assert is also a keyword like return, import and etc. See the groovy doc.

As we know we can't ommit the paranthesis after the keyword such as return. See the document omitting_parentheses. Unfortunately it only discuss about the method not mentioning any keywords :( .

return test("String")  // will work

return test "String" // won't

I hope you got the point.

Upvotes: 1

Related Questions