Sha256: 3e56fc131f14a1d5b1e929ba636d0d27316b3b2cbe90d2bbf7573c68fd3446d3

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # This cop enforces the test to use `assert_empty`
      # instead of using `assert_equal([], object)` or `assert_equal({}, object)`.
      #
      # @example
      #   # bad
      #   assert_equal([], object)
      #   assert_equal({}, object)
      #
      #   # good
      #   assert_empty(object)
      #
      class AssertEmptyLiteral < Base
        include ArgumentRangeHelper
        extend AutoCorrector

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

        def_node_matcher :assert_equal_with_empty_literal, <<~PATTERN
          (send nil? :assert_equal ${hash array} $...)
        PATTERN

        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
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-minitest-0.17.2 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.17.1 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.17.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.16.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.15.2 lib/rubocop/cop/minitest/assert_empty_literal.rb