Sha256: 55bba4ef76864de98524d6f6dbeb0ffbaad2dd20aaa54428c48775521897af80

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 `refute_nil` instead of using
      # `refute_equal(nil, something)` or `refute(something.nil?)`.
      #
      # @example
      #   # bad
      #   refute_equal(nil, actual)
      #   refute_equal(nil, actual, 'message')
      #   refute(actual.nil?)
      #   refute(actual.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_equal refute].freeze

        def_node_matcher :nil_refutation, <<~PATTERN
          {
            (send nil? :refute_equal nil $_ $...)
            (send nil? :refute (send $_ :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

7 entries across 7 versions & 1 rubygems

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