Sha256: 1e6afb596939160385734defcd6cc109335d3c4dd032d949dde6bc90ce74af38

Contents?: true

Size: 1.56 KB

Versions: 21

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces correct order of expected and
      # actual arguments for `assert_equal`.
      #
      # @example
      #   # bad
      #   assert_equal foo, 2
      #   assert_equal foo, [1, 2]
      #   assert_equal foo, [1, 2], 'message'
      #
      #   # good
      #   assert_equal 2, foo
      #   assert_equal [1, 2], foo
      #   assert_equal [1, 2], foo, 'message'
      #
      class LiteralAsActualArgument < Base
        include ArgumentRangeHelper
        extend AutoCorrector

        MSG = 'Replace the literal with the first argument.'
        RESTRICT_ON_SEND = %i[assert_equal].freeze

        def on_send(node)
          return unless node.method?(:assert_equal)

          expected, actual, message = *node.arguments
          return unless actual&.recursive_basic_literal?
          return if expected.recursive_basic_literal?

          add_offense(all_arguments_range(node)) do |corrector|
            autocorrect(corrector, node, expected, actual, message)
          end
        end

        def autocorrect(corrector, node, expected, actual, message)
          new_actual_source = if actual.hash_type? && !actual.braces?
                                "{#{actual.source}}"
                              else
                                actual.source
                              end
          arguments = [new_actual_source, expected.source, message&.source].compact.join(', ')

          corrector.replace(node, "assert_equal(#{arguments})")
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.26.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.26.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.25.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.25.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.24.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.23.2 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.23.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.23.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.22.2 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.22.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.22.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.21.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.21.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.20.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb