/
identity_comparison_spec.rb
46 lines (38 loc) · 1.26 KB
/
identity_comparison_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::IdentityComparison, :config do
subject(:cop) { described_class.new(config) }
it 'registers an offense and corrects when using `==` for comparison between `object_id`s' do
expect_offense(<<~RUBY)
foo.object_id == bar.object_id
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `equal?` instead `==` when comparing `object_id`.
RUBY
expect_correction(<<~RUBY)
foo.equal?(bar)
RUBY
end
it 'does not register an offense when using `==` for comparison between `object_id` and other' do
expect_no_offenses(<<~RUBY)
foo.object_id == bar.do_something
RUBY
end
it 'does not register an offense when a receiver that is not `object_id` uses `==`' do
expect_no_offenses(<<~RUBY)
foo.do_something == bar.object_id
RUBY
end
it 'does not register an offense when using `==`' do
expect_no_offenses(<<~RUBY)
foo.equal(bar)
RUBY
end
it 'does not register an offense when lhs is `object_id` without receiver' do
expect_no_offenses(<<~RUBY)
object_id == bar.object_id
RUBY
end
it 'does not register an offense when rhs is `object_id` without receiver' do
expect_no_offenses(<<~RUBY)
foo.object_id == object_id
RUBY
end
end