Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support some "predicate" assertions in RSpec/Rails/MinitestAssertions #1789

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Support asserts with messages in `Rspec/BeEmpty`. ([@G-Rath])
- Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
- Support correcting some `*_predicate` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `*_match` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `*_instance_of` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
- Support correcting `*_includes` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
Expand Down
26 changes: 26 additions & 0 deletions lib/rubocop/cop/rspec/rails/minitest_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class MinitestAssertions < Base
assert_not_instance_of
assert_includes
assert_not_includes
assert_predicate
assert_not_predicate
assert_match
assert_nil
assert_not_nil
Expand All @@ -42,6 +44,7 @@ class MinitestAssertions < Base
refute_equal
refute_instance_of
refute_includes
refute_predicate
refute_nil
refute_empty
refute_match
Expand All @@ -62,6 +65,11 @@ class MinitestAssertions < Base
(send nil? {:assert_includes :assert_not_includes :refute_includes} $_ $_ $_?)
PATTERN

# @!method minitest_predicate(node)
def_node_matcher :minitest_predicate, <<~PATTERN
(send nil? {:assert_predicate :assert_not_predicate :refute_predicate} $_ ${sym} $_?)
PATTERN

# @!method minitest_match(node)
def_node_matcher :minitest_match, <<~PATTERN
(send nil? {:assert_match :refute_match} $_ $_ $_?)
Expand All @@ -88,6 +96,13 @@ def on_send(node) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
failure_message.first))
end

minitest_predicate(node) do |subject, predicate, failure_message|
next unless predicate.value.end_with?('?')

on_assertion(node, PredicateAssertion.new(predicate, subject,
failure_message.first))
end

minitest_includes(node) do |collection, expected, failure_message|
on_assertion(node, IncludesAssertion.new(expected, collection,
failure_message.first))
Expand Down Expand Up @@ -171,6 +186,17 @@ def assertion
end
end

# :nodoc:
class PredicateAssertion < BasicAssertion
def negated?(node)
!node.method?(:assert_predicate)
end

def assertion
"be_#{@expected.delete_prefix(':').delete_suffix('?')}"
end
end

# :nodoc:
class MatchAssertion < BasicAssertion
def negated?(node)
Expand Down
130 changes: 130 additions & 0 deletions spec/rubocop/cop/rspec/rails/minitest_assertions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,134 @@
RUBY
end
end

context 'with predicate assertions' do
it 'registers an offense when using `assert_predicate` with ' \
'an actual predicate' do
expect_offense(<<~RUBY)
assert_predicate(a, :valid?)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to be_valid`.
RUBY

expect_correction(<<~RUBY)
expect(a).to be_valid
RUBY
end

it 'registers an offense when using `assert_predicate` with ' \
'an actual predicate and no parentheses' do
expect_offense(<<~RUBY)
assert_predicate a, :valid?
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to be_valid`.
RUBY

expect_correction(<<~RUBY)
expect(a).to be_valid
RUBY
end

it 'registers an offense when using `assert_predicate` with ' \
'an actual predicate and a failure message' do
expect_offense(<<~RUBY)
assert_predicate a, :valid?, "must be valid"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be_valid, "must be valid")`.
RUBY

expect_correction(<<~RUBY)
expect(a).to(be_valid, "must be valid")
RUBY
end

it 'registers an offense when using `assert_predicate` with ' \
'an actual predicate and multi-line arguments' do
expect_offense(<<~RUBY)
assert_predicate(a,
^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(be_valid, "must be valid")`.
:valid?,
"must be valid")
RUBY

expect_correction(<<~RUBY)
expect(a).to(be_valid, "must be valid")
RUBY
end

it 'registers an offense when using `assert_not_predicate` with ' \
'an actual predicate' do
expect_offense(<<~RUBY)
assert_not_predicate a, :valid?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).not_to be_valid`.
RUBY

expect_correction(<<~RUBY)
expect(a).not_to be_valid
RUBY
end

it 'registers an offense when using `refute_predicate` with ' \
'an actual predicate' do
expect_offense(<<~RUBY)
refute_predicate a, :valid?
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).not_to be_valid`.
RUBY

expect_correction(<<~RUBY)
expect(a).not_to be_valid
RUBY
end

it 'does not register an offense when using `expect(a).to be_predicate`' do
expect_no_offenses(<<~RUBY)
expect(a).to be_predicate
RUBY
end

it 'does not register an offense when using ' \
'`expect(a).not_to be_predicate`' do
expect_no_offenses(<<~RUBY)
expect(a).not_to be_predicate
RUBY
end

it 'does not register an offense when using `assert_predicate` with ' \
'not a predicate' do
expect_no_offenses(<<~RUBY)
assert_predicate foo, :do_something
RUBY
end

it 'does not register an offense when using `assert_not_predicate` with ' \
'not a predicate' do
expect_no_offenses(<<~RUBY)
assert_not_predicate foo, :do_something
RUBY
end

it 'does not register an offense when using `refute_predicate` with ' \
'not a predicate' do
expect_no_offenses(<<~RUBY)
refute_predicate foo, :do_something
RUBY
end

it 'does not register an offense when the predicate is not a symbol' do
expect_no_offenses(<<~RUBY)
assert_predicate a, 1
RUBY
end

it 'does not register an offense when the predicate is missing' do
expect_no_offenses(<<~RUBY)
assert_predicate a, "whoops, we forgot about the actual predicate!"
RUBY
end

it 'does not register an offense when the predicate is a variable' do
expect_no_offenses(<<~RUBY)
foo = :foo?

assert_predicate a, foo
RUBY
end
end
end