/
duplicate_rescue_exception_spec.rb
75 lines (68 loc) · 1.92 KB
/
duplicate_rescue_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
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::DuplicateRescueException do
subject(:cop) { described_class.new }
it 'registers an offense when duplicate exception exists' do
expect_offense(<<~RUBY)
begin
something
rescue FirstError
rescue SecondError, FirstError
^^^^^^^^^^ Duplicate `rescue` exception detected.
end
RUBY
end
it 'registers an offense when duplicate exception splat exists' do
expect_offense(<<~RUBY)
begin
something
rescue *ERRORS
rescue SecondError, *ERRORS
^^^^^^^ Duplicate `rescue` exception detected.
end
RUBY
end
it 'registers an offense when multiple duplicate exceptions exist' do
expect_offense(<<~RUBY)
begin
something
rescue FirstError
rescue SecondError
rescue FirstError
^^^^^^^^^^ Duplicate `rescue` exception detected.
rescue SecondError
^^^^^^^^^^^ Duplicate `rescue` exception detected.
end
RUBY
end
it 'registers an offense when duplicate exception exists within rescues with `else` branch' do
expect_offense(<<~RUBY)
begin
something
rescue FirstError
rescue SecondError, FirstError
^^^^^^^^^^ Duplicate `rescue` exception detected.
else
end
RUBY
end
it 'registers an offense when duplicate exception exists within rescues with empty `rescue` branch' do
expect_offense(<<~RUBY)
begin
something
rescue FirstError
rescue SecondError, FirstError
^^^^^^^^^^ Duplicate `rescue` exception detected.
rescue
end
RUBY
end
it 'does not register an offense when there are no duplicate exceptions' do
expect_no_offenses(<<~RUBY)
begin
something
rescue FirstError
rescue SecondError
end
RUBY
end
end