/
redundant_merge_spec.rb
276 lines (245 loc) · 7.4 KB
/
redundant_merge_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::RedundantMerge, :config do
let(:cop_config) do
{ 'MaxKeyValuePairs' => 2 }
end
it 'autocorrects hash.merge!(a: 1)' do
new_source = autocorrect_source('hash.merge!(a: 1)')
expect(new_source).to eq 'hash[:a] = 1'
end
it 'autocorrects hash.merge!("abc" => "value")' do
new_source = autocorrect_source('hash.merge!("abc" => "value")')
expect(new_source).to eq 'hash["abc"] = "value"'
end
context 'when receiver is a local variable' do
it 'autocorrects hash.merge!(a: 1, b: 2)' do
new_source = autocorrect_source(<<~RUBY)
hash = {}
hash.merge!(a: 1, b: 2)
RUBY
expect(new_source).to eq(<<~RUBY)
hash = {}
hash[:a] = 1
hash[:b] = 2
RUBY
end
end
context 'when receiver is a method call' do
it "doesn't autocorrect hash.merge!(a: 1, b: 2)" do
new_source = autocorrect_source('hash.merge!(a: 1, b: 2)')
expect(new_source).to eq('hash.merge!(a: 1, b: 2)')
end
end
# FIXME: `undefined method `[]' for nil` occurs Prism 0.24.0. It has been resolved in
# the development line. This will be resolved in Prism > 0.24.0 and higher releases.
context 'when receiver is implicit', broken_on: :prism do
it "doesn't autocorrect" do
new_source = autocorrect_source('merge!(foo: 1, bar: 2)')
expect(new_source).to eq('merge!(foo: 1, bar: 2)')
end
end
context 'when any argument is a double splat' do
it 'does not register an offense when the only argument is adouble splat' do
expect_no_offenses(<<~RUBY)
foo.merge!(**bar)
RUBY
end
it 'does not register an offense when there are multiple arguments and at least one is a double splat' do
expect_no_offenses(<<~RUBY)
foo.merge!(baz: qux, **bar)
RUBY
end
end
context 'when internal to each_with_object' do
it 'autocorrects when the receiver is the object being built' do
source = <<~RUBY
foo.each_with_object({}) do |f, hash|
hash.merge!(a: 1, b: 2)
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
foo.each_with_object({}) do |f, hash|
hash[:a] = 1
hash[:b] = 2
end
RUBY
end
it 'autocorrects when the receiver is the object being built when merge! is the last statement' do
source = <<~RUBY
foo.each_with_object({}) do |f, hash|
some_method
hash.merge!(a: 1, b: 2)
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
foo.each_with_object({}) do |f, hash|
some_method
hash[:a] = 1
hash[:b] = 2
end
RUBY
end
it 'autocorrects when the receiver is the object being built when merge! is not the last statement' do
source = <<~RUBY
foo.each_with_object({}) do |f, hash|
hash.merge!(a: 1, b: 2)
why_are_you_doing_this?
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
foo.each_with_object({}) do |f, hash|
hash[:a] = 1
hash[:b] = 2
why_are_you_doing_this?
end
RUBY
end
it 'does not register an offense when merge! is being assigned inside each_with_object' do
expect_no_offenses(<<~RUBY)
foo.each_with_object({}) do |f, hash|
changes = hash.merge!(a: 1, b: 2)
why_are_you_doing_this?
end
RUBY
end
it 'autocorrects when receiver uses element reference to the object built by each_with_object' do
source = <<~RUBY
foo.each_with_object(bar) do |f, hash|
hash[:a].merge!(b: "")
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
foo.each_with_object(bar) do |f, hash|
hash[:a][:b] = ""
end
RUBY
end
it 'autocorrects when receiver uses multiple element references to the object built by each_with_object' do
source = <<~RUBY
foo.each_with_object(bar) do |f, hash|
hash[:a][:b].merge!(c: "")
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
foo.each_with_object(bar) do |f, hash|
hash[:a][:b][:c] = ""
end
RUBY
end
it 'autocorrects merge! called on any method on the object built by each_with_object' do
source = <<~RUBY
foo.each_with_object(bar) do |f, hash|
hash.bar.merge!(c: "")
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(<<~RUBY)
foo.each_with_object(bar) do |f, hash|
hash.bar[:c] = ""
end
RUBY
end
end
%w[if unless while until].each do |kw|
context "when there is a modifier #{kw}, and more than 1 pair" do
it "autocorrects it to an #{kw} block" do
new_source = autocorrect_source(
<<~RUBY
hash = {}
hash.merge!(a: 1, b: 2) #{kw} condition1 && condition2
RUBY
)
expect(new_source).to eq(<<~RUBY)
hash = {}
#{kw} condition1 && condition2
hash[:a] = 1
hash[:b] = 2
end
RUBY
end
context 'when original code was indented' do
it 'maintains proper indentation' do
new_source = autocorrect_source(
<<~RUBY
hash = {}
begin
hash.merge!(a: 1, b: 2) #{kw} condition1
end
RUBY
)
expect(new_source).to eq(<<~RUBY)
hash = {}
begin
#{kw} condition1
hash[:a] = 1
hash[:b] = 2
end
end
RUBY
end
end
end
end
context 'when code is indented, and there is more than 1 pair' do
it 'indents the autocorrected code properly' do
new_source = autocorrect_source(<<~RUBY)
hash = {}
begin
hash.merge!(a: 1, b: 2)
end
RUBY
expect(new_source).to eq(<<~RUBY)
hash = {}
begin
hash[:a] = 1
hash[:b] = 2
end
RUBY
end
end
it 'does not register an offense when using an empty hash argument' do
expect_no_offenses(<<~RUBY)
foo.merge!({})
RUBY
end
it "doesn't register an error when return value is used" do
expect_no_offenses(<<~RUBY)
variable = hash.merge!(a: 1)
puts variable
RUBY
end
it 'formats the error message correctly for hash.merge!(a: 1)' do
expect_offense(<<~RUBY)
hash.merge!(a: 1)
^^^^^^^^^^^^^^^^^ Use `hash[:a] = 1` instead of `hash.merge!(a: 1)`.
RUBY
end
context 'with MaxKeyValuePairs of 1' do
let(:cop_config) do
{ 'MaxKeyValuePairs' => 1 }
end
it "doesn't register errors for multi-value hash merges" do
expect_no_offenses(<<~RUBY)
hash = {}
hash.merge!(a: 1, b: 2)
RUBY
end
end
context 'when MaxKeyValuePairs is set to nil' do
let(:cop_config) do
{ 'MaxKeyValuePairs' => nil }
end
it 'does not raise `TypeError`' do
expect_offense(<<~RUBY)
hash = {}
hash.merge!(a: 1, b: 2)
^^^^^^^^^^^^^^^^^^^^^^^ Use `hash[:a] = 1; hash[:b] = 2` instead of `hash.merge!(a: 1, b: 2)`.
RUBY
end
end
end