Sha256: 124ab14984039ac497cd0b9028f417342dce51da88df943fc642e4915c972689

Contents?: true

Size: 1.38 KB

Versions: 34

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # 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

34 entries across 34 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/refute_nil.rb
rubocop-minitest-0.26.1 lib/rubocop/cop/minitest/refute_nil.rb