Project

General

Profile

Actions

Feature #20922

open

Should not we omit parentheses in assert calls?

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

Status:
Open
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 days ago

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

assert_equal {}, something
Actions

Also available in: Atom PDF

Like0
Like0Like0