forked from rubocop/rubocop
/
hash_each_methods_spec.rb
93 lines (76 loc) · 2.63 KB
/
hash_each_methods_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::HashEachMethods do
subject(:cop) { described_class.new }
context 'when node matches a keys#each or values#each' do
context 'when receiver is a send' do
it 'registers offense, auto-corrects foo#keys.each to foo#each_key' do
expect_offense(<<~RUBY)
foo.keys.each { |k| p k }
^^^^^^^^^ Use `each_key` instead of `keys.each`.
RUBY
expect_correction(<<~RUBY)
foo.each_key { |k| p k }
RUBY
end
it 'registers offense, auto-corrects foo#values.each to foo#each_value' do
expect_offense(<<~RUBY)
foo.values.each { |v| p v }
^^^^^^^^^^^ Use `each_value` instead of `values.each`.
RUBY
expect_correction(<<~RUBY)
foo.each_value { |v| p v }
RUBY
end
it 'does not register an offense for foo#each_key' do
expect_no_offenses('foo.each_key { |k| p k }')
end
it 'does not register an offense for Hash#each_value' do
expect_no_offenses('foo.each_value { |v| p v }')
end
end
context 'when receiver is a hash literal' do
it 'registers offense, auto-corrects {}#keys.each with {}#each_key' do
expect_offense(<<~RUBY)
{}.keys.each { |k| p k }
^^^^^^^^^ Use `each_key` instead of `keys.each`.
RUBY
expect_correction(<<~RUBY)
{}.each_key { |k| p k }
RUBY
end
it 'registers offense, auto-corrects {}#values.each with {}#each_value' do
expect_offense(<<~RUBY)
{}.values.each { |k| p k }
^^^^^^^^^^^ Use `each_value` instead of `values.each`.
RUBY
expect_correction(<<~RUBY)
{}.each_value { |k| p k }
RUBY
end
it 'does not register an offense for {}#each_key' do
expect_no_offenses('{}.each_key { |k| p k }')
end
it 'does not register an offense for {}#each_value' do
expect_no_offenses('{}.each_value { |v| p v }')
end
end
context 'when receiver is implicit' do
it 'does not register an offense for `keys.each`' do
expect_no_offenses(<<~RUBY)
keys.each { |k| p k }
RUBY
end
it 'does not register an offense for `values.each`' do
expect_no_offenses(<<~RUBY)
values.each { |v| p v }
RUBY
end
it 'does not register an offense for each_key' do
expect_no_offenses('each_key { |k| p k }')
end
it 'does not register an offense for each_value' do
expect_no_offenses('each_value { |v| p v }')
end
end
end
end