forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unspecified_exception.rb
36 lines (31 loc) · 966 Bytes
/
unspecified_exception.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
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
# Checks for a specified error in `assert_raises`.
#
# @example
# # bad
# assert_raises { raise FooException }
# assert_raises('This should have raised') { raise FooException }
#
# # good
# assert_raises(FooException) { raise FooException }
# assert_raises(FooException, 'This should have raised') { raise FooException }
#
class UnspecifiedException < Base
MSG = 'Specify the exception being captured.'
def on_block(block_node)
node = block_node.send_node
return unless node.method?(:assert_raises)
add_offense(node) if unspecified_exception?(node)
end
private
def unspecified_exception?(node)
args = node.arguments
args.empty? || (args.size == 1 && args[0].str_type?)
end
end
end
end
end