Sha256: 419958a9ee3aad96001a9ea6da4542491a9f6d5e2f51a554329caf94a74bcddf
Contents?: true
Size: 1.37 KB
Versions: 4
Compression:
Stored size: 1.37 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, 'the message') # # # good # assert_nil(actual) # assert_nil(actual, 'the 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
4 entries across 4 versions & 1 rubygems