Sha256: 02f53acfb28ee02d11715b2ed5bbcd8241e1a058328f59f5067a3d3e71c16213

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 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)
          end
        end

        private

        def autocorrect(corrector, node, expected, actual)
          new_actual_source = if actual.hash_type? && !actual.braces?
                                "{#{actual.source}}"
                              else
                                actual.source
                              end

          corrector.replace(expected, new_actual_source)
          corrector.replace(actual, expected.source)

          wrap_with_parentheses(corrector, node) if !node.parenthesized? && actual.hash_type?
        end

        def wrap_with_parentheses(corrector, node)
          range = node.loc.selector.end.join(node.first_argument.source_range.begin)

          corrector.replace(range, '(')
          corrector.insert_after(node, ')')
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/literal_as_actual_argument.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/minitest/literal_as_actual_argument.rb