Class: RuboCop::Cop::Minitest::AssertTruthy

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

Overview

Enforces the test to use ‘assert(actual)` instead of using `assert_equal(true, actual)`.

Examples:

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

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/minitest/assert_truthy.rb', line 38

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

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

    add_offense(node, message: format(MSG, arguments: arguments)) do |corrector|
      corrector.replace(node.loc.selector, 'assert')
      corrector.replace(first_and_second_arguments_range(node), actual.source)
    end
  end
end