Sha256: d904ce76ebd895161e4b9a80cda22f3e2a98c6acacecb8246b0d667dd90438b4

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # This cop enforces the test to use `refute_nil` instead of using
      # `refute_equal(nil, something)`, `refute(something.nil?)`, or `refute_predicate(something, :nil?)`.
      #
      # @example
      #   # bad
      #   refute_equal(nil, actual)
      #   refute_equal(nil, actual, 'message')
      #   refute(actual.nil?)
      #   refute(actual.nil?, 'message')
      #   refute_predicate(object, :nil?)
      #   refute_predicate(object, :nil?, 'message')
      #
      #   # good
      #   refute_nil(actual)
      #   refute_nil(actual, 'message')
      #
      class RefuteNil < Base
        include ArgumentRangeHelper
        include NilAssertionHandleable
        extend AutoCorrector

        ASSERTION_TYPE = 'refute'
        RESTRICT_ON_SEND = %i[refute refute_equal refute_predicate].freeze

        def_node_matcher :nil_refutation, <<~PATTERN
          {
            (send nil? :refute_equal nil $_ $...)
            (send nil? :refute (send $_ :nil?) $...)
            (send nil? :refute_predicate $_ (sym :nil?) $...)
          }
        PATTERN

        def on_send(node)
          nil_refutation(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

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-minitest-0.19.1 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.19.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.18.0 lib/rubocop/cop/minitest/refute_nil.rb