/
collection_literal_in_loop_spec.rb
274 lines (243 loc) · 7.95 KB
/
collection_literal_in_loop_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::CollectionLiteralInLoop, :config do
let(:cop_config) do
{ 'MinSize' => 1 }
end
context 'when inside `while` loop' do
it 'registers an offense when using Array literal' do
expect_offense(<<~RUBY)
while true
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
it 'registers an offense when using Hash literal' do
expect_offense(<<~RUBY)
while i < 100
{ foo: :bar }.key?(:foo)
^^^^^^^^^^^^^ Avoid immutable Hash literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
end
context 'when inside `until` loop' do
it 'registers an offense when using Array literal' do
expect_offense(<<~RUBY)
until i < 100
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
it 'registers an offense when using Hash literal' do
expect_offense(<<~RUBY)
until i < 100
{ foo: :bar }.key?(:foo)
^^^^^^^^^^^^^ Avoid immutable Hash literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
end
context 'when inside `for` loop' do
it 'registers an offense when using Array literal' do
expect_offense(<<~RUBY)
for i in 1..100
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
it 'registers an offense when using Hash literal' do
expect_offense(<<~RUBY)
for i in 1..100
{ foo: :bar }.key?(:foo)
^^^^^^^^^^^^^ Avoid immutable Hash literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
end
context 'when inside `while modifier` loop' do
it 'registers an offense when using Array literal' do
expect_offense(<<~RUBY)
begin
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end while i < 100
RUBY
end
it 'registers an offense when using Hash literal' do
expect_offense(<<~RUBY)
begin
{ foo: :bar }.key?(:foo)
^^^^^^^^^^^^^ Avoid immutable Hash literals in loops. It is better to extract it into a local variable or a constant.
end while i < 100
RUBY
end
end
context 'when inside `until modifier` loop' do
it 'registers an offense when using Array literal' do
expect_offense(<<~RUBY)
begin
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end until i < 100
RUBY
end
it 'registers an offense when using Hash literal' do
expect_offense(<<~RUBY)
begin
{ foo: :bar }.key?(:foo)
^^^^^^^^^^^^^ Avoid immutable Hash literals in loops. It is better to extract it into a local variable or a constant.
end until i < 100
RUBY
end
end
context 'when inside `Kernel#loop` loop' do
it 'registers an offense when using Array literal' do
expect_offense(<<~RUBY)
loop do
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
it 'registers an offense when using Hash literal' do
expect_offense(<<~RUBY)
loop do
{ foo: :bar }.key?(:foo)
^^^^^^^^^^^^^ Avoid immutable Hash literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
end
context 'when inside one of `Enumerable` loop-like methods' do
it 'registers an offense when using Array literal' do
expect_offense(<<~RUBY)
array.all? do |e|
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
it 'registers an offense when using Hash literal' do
expect_offense(<<~RUBY)
array.all? do |e|
{ foo: :bar }.key?(:foo)
^^^^^^^^^^^^^ Avoid immutable Hash literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
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.
it 'registers an offense when the method is called with no receiver', broken_on: :prism do
expect_offense(<<~RUBY)
all? do |e|
[1, 2, 3].include?(e)
^^^^^^^^^ Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
RUBY
end
end
context 'when not inside loop' do
it 'does not register an offense when using Array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, 3].include?(e)
RUBY
end
it 'does not register an offense when using Hash literal' do
expect_no_offenses(<<~RUBY)
{ foo: :bar }.key?(:foo)
RUBY
end
end
context 'when literal contains element of non basic type' do
it 'does not register an offense when using Array literal' do
expect_no_offenses(<<~RUBY)
[1, 2, variable].include?(e)
RUBY
end
it 'does not register an offense when using Hash literal' do
expect_no_offenses(<<~RUBY)
{ foo: { bar: variable } }.key?(:foo)
RUBY
end
end
context 'when is an argument for `Enumerable` loop-like method' do
it 'does not register an offense when using Array literal' do
expect_no_offenses(<<~RUBY)
[[1, 2, 3] | [2, 3, 4]].each { |e| puts e }
RUBY
end
it 'does not register an offense when using Hash literal' do
expect_no_offenses(<<~RUBY)
({ foo: :bar }.merge(baz: :quux)).each { |k, v| puts k + v }
RUBY
end
end
context 'when destructive method is called' do
it 'does not register an offense when using Array literal' do
expect_no_offenses(<<~RUBY)
loop do
[1, nil, 3].compact!
end
RUBY
end
it 'does not register an offense when using Hash literal' do
expect_no_offenses(<<~RUBY)
loop do
{ foo: :bar, baz: nil }.select! { |_k, v| !v.nil? }
end
RUBY
end
end
context 'when none method is called' do
it 'does not register an offense when using Array literal' do
expect_no_offenses(<<~RUBY)
loop do
array = [1, nil, 3]
end
RUBY
end
it 'does not register an offense when using Hash literal' do
expect_no_offenses(<<~RUBY)
loop do
hash = { foo: :bar, baz: nil }
end
RUBY
end
end
it 'does not register an offense when there are no literals in a loop' do
expect_no_offenses(<<~RUBY)
while x < 100
puts x
end
RUBY
end
it 'does not register an offense when nondestructive method is called on nonliteral' do
expect_no_offenses(<<~RUBY)
loop do
array.all? { |x| x > 100 }
end
RUBY
end
context 'with MinSize of 2' do
let(:cop_config) do
{ 'MinSize' => 2 }
end
it 'does not register an offense when using Array literal' do
expect_no_offenses(<<~RUBY)
while true
[1].include?(e)
end
RUBY
end
it 'does not register an offense when using Hash literal' do
expect_no_offenses(<<~RUBY)
while i < 100
{ foo: :bar }.key?(:foo)
end
RUBY
end
end
end