Project

General

Profile

Actions

Feature #20922

closed

Should not we omit parentheses in assert calls?

Added by mame (Yusuke Endoh) 5 months ago. Updated 5 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:120050]

Description

I often see the style of omitting parentheses in assert calls, but it leads to annoying warnings in the following case:

assert_equal -1, val    #=> warning: ambiguous first argument; put parentheses or a space even after `-` operator
assert_match /foo/, str #=> warning: ambiguous `/`; wrap regexp in parentheses or add a space after `/` operator

To deal with this warning, it is necessary to add parentheses only to some assert calls.

assert_equal 1, one
assert_equal 0, zero
assert_equal(-1, minus_one)

Obviously, this is not very cool.

I feel that by these warnings, Ruby says "we should not omit parentheses in assert calls". Is this what matz intended?

If it is the intent, I would like to add parentheses on all assert calls in tests of Ruby and default gems.
If it is not the intent, why don't we remove these warnings?

Note that, as far as I recall, I encountered this problem only with assert_equal and assert_match.
I don't write parentheses in p, puts, attr_reader, include, etc., but all of them rarely accept -1 and // literally as the first argument in real code.
As a milder approach, I came up with an idea to stop the warning only when the method name starts with "assert". It is very ad-hoc, though.

Updated by byroot (Jean Boussier) 5 months ago

I generally agree, and this change would be welcome, but there's still a common case that is an error:

assert_equal {}, something

Updated by mame (Yusuke Endoh) 5 months ago

  • Status changed from Open to Rejected

Discussed at the dev meeting.

@matz (Yukihiro Matsumoto) said he was not going to remove this warning.

Several ideas to prevent a warning were raised.

Parenthesizing only the first argument prevents the warning. This solution works today, and usable for {} as well.

assert_equal (-1), minus_one
assert_match (/foo/), str

Assigning the first argument to a varaible also works.

re = /foo/
assert_match re, str

A new idea is to prevent a warning when there are two spaces, but not accepted.

assert_equal  -1, minus_one

Finally, writing the method call parentheses only where necessary

assert_equal 1, one
assert_equal 0, zero
assert_equal(-1, minus_one)

is the most reasonable solution. :sad:

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0