Sha256: 1afceebe0f7a60c179fefb0baad49ac95b150af352024519b5071353cb91cea1

Contents?: true

Size: 1.72 KB

Versions: 10

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces the use of `assert_equal(expected, actual)`
      # over `assert(expected == actual)`.
      #
      # @example
      #   # bad
      #   assert("rubocop-minitest" == actual)
      #   assert_operator("rubocop-minitest", :==, actual)
      #
      #   # good
      #   assert_equal("rubocop-minitest", actual)
      #
      class AssertEqual < Base
        include ArgumentRangeHelper
        extend AutoCorrector

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

        def_node_matcher :assert_equal, <<~PATTERN
          {
            (send nil? :assert (send $_ :== $_) $...)
            (send nil? :assert_operator $_ (sym :==) $_ $...)
          }
        PATTERN

        # rubocop:disable Metrics/AbcSize
        def on_send(node)
          assert_equal(node) do |expected, actual, rest_args|
            basic_arguments = "#{expected.source}, #{actual.source}"
            preferred = (message_arg = rest_args.first) ? "#{basic_arguments}, #{message_arg.source}" : basic_arguments
            message = format(MSG, preferred: preferred)

            add_offense(node, message: message) do |corrector|
              corrector.replace(node.loc.selector, 'assert_equal')

              range = if node.method?(:assert)
                        node.first_argument
                      else
                        node.first_argument.source_range.begin.join(node.arguments[2].source_range.end)
                      end

              corrector.replace(range, basic_arguments)
            end
          end
        end
        # rubocop:enable Metrics/AbcSize
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

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