Class: RuboCop::Cop::Minitest::AssertEmptyLiteral

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

Overview

Enforces the test to use ‘assert_empty` instead of using `assert_equal([], object)` or `assert_equal({}, object)`.

Examples:

# bad
assert_equal([], object)
assert_equal({}, object)

# good
assert_empty(object)

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/minitest/assert_empty_literal.rb', line 28

def on_send(node)
  assert_equal_with_empty_literal(node) do |literal, matchers|
    return unless literal.values.empty?

    args = matchers.map(&:source).join(', ')

    message = format(MSG, literal: literal.source, arguments: args)
    add_offense(node, message: message) do |corrector|
      object = matchers.first

      corrector.replace(node.loc.selector, 'assert_empty')
      corrector.replace(first_and_second_arguments_range(node), object.source)
    end
  end
end