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

Add cop to enfore refute_empty over refute(actual.empty?) #20

Merged
merged 1 commit into from
Oct 7, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New features

* [#15](https://github.com/rubocop-hq/rubocop-minitest/pull/15): Add new `Minitest/RefuteIncludes` cop. ([@abhaynikam][])
* [#20](https://github.com/rubocop-hq/rubocop-minitest/pull/20): Add new `Minitest/RefuteEmpty` cop. ([@abhaynikam][])

### Bug fixes

Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Minitest/RefuteNil:
Enabled: true
VersionAdded: '0.2'

Minitest/RefuteEmpty:
Description: 'This cop enforces to use `refute_empty` instead of using `refute(object.empty?)`.'
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-empty'
Enabled: true
VersionAdded: '0.3'

Minitest/RefuteIncludes:
Description: 'This cop enforces the test to use `refute_includes` instead of using `refute(collection.include?(object))`.'
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-includes'
Expand Down
51 changes: 51 additions & 0 deletions lib/rubocop/cop/minitest/refute_empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# This cop enforces to use `refute_empty` instead of
# using `refute(object.empty?)`.
#
# @example
# # bad
# refute(object.empty?)
# refute(object.empty?, 'the message')
#
# # good
# refute_empty(object)
# refute_empty(object, 'the message')
#
class RefuteEmpty < Cop
MSG = 'Prefer using `refute_empty(%<arguments>s)` over ' \
'`refute(%<receiver>s)`.'

def_node_matcher :refute_with_empty, <<~PATTERN
(send nil? :refute $(send $_ :empty?) $...)
PATTERN

def on_send(node)
refute_with_empty(node) do |first_receiver_arg, object, rest_receiver_arg|
message = rest_receiver_arg.first

arguments = [object.source, message&.source].compact.join(', ')
receiver = [first_receiver_arg.source, message&.source].compact.join(', ')

offense_message = format(MSG, arguments: arguments, receiver: receiver)
add_offense(node, message: offense_message)
end
end

def autocorrect(node)
lambda do |corrector|
refute_with_empty(node) do |_first_receiver_arg, object, rest_receiver_arg|
message = rest_receiver_arg.first

replacement = [object.source, message&.source].compact.join(', ')
corrector.replace(node.loc.expression, "refute_empty(#{replacement})")
end
end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/minitest_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
require_relative 'minitest/assert_nil'
require_relative 'minitest/assert_includes'
require_relative 'minitest/assert_truthy'
require_relative 'minitest/refute_empty'
require_relative 'minitest/refute_nil'
require_relative 'minitest/refute_includes'
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [Minitest/AssertIncludes](cops_minitest.md#minitestassertincludes)
* [Minitest/AssertNil](cops_minitest.md#minitestassertnil)
* [Minitest/AssertTruthy](cops_minitest.md#minitestasserttruthy)
* [Minitest/RefuteEmpty](cops_minitest.md#minitestrefuteempty)
* [Minitest/RefuteIncludes](cops_minitest.md#minitestrefuteincludes)
* [Minitest/RefuteNil](cops_minitest.md#minitestrefutenil)

Expand Down
25 changes: 25 additions & 0 deletions manual/cops_minitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ assert(actual, 'the message')

* [https://github.com/rubocop-hq/minitest-style-guide#assert-truthy](https://github.com/rubocop-hq/minitest-style-guide#assert-truthy)

## Minitest/RefuteEmpty

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.3 | -

This cop enforces to use `refute_empty` instead of
using `refute(object.empty?)`.

### Examples

```ruby
# bad
refute(object.empty?)
refute(object.empty?, 'the message')

# good
refute_empty(object)
refute_empty(object, 'the message')
```

### References

* [https://github.com/rubocop-hq/minitest-style-guide#refute-empty](https://github.com/rubocop-hq/minitest-style-guide#refute-empty)

## Minitest/RefuteIncludes

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
Expand Down
57 changes: 57 additions & 0 deletions test/rubocop/cop/minitest/refute_empty_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'test_helper'

class RefuteEmptyTest < Minitest::Test
def setup
@cop = RuboCop::Cop::Minitest::RefuteEmpty.new
end

def test_registers_offense_when_using_refute_with_empty
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute(somestuff.empty?)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff)` over `refute(somestuff.empty?)`.
end
end
RUBY

assert_correction(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute_empty(somestuff)
end
end
RUBY
end

def test_registers_offense_when_using_refute_with_empty_and_message
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute(somestuff.empty?, 'the message')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff, 'the message')` over `refute(somestuff.empty?, 'the message')`.
end
end
RUBY

assert_correction(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute_empty(somestuff, 'the message')
end
end
RUBY
end

def refute_empty_method
assert_no_offenses(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute_empty(somestuff)
end
end
RUBY
end
end