-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
index_with_spec.rb
150 lines (127 loc) · 4.61 KB
/
index_with_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::IndexWith, :config do
context 'when using Rails 6.0 or newer', :rails60 do
context 'with an inline block' do
it 'registers an offense for `each_with_object`' do
expect_offense(<<~RUBY)
x.each_with_object({}) { |el, h| h[el] = foo(el) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `each_with_object`.
RUBY
expect_correction(<<~RUBY)
x.index_with { |el| foo(el) }
RUBY
end
end
context 'with a multiline block' do
it 'registers an offense for `each_with_object`' do
expect_offense(<<~RUBY)
x.each_with_object({}) do |el, memo|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `each_with_object`.
memo[el] = el.to_sym
end
RUBY
expect_correction(<<~RUBY)
x.index_with do |el|
el.to_sym
end
RUBY
end
end
context 'with the safe navigation operator' do
it 'registers an offense for `each_with_object`' do
expect_offense(<<~RUBY)
x&.each_with_object({}) { |el, h| h[el] = foo(el) }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `each_with_object`.
RUBY
expect_correction(<<~RUBY)
x&.index_with { |el| foo(el) }
RUBY
end
end
context 'when keys are transformed' do
it 'does not register an offense for `each_with_object`' do
expect_no_offenses(<<~RUBY)
x.each_with_object({}) { |el, h| h[el.to_sym] = foo(el) }
RUBY
end
end
context 'when values are not transformed' do
it 'does not register an offense for `each_with_object`' do
expect_no_offenses('x.each_with_object({}) { |el, h| h[el] = el }')
end
end
context 'when the given hash is not used' do
it 'does not register an offense for `each_with_object`' do
expect_no_offenses(<<~RUBY)
x.each_with_object({}) { |el, h| other_h[el] = el.to_sym }
RUBY
end
end
context 'when the given hash is used in the value' do
it 'does not register an offense for `each_with_object`' do
expect_no_offenses(<<~RUBY)
x.each_with_object({}) { |el, h| h[el] = h.count }
RUBY
end
end
context 'when `to_h` is given a block' do
it 'registers an offense for `map { ... }.to_h`' do
expect_offense(<<~RUBY)
x.map { |el| [el, el.to_sym] }.to_h { |k, v| [v, k] }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `map { ... }.to_h`.
RUBY
expect_correction(<<~RUBY)
x.index_with { |el| el.to_sym }.to_h { |k, v| [v, k] }
RUBY
end
end
context 'when `to_h` is not given a block' do
it 'registers an offense for `map { ... }.to_h`' do
expect_offense(<<~RUBY)
x.map { |el| [el, el.to_sym] }.to_h
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `map { ... }.to_h`.
RUBY
expect_correction(<<~RUBY)
x.index_with { |el| el.to_sym }
RUBY
end
end
context 'when to_h is not called on the result' do
it 'does not register an offense for `map { ... }.to_h`' do
expect_no_offenses('x.map { |el| [el, el.to_sym] }')
end
end
it 'registers an offense for `Hash[map { ... }]`' do
expect_offense(<<~RUBY)
Hash[x.map { |el| [el, el.to_sym] }]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `Hash[map { ... }]`.
RUBY
expect_correction(<<~RUBY)
x.index_with { |el| el.to_sym }
RUBY
end
it 'registers an offense for `to_h { ... }`' do
expect_offense(<<~RUBY)
x.to_h { |el| [el, el.to_sym] }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `index_with` over `to_h { ... }`.
RUBY
expect_correction(<<~RUBY)
x.index_with { |el| el.to_sym }
RUBY
end
end
context 'when using Rails 5.2 or older', :rails52 do
it 'does not register an offense for `each_with_object`' do
expect_no_offenses('x.each_with_object({}) { |el, h| h[el] = foo(el) }')
end
it 'does not register an offense for `to_h { ... }`' do
expect_no_offenses('x.to_h { |el| [el, el.to_sym] }')
end
it 'does not register an offense for `map { ... }.to_h`' do
expect_no_offenses('x.map { |el| [el, el.to_sym] }.to_h')
end
it 'does not register an offense for `Hash[map { ... }]`' do
expect_no_offenses('Hash[x.map { |el| [el, el.to_sym] }]')
end
end
end