forked from rubocop/rubocop-minitest
/
assert_silent.rb
47 lines (39 loc) · 1.1 KB
/
assert_silent.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
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# This cop enforces the test to use `assert_silent { ... }`
# instead of using `assert_output('', '') { ... }`.
#
# @example
# # bad
# assert_output('', '') { puts object.do_something }
#
# # good
# assert_silent { puts object.do_something }
#
class AssertSilent < Base
extend AutoCorrector
MSG = 'Prefer using `assert_silent` over `assert_output("", "")`.'
def_node_matcher :assert_silent_candidate?, <<~PATTERN
(block
(send nil? :assert_output
#empty_string?
#empty_string?)
...)
PATTERN
def on_block(node)
return unless assert_silent_candidate?(node)
send_node = node.send_node
add_offense(send_node) do |corrector|
corrector.replace(send_node, 'assert_silent')
end
end
private
def empty_string?(node)
node.str_type? && node.value.empty?
end
end
end
end
end