forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_raises_compound_body.rb
45 lines (40 loc) · 1.01 KB
/
assert_raises_compound_body.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
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Enforces the block body of `assert_raises { ... }` to be reduced to only the raising code.
#
# @example
# # bad
# assert_raises(MyError) do
# foo
# bar
# end
#
# # good
# assert_raises(MyError) do
# foo
# end
#
# # good
# assert_raises(MyError) do
# foo do
# bar
# baz
# end
# end
#
class AssertRaisesCompoundBody < Base
MSG = 'Reduce `assert_raises` block body to contain only the raising code.'
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
return unless node.method?(:assert_raises) && multi_statement_begin?(node.body)
add_offense(node)
end
private
def multi_statement_begin?(node)
node.begin_type? && node.children.size > 1
end
end
end
end
end