Sha256: 196a2662ebcc67598b91c1abd9db2c0489951c3ea12bd15881c648d460d51df4

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # This cop enforces the test to use `assert_nil`
      # instead of using `assert_equal(nil, something)`.
      #
      # @example
      #   # bad
      #   assert_equal(nil, actual)
      #   assert_equal(nil, actual, 'message')
      #
      #   # good
      #   assert_nil(actual)
      #   assert_nil(actual, 'message')
      #
      class AssertNil < Cop
        include ArgumentRangeHelper

        MSG = 'Prefer using `assert_nil(%<arguments>s)` over ' \
              '`assert_equal(nil, %<arguments>s)`.'
        RESTRICT_ON_SEND = %i[assert_equal].freeze

        def_node_matcher :assert_equal_with_nil, <<~PATTERN
          (send nil? :assert_equal nil $_ $...)
        PATTERN

        def on_send(node)
          assert_equal_with_nil(node) do |actual, message|
            message = message.first

            arguments = [actual.source, message&.source].compact.join(', ')

            add_offense(node, message: format(MSG, arguments: arguments))
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            assert_equal_with_nil(node) do |actual|
              corrector.replace(node.loc.selector, 'assert_nil')
              corrector.replace(
                first_and_second_arguments_range(node), actual.source
              )
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-minitest-0.11.1 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.11.0 lib/rubocop/cop/minitest/assert_nil.rb