Sha256: 6c26a33f61929e5035d931a354e43d47b29da238158c1cad9d6843d8f2322087

Contents?: true

Size: 1.54 KB

Versions: 16

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Common functionality for `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` cops.
      # @api private
      module InstanceOfAssertionHandleable
        include ArgumentRangeHelper

        MSG = 'Prefer using `%<prefer>s`.'

        private

        def investigate(node, assertion_type)
          return unless (first_capture, second_capture, message = instance_of_assertion?(node))

          required_arguments = build_required_arguments(node, assertion_type, first_capture, second_capture)
          full_arguments = [required_arguments, message.first&.source].compact.join(', ')
          prefer = "#{assertion_type}_instance_of(#{full_arguments})"

          add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
            range = replacement_range(node, assertion_type)

            corrector.replace(node.loc.selector, "#{assertion_type}_instance_of")
            corrector.replace(range, required_arguments)
          end
        end

        def build_required_arguments(node, method_name, first_capture, second_capture)
          if node.method?(method_name)
            [second_capture, first_capture]
          else
            [first_capture, second_capture]
          end.map(&:source).join(', ')
        end

        def replacement_range(node, method_name)
          if node.method?(method_name)
            node.first_argument
          else
            first_and_second_arguments_range(node)
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb