Sha256: 66e2e38a592a07f25473c0196e87f18e15361b14044a98b885ddf3a2463fff10

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 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)` or `assert(something.nil?)`.
      #
      # @example
      #   # bad
      #   assert_equal(nil, actual)
      #   assert_equal(nil, actual, 'message')
      #   assert(object.nil?)
      #   assert(object.nil?, 'message')
      #
      #   # good
      #   assert_nil(actual)
      #   assert_nil(actual, 'message')
      #
      class AssertNil < Base
        include ArgumentRangeHelper
        include NilAssertionHandleable
        extend AutoCorrector

        ASSERTION_TYPE = 'assert'
        RESTRICT_ON_SEND = %i[assert_equal assert].freeze

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

        def on_send(node)
          nil_assertion(node) do |actual, message|
            register_offense(node, actual, message)
          end
        end

        private

        def assertion_type
          ASSERTION_TYPE
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-minitest-0.17.2 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.17.1 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.17.0 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.16.0 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.15.2 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.15.1 lib/rubocop/cop/minitest/assert_nil.rb
rubocop-minitest-0.15.0 lib/rubocop/cop/minitest/assert_nil.rb