Skip to content

Commit

Permalink
Fix an incorrect autocorrect for Minitest/RefuteMatch
Browse files Browse the repository at this point in the history
Follow up rubocop#181 and rubocop#184.

This PR fixes an incorrect autocorrect for `Minitest/AssertMatch`
when `refute` with `match` and RHS is a regexp literal.

A regular expression literal must be the first argument to `refute_match`.
`TypeError: no implicit conversion of Regexp into String` will occur if it is
passed as the second argument.

```ruby
refute_match(object, /regexp/) #=> TypeError: no implicit conversion of Regexp into String
```
  • Loading branch information
koic committed Sep 27, 2022
1 parent 15f7954 commit 40cbe03
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
@@ -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

0 comments on commit 40cbe03

Please sign in to comment.