Skip to content

Commit

Permalink
feat: support "match" assertions in RSpec/Rails/MinitestAssertions
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jan 19, 2024
1 parent 288061b commit 3934a54
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

- Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
- 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])
- Support correcting `assert_not_equal` and `assert_not_nil` in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
Expand Down
23 changes: 23 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,7 @@ class MinitestAssertions < Base
assert_not_instance_of
assert_includes
assert_not_includes
assert_match
assert_nil
assert_not_nil
assert_empty
Expand All @@ -43,6 +44,7 @@ class MinitestAssertions < Base
refute_includes
refute_nil
refute_empty
refute_match
].freeze

# @!method minitest_equal(node)
Expand All @@ -60,6 +62,11 @@ class MinitestAssertions < Base
(send nil? {:assert_includes :assert_not_includes :refute_includes} $_ $_ $_?)
PATTERN

# @!method minitest_match(node)
def_node_matcher :minitest_match, <<~PATTERN
(send nil? {:assert_match :refute_match} $_ $_ $_?)
PATTERN

# @!method minitest_nil(node)
def_node_matcher :minitest_nil, <<~PATTERN
(send nil? {:assert_nil :assert_not_nil :refute_nil} $_ $_?)
Expand All @@ -86,6 +93,11 @@ def on_send(node) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
failure_message.first))
end

minitest_match(node) do |matcher, actual, failure_message|
on_assertion(node, MatchAssertion.new(matcher, actual,
failure_message.first))
end

minitest_nil(node) do |actual, failure_message|
on_assertion(node, NilAssertion.new(nil, actual,
failure_message.first))
Expand Down Expand Up @@ -159,6 +171,17 @@ def assertion
end
end

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

def assertion
"match(#{@expected})"
end
end

# :nodoc:
class NilAssertion < BasicAssertion
def negated?(node)
Expand Down
73 changes: 73 additions & 0 deletions spec/rubocop/cop/rspec/rails/minitest_assertions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,79 @@
end
end

context 'with match assertions' do
it 'registers an offense when using `assert_match`' do
expect_offense(<<~RUBY)
assert_match(/xyz/, b)
^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to match(/xyz/)`.
RUBY

expect_correction(<<~RUBY)
expect(b).to match(/xyz/)
RUBY
end

it 'registers an offense when using `assert_match` with no parentheses' do
expect_offense(<<~RUBY)
assert_match /xyz/, b
^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to match(/xyz/)`.
RUBY

expect_correction(<<~RUBY)
expect(b).to match(/xyz/)
RUBY
end

it 'registers an offense when using `assert_match` with failure message' do
expect_offense(<<~RUBY)
assert_match /xyz/, b, "must match"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to(match(/xyz/), "must match")`.
RUBY

expect_correction(<<~RUBY)
expect(b).to(match(/xyz/), "must match")
RUBY
end

it 'registers an offense when using `assert_match` with ' \
'multi-line arguments' do
expect_offense(<<~RUBY)
assert_match(/xyz/,
^^^^^^^^^^^^^^^^^^^ Use `expect(b).to(match(/xyz/), "must match")`.
b,
"must match")
RUBY

expect_correction(<<~RUBY)
expect(b).to(match(/xyz/), "must match")
RUBY
end

it 'registers an offense when using `refute_match`' do
expect_offense(<<~RUBY)
refute_match /xyz/, b
^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).not_to match(/xyz/)`.
RUBY

expect_correction(<<~RUBY)
expect(b).not_to match(/xyz/)
RUBY
end

it 'does not register an offense when using `expect(b).to match(/xyz/)`' do
expect_no_offenses(<<~RUBY)
expect(b).to match(/xyz/)
RUBY
end

it 'does not register an offense when ' \
'using `expect(b).not_to match(/xyz/)`' do
expect_no_offenses(<<~RUBY)
expect(b).not_to match(/xyz/)
RUBY
end
end

context 'with nil assertions' do
it 'registers an offense when using `assert_nil`' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit 3934a54

Please sign in to comment.