Skip to content

Commit

Permalink
Merge pull request #151 from koic/fix_false_positive_for_assert_empty…
Browse files Browse the repository at this point in the history
…_and_refute_empty

[Fix #150] Fix a false positive for `Minitest/AssertEmpty` and `RefuteEmpty`
  • Loading branch information
koic authored Nov 5, 2021
2 parents 92c731a + 424d4bb commit 8bb4f29
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug fixes

* [#142](https://github.com/rubocop/rubocop-minitest/issues/142): Fix `Minitest/GlobalExpectations` autocorrect when receiver is lambda. ([@gi][])
* [#150](https://github.com/rubocop/rubocop-minitest/issues/150): Fix a false positive for `Minitest/AssertEmpty` and `RefuteEmpty` cops when using `empty` method with any arguments. ([@koic][])

## 0.15.2 (2021-10-11)

Expand Down
11 changes: 11 additions & 0 deletions lib/rubocop/cop/minitest/assert_empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ class AssertEmpty < Base
extend MinitestCopRule

define_rule :assert, target_method: :empty?

def on_send(node)
return unless node.method?(:assert)
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:empty?)
return unless arguments.first.arguments.empty?

add_offense(node, message: offense_message(arguments)) do |corrector|
autocorrect(corrector, node, arguments)
end
end
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/rubocop/cop/minitest/refute_empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ class RefuteEmpty < Base
extend MinitestCopRule

define_rule :refute, target_method: :empty?

def on_send(node)
return unless node.method?(:refute)
return unless (arguments = peel_redundant_parentheses_from(node.arguments))
return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:empty?)
return unless arguments.first.arguments.empty?

add_offense(node, message: offense_message(arguments)) do |corrector|
autocorrect(corrector, node, arguments)
end
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/rubocop/cop/minitest/assert_empty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ def test_do_something
end
RUBY
end

def test_does_not_register_offense_when_using_assert_empty_method_with_any_arguments
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
assert(File.empty?(path))
end
end
RUBY
end
end
10 changes: 10 additions & 0 deletions test/rubocop/cop/minitest/refute_empty_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ def test_do_something
end
RUBY
end

def test_does_not_register_offense_when_using_refute_empty_method_with_any_arguments
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
def test_do_something
refute(File.empty?(path))
end
end
RUBY
end
end

0 comments on commit 8bb4f29

Please sign in to comment.