/
hash_compare_by_identity_spec.rb
33 lines (29 loc) · 1.31 KB
/
hash_compare_by_identity_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::HashCompareByIdentity do
subject(:cop) { described_class.new }
it 'registers an offense when using hash methods with `object_id` on receiver as a key' do
expect_offense(<<~RUBY)
hash.key?(foo.object_id)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys.
hash.has_key?(foo.object_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys.
hash[foo.object_id] = bar
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys.
hash[foo.object_id]
^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys.
hash.fetch(foo.object_id, 42)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys.
RUBY
end
it 'registers an offense when using hash method with `object_id` as a key' do
expect_offense(<<~RUBY)
hash.key?(object_id)
^^^^^^^^^^^^^^^^^^^^ Use `Hash#compare_by_identity` instead of using `object_id` for keys.
RUBY
end
it 'does not register an offense for hash methods without `object_id` as key' do
expect_no_offenses(<<~RUBY)
hash.key?(foo)
RUBY
end
end