/
redundant_safe_navigation_spec.rb
54 lines (45 loc) · 1.36 KB
/
redundant_safe_navigation_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::RedundantSafeNavigation, :config do
let(:cop_config) do
{ 'IgnoredMethods' => [] }
end
it 'registers an offense and corrects when `.&` is used for `nil` method' do
expect_offense(<<~RUBY)
foo&.respond_to?(:bar)
^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected.
RUBY
expect_correction(<<~RUBY)
foo.respond_to?(:bar)
RUBY
end
it 'registers an offense and corrects when multiple `.&` are used for `nil` methods' do
expect_offense(<<~RUBY)
foo.do_something&.dup&.inspect
^^^^^ Redundant safe navigation detected.
^^^^^^^^^ Redundant safe navigation detected.
RUBY
expect_correction(<<~RUBY)
foo.do_something.dup.inspect
RUBY
end
it 'does not register an offense when using non-nil method with `.&`' do
expect_no_offenses(<<~RUBY)
foo.&do_something
RUBY
end
it 'does not register an offense when using `nil` method without `.&`' do
expect_no_offenses(<<~RUBY)
foo.dup
RUBY
end
context 'when AllowedMethods is set' do
let(:cop_config) do
{ 'IgnoredMethods' => ['to_f'] }
end
it 'does not register an offense when using ignored `nil` method with `.&`' do
expect_no_offenses(<<~RUBY)
foo&.to_f
RUBY
end
end
end