Skip to content

Commit

Permalink
Merge pull request #11 from tejasbubane/refute-nil-cop
Browse files Browse the repository at this point in the history
Add new cop `Minitest/RefuteNil`
  • Loading branch information
koic committed Sep 6, 2019
2 parents bea0298 + 7331315 commit 04e9a82
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master (unreleased)

### New features

* [#11](https://github.com/rubocop-hq/rubocop-minitest/pull/11): Add new `Minitest/RefuteNil` cop. ([@tejasbubane ][])

## 0.1.0 (2019-09-01)

### New features
Expand All @@ -9,3 +13,4 @@

[@koic]: https://github.com/koic
[@duduribeiro]: https://github.com/duduribeiro
[@tejasbubane]: https://github.com/tejasbubane
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ Minitest/AssertNil:
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-nil'
Enabled: true
VersionAdded: '0.1'

Minitest/RefuteNil:
Description: 'Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.'
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-nil'
Enabled: true
VersionAdded: '0.1.1'
45 changes: 45 additions & 0 deletions lib/rubocop/cop/minitest/refute_nil.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.
#
# @example
# # bad
# refute_equal(nil, actual)
# refute_equal(nil, actual, 'the message')
#
# # good
# refute_nil(actual)
# refute_nil(actual, 'the message')
#
class RefuteNil < Cop
MSG = 'Prefer using `refute_nil(%<arguments>s)` over ' \
'`refute_equal(nil, %<arguments>s)`.'

def_node_matcher :refute_equal_with_nil, <<~PATTERN
(send nil? :refute_equal nil $_ $...)
PATTERN

def on_send(node)
refute_equal_with_nil(node) do |actual, message|
message = message.first

arguments = [actual.source, message&.source].compact.join(', ')

add_offense(node, message: format(MSG, arguments: arguments))
end
end

def autocorrect(node)
lambda do |corrector|
arguments = node.arguments.reject(&:nil_type?)
replacement = arguments.map(&:source).join(', ')
corrector.replace(node.loc.expression, "refute_nil(#{replacement})")
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
@@ -1,3 +1,4 @@
# frozen_string_literal: true

require_relative 'minitest/assert_nil'
require_relative 'minitest/refute_nil'
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#### Department [Minitest](cops_minitest.md)

* [Minitest/AssertNil](cops_minitest.md#minitestassertnil)
* [Minitest/RefuteNil](cops_minitest.md#minitestrefutenil)

<!-- END_COP_LIST -->
24 changes: 24 additions & 0 deletions manual/cops_minitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,27 @@ assert_nil(actual, 'the message')
### References

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

## Minitest/RefuteNil

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

Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.

### Examples

```ruby
# bad
refute_equal(nil, actual)
refute_equal(nil, actual, 'the message')

# good
refute_nil(actual)
refute_nil(actual, 'the message')
```

### References

* [https://github.com/rubocop-hq/minitest-style-guide#refute-nil](https://github.com/rubocop-hq/minitest-style-guide#refute-nil)
120 changes: 120 additions & 0 deletions test/rubocop/cop/minitest/refute_nil_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# frozen_string_literal: true

require 'test_helper'

class RefuteNilTest < Minitest::Test
def setup
@cop = RuboCop::Cop::Minitest::RefuteNil.new
end

def test_adds_offense_for_use_of_refute_equal_nil
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute_equal(nil, somestuff)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_nil(somestuff)` over `refute_equal(nil, somestuff)`.
end
end
RUBY

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

def test_adds_offense_for_use_of_refute_equal_nil_with_message
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute_equal(nil, somestuff, 'the message')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_nil(somestuff, 'the message')` over `refute_equal(nil, somestuff, 'the message')`.
end
end
RUBY

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

def test_adds_offense_for_use_of_refute_equal_with_a_method_call
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute_equal(nil, obj.do_something, 'the message')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_nil(obj.do_something, 'the message')` over `refute_equal(nil, obj.do_something, 'the message')`.
end
end
RUBY

assert_correction(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
refute_nil(obj.do_something, 'the message')
end
end
RUBY
end

def test_adds_offense_for_use_of_refute_equal_with_a_string_variable_in_message
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
message = 'the message'
refute_equal(nil, somestuff, message)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_nil(somestuff, message)` over `refute_equal(nil, somestuff, message)`.
end
end
RUBY

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

def test_adds_offense_for_use_of_refute_equal_with_a_constant_in_message
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
MESSAGE = 'the message'
def test_do_something
refute_equal(nil, somestuff, MESSAGE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_nil(somestuff, MESSAGE)` over `refute_equal(nil, somestuff, MESSAGE)`.
end
end
RUBY

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

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

0 comments on commit 04e9a82

Please sign in to comment.