Sha256: d4b577b5f87444700805781620c31e5604b39fcb7f804fb2e77a7fbfe4da69fe

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # This cop enforces the use of `refute(object)`
      # over `assert_equal(false, object)`.
      #
      # @example
      #   # bad
      #   assert_equal(false, actual)
      #   assert_equal(false, actual, 'the message')
      #
      #   # good
      #   refute(actual)
      #   refute(actual, 'the message')
      #
      class RefuteFalse < Cop
        MSG = 'Prefer using `refute(%<arguments>s)` over ' \
              '`assert_equal(false, %<arguments>s)`.'

        def_node_matcher :assert_equal_with_false, <<~PATTERN
          (send nil? :assert_equal false $_ $...)
        PATTERN

        def on_send(node)
          assert_equal_with_false(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))
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            arguments = node.arguments.reject(&:false_type?)
            replacement = arguments.map(&:source).join(', ')
            corrector.replace(node.loc.expression, "refute(#{replacement})")
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-minitest-0.5.0 lib/rubocop/cop/minitest/refute_false.rb
rubocop-minitest-0.4.1 lib/rubocop/cop/minitest/refute_false.rb
rubocop-minitest-0.4.0 lib/rubocop/cop/minitest/refute_false.rb
rubocop-minitest-0.3.0 lib/rubocop/cop/minitest/refute_false.rb