diff --git a/changelog/fix_an_incorrect_autocorrect_for_minitest_refute_match.md b/changelog/fix_an_incorrect_autocorrect_for_minitest_refute_match.md new file mode 100644 index 00000000..10e76e22 --- /dev/null +++ b/changelog/fix_an_incorrect_autocorrect_for_minitest_refute_match.md @@ -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][]) diff --git a/lib/rubocop/cop/minitest/refute_match.rb b/lib/rubocop/cop/minitest/refute_match.rb index 797c192c..9fa165d9 100644 --- a/lib/rubocop/cop/minitest/refute_match.rb +++ b/lib/rubocop/cop/minitest/refute_match.rb @@ -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 diff --git a/test/rubocop/cop/minitest/refute_match_test.rb b/test/rubocop/cop/minitest/refute_match_test.rb index 8e8799ed..a6178a6e 100644 --- a/test/rubocop/cop/minitest/refute_match_test.rb +++ b/test/rubocop/cop/minitest/refute_match_test.rb @@ -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 @@ -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