forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_nil.rb
52 lines (46 loc) · 1.38 KB
/
assert_nil.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Enforces the test to use `assert_nil` instead of using
# `assert_equal(nil, something)`, `assert(something.nil?)`, or `assert_predicate(something, :nil?)`.
#
# @example
# # bad
# assert_equal(nil, actual)
# assert_equal(nil, actual, 'message')
# assert(object.nil?)
# assert(object.nil?, 'message')
# assert_predicate(object, :nil?)
# assert_predicate(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 assert_equal assert_predicate].freeze
def_node_matcher :nil_assertion, <<~PATTERN
{
(send nil? :assert_equal nil $_ $...)
(send nil? :assert (send $_ :nil?) $...)
(send nil? :assert_predicate $_ (sym :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