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

Fix an incorrect autocorrect for Minitest/RefuteMatch #185

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#185](https://github.com/rubocop/rubocop-minitest/pull/185): Fix an incorrect autocorrect for `Minitest/RefuteMatch` when `refute` with `match` and RHS is a regexp literal. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/minitest/refute_match.rb
Expand Up @@ -18,7 +18,7 @@ module Minitest
class RefuteMatch < Base
extend MinitestCopRule

define_rule :refute, target_method: :match
define_rule :refute, target_method: :match, inverse: 'regexp_type?'
end
end
end
Expand Down
58 changes: 58 additions & 0 deletions test/rubocop/cop/minitest/refute_match_test.rb
Expand Up @@ -22,6 +22,44 @@ def test_do_something
RUBY
end

def test_registers_offense_when_using_refute_with_match_and_lhs_is_regexp_literal
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute(/regexp/.match(object))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_match(/regexp/, object)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute_match(/regexp/, object)
end
end
RUBY
end

def test_registers_offense_when_using_refute_with_match_and_rhs_is_regexp_literal
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute(object.match(/regexp/))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_match(/regexp/, object)`.
end
end
RUBY

assert_correction(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute_match(/regexp/, object)
end
end
RUBY
end

def test_registers_offense_when_using_refute_with_match_and_message
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
Expand Down Expand Up @@ -94,4 +132,24 @@ def test_do_something
end
RUBY
end

def test_does_not_register_offense_when_using_refute_with_no_arguments_match_call
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute(matcher.match)
end
end
RUBY
end

def test_does_not_register_offense_when_using_refute_with_no_arguments_match_safe_navigation_call
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute(matcher&.match)
end
end
RUBY
end
end