forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_raises_with_regexp_argument.rb
36 lines (34 loc) · 1.13 KB
/
assert_raises_with_regexp_argument.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 `assert_raises` with arguments of regular expression literals.
# Arguments should be exception classes.
# Optionally the last argument can be a custom message string to help explain failures.
# Either way, it's not the argument that `exception.message` is compared to.
# The raised exception is returned and can be used
# to match against a regular expression.
#
# @example
#
# # bad
# assert_raises FooError, /some message/ do
# obj.occur_error
# end
#
# # good
# exception = assert_raises FooError do
# obj.occur_error
# end
# assert_match(/some message/, exception.message)
#
class AssertRaisesWithRegexpArgument < Base
MSG = 'Do not pass regular expression literals to `assert_raises`. Test the resulting exception.'
RESTRICT_ON_SEND = %i[assert_raises].freeze
def on_send(node)
add_offense(node) if node.last_argument&.regexp_type?
end
end
end
end
end