Sha256: 17f4a81c56f36ea20f8a3e1b4f72b4c3b6a5597c09b879b54618ce3485bec77c

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 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, 'message')
      #
      #   assert(!test)
      #   assert(!test, 'message')
      #
      #   # good
      #   refute(actual)
      #   refute(actual, 'message')
      #
      class RefuteFalse < Base
        include ArgumentRangeHelper
        extend AutoCorrector

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

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

        def_node_matcher :assert_with_bang_argument, <<~PATTERN
          (send nil? :assert (send $_ :!) $...)
        PATTERN

        def on_send(node)
          actual, rest_receiver_arg = assert_equal_with_false(node) || assert_with_bang_argument(node)
          return unless actual

          message_argument = rest_receiver_arg.first

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

          message = node.method?(:assert_equal) ? MSG_FOR_ASSERT_EQUAL : MSG_FOR_ASSERT

          add_offense(node, message: format(message, arguments: arguments)) do |corrector|
            autocorrect(corrector, node, actual)
          end
        end

        private

        def autocorrect(corrector, node, actual)
          corrector.replace(node.loc.selector, 'refute')

          if node.method?(:assert_equal)
            corrector.replace(first_and_second_arguments_range(node), actual.source)
          else
            corrector.replace(first_argument_range(node), actual.source)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-minitest-0.19.1 lib/rubocop/cop/minitest/refute_false.rb
rubocop-minitest-0.19.0 lib/rubocop/cop/minitest/refute_false.rb
rubocop-minitest-0.18.0 lib/rubocop/cop/minitest/refute_false.rb