/
redundant_exception_spec.rb
62 lines (50 loc) · 1.88 KB
/
redundant_exception_spec.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
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::RedundantException do
subject(:cop) { described_class.new }
shared_examples 'common behavior' do |keyword|
it "reports an offense for a #{keyword} with RuntimeError" do
expect_offense(<<~RUBY, keyword: keyword)
%{keyword} RuntimeError, msg
^{keyword}^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError` argument can be removed.
RUBY
expect_correction(<<~RUBY)
#{keyword} msg
RUBY
end
it "reports an offense for a #{keyword} with RuntimeError and ()" do
expect_offense(<<~RUBY, keyword: keyword)
%{keyword}(RuntimeError, msg)
^{keyword}^^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError` argument can be removed.
RUBY
expect_correction(<<~RUBY)
#{keyword}(msg)
RUBY
end
it "reports an offense for a #{keyword} with RuntimeError.new" do
expect_offense(<<~RUBY, keyword: keyword)
%{keyword} RuntimeError.new msg
^{keyword}^^^^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError.new` call can be replaced with just the message.
RUBY
expect_correction(<<~RUBY)
#{keyword} msg
RUBY
end
it "reports an offense for a #{keyword} with RuntimeError.new" do
expect_offense(<<~RUBY, keyword: keyword)
%{keyword} RuntimeError.new(msg)
^{keyword}^^^^^^^^^^^^^^^^^^^^^^ Redundant `RuntimeError.new` call can be replaced with just the message.
RUBY
expect_correction(<<~RUBY)
#{keyword} msg
RUBY
end
it "accepts a #{keyword} with RuntimeError if it does not have 2 args" do
expect_no_offenses("#{keyword} RuntimeError, msg, caller")
end
it 'accepts rescue w/ non redundant error' do
expect_no_offenses "#{keyword} OtherError, msg"
end
end
include_examples 'common behavior', 'raise'
include_examples 'common behavior', 'fail'
end