Sha256: 9c50df1d7048e21b595883f83e7d83cffea4227a2eb433a328433fdf835b36c0

Contents?: true

Size: 1.31 KB

Versions: 31

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # 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)`.'
        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

31 entries across 31 versions & 1 rubygems

Version Path
rubocop-minitest-0.34.5 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.26.1 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.26.0 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.25.1 lib/rubocop/cop/minitest/assert_empty_literal.rb
rubocop-minitest-0.25.0 lib/rubocop/cop/minitest/assert_empty_literal.rb