Sha256: c4a567d4080290fa5981f10eb7980e40f67f29830589b31f82eacfdfa44f3b4b

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 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)`.'

        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

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-minitest-0.10.3 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.10.2 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.10.1 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.10.0 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.9.0 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.8.1 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.8.0 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.7.0 lib/rubocop/cop/minitest/assert_nil.rb