Sha256: 0a22199c11b4d1b83aca949afc6a0533bbf4aef4108e5e3cc9c682dcebbdd433

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # This cop 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 < Cop
        include ArgumentRangeHelper

        MSG = 'Replace the literal with the first argument.'

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

          actual = node.arguments[1]
          return unless actual

          add_offense(node, location: all_arguments_range(node)) if actual.recursive_basic_literal?
        end

        def autocorrect(node)
          expected, actual, message = *node.arguments

          lambda do |corrector|
            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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-minitest-0.10.3 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.10.2 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.10.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.10.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb