Sha256: 98479124ab4155aa0f304dff64d33e83257b906f841c80e4bf89a66ca3021c0d

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Common functionality for `Minitest/AssertPredicate` and `Minitest/RefutePredicate` cops.
      # @api private
      module PredicateAssertionHandleable
        MSG = 'Prefer using `%<assertion_type>s_predicate(%<new_arguments>s)`.'

        def on_send(node)
          return unless node.first_argument
          return if node.first_argument.block_type? || node.first_argument.numblock_type?
          return unless predicate_method?(node.first_argument)
          return unless node.first_argument.arguments.count.zero?

          add_offense(node, message: offense_message(node.arguments)) do |corrector|
            autocorrect(corrector, node, node.arguments)
          end
        end

        def autocorrect(corrector, node, arguments)
          corrector.replace(node.loc.selector, "#{assertion_type}_predicate")

          new_arguments = new_arguments(arguments).join(', ')

          corrector.replace(node.first_argument, new_arguments)
        end

        private

        def predicate_method?(first_argument)
          first_argument.respond_to?(:predicate_method?) && first_argument.predicate_method?
        end

        def offense_message(arguments)
          message_argument = arguments.last if arguments.first != arguments.last

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

          format(MSG, assertion_type: assertion_type, new_arguments: new_arguments)
        end

        def new_arguments(arguments)
          receiver = correct_receiver(arguments.first.receiver)
          method_name = arguments.first.method_name

          [receiver, ":#{method_name}"]
        end

        def correct_receiver(receiver)
          receiver ? receiver.source : 'self'
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/mixin/predicate_assertion_handleable.rb