Sha256: b8649b28c46a81958ec73fbf4c9c4fbf5646c449b7719dcf8150467bbec0c3ee

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces the use of `assert_operator(expected, :<, actual)` over `assert(expected < actual)`.
      #
      # @example
      #
      #   # bad
      #   assert(expected < actual)
      #
      #   # good
      #   assert_operator(expected, :<, actual)
      #
      class AssertOperator < Base
        extend AutoCorrector

        MSG = 'Prefer using `assert_operator(%<new_arguments>s)`.'
        RESTRICT_ON_SEND = %i[assert].freeze
        ALLOWED_OPERATORS = [:[]].freeze

        def on_send(node)
          first_argument = node.first_argument
          return unless first_argument.respond_to?(:binary_operation?) && first_argument.binary_operation?

          operator = first_argument.to_a[1]
          return if ALLOWED_OPERATORS.include?(operator)

          new_arguments = build_new_arguments(node)

          add_offense(node, message: format(MSG, new_arguments: new_arguments)) do |corrector|
            corrector.replace(node.loc.selector, 'assert_operator')

            corrector.replace(range_of_arguments(node), new_arguments)
          end
        end

        private

        def build_new_arguments(node)
          lhs, op, rhs = *node.first_argument
          new_arguments = +"#{lhs.source}, :#{op}, #{rhs.source}"

          if node.arguments.count == 2
            new_arguments << ", #{node.last_argument.source}"
          else
            new_arguments
          end
        end

        def range_of_arguments(node)
          node.first_argument.source_range.begin.join(node.last_argument.source_range.end)
        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/minitest/assert_operator.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/minitest/assert_operator.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/minitest/assert_operator.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/minitest/assert_operator.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/minitest/assert_operator.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/assert_operator.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/assert_operator.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/assert_operator.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/assert_operator.rb