Class: RuboCop::Cop::Minitest::RefuteFalse

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ArgumentRangeHelper
Defined in:
lib/rubocop/cop/minitest/refute_false.rb

Overview

Enforces the use of ‘refute(object)` over `assert_equal(false, object)`.

Examples:

# bad
assert_equal(false, actual)
assert_equal(false, actual, 'message')

assert(!test)
assert(!test, 'message')

# good
refute(actual)
refute(actual, 'message')

Constant Summary collapse

MSG_FOR_ASSERT_EQUAL =
'Prefer using `refute(%<arguments>s)`.'
MSG_FOR_ASSERT =
'Prefer using `refute(%<arguments>s)`.'
RESTRICT_ON_SEND =
%i[assert_equal assert].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/minitest/refute_false.rb', line 46

def on_send(node)
  actual, rest_receiver_arg = assert_equal_with_false(node) || assert_with_bang_argument(node)
  return unless actual

  message_argument = rest_receiver_arg.first

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

  message = node.method?(:assert_equal) ? MSG_FOR_ASSERT_EQUAL : MSG_FOR_ASSERT

  add_offense(node, message: format(message, arguments: arguments)) do |corrector|
    autocorrect(corrector, node, actual)
  end
end