forked from rubocop/rubocop
/
unreachable_loop_spec.rb
253 lines (227 loc) · 6.01 KB
/
unreachable_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::UnreachableLoop, :config do
context 'without preceding continue statements' do
it 'registers an offense when using `break`' do
expect_offense(<<~RUBY)
while x > 0
^^^^^^^^^^^ This loop will have at most one iteration.
x += 1
break
end
RUBY
end
it 'registers an offense when using `if-else` with all break branches' do
expect_offense(<<~RUBY)
while x > 0
^^^^^^^^^^^ This loop will have at most one iteration.
if condition
break
else
raise MyError
end
end
RUBY
end
it 'does not register an offense when using `if` without `else`' do
expect_no_offenses(<<~RUBY)
while x > 0
if condition
break
elsif other_condition
raise MyError
end
end
RUBY
end
it 'does not register an offense when using `if-elsif-else` and not all branches are breaking' do
expect_no_offenses(<<~RUBY)
while x > 0
if condition
break
elsif other_condition
do_something
else
raise MyError
end
end
RUBY
end
it 'registers an offense when using `case-when-else` with all break branches' do
expect_offense(<<~RUBY)
while x > 0
^^^^^^^^^^^ This loop will have at most one iteration.
case x
when 1
break
else
raise MyError
end
end
RUBY
end
it 'does not register an offense when using `case` without `else`' do
expect_no_offenses(<<~RUBY)
while x > 0
case x
when 1
break
end
end
RUBY
end
it 'does not register an offense when using `case-when-else` and not all branches are breaking' do
expect_no_offenses(<<~RUBY)
while x > 0
case x
when 1
break
when 2
do_something
else
raise MyError
end
end
RUBY
end
end
context 'with preceding continue statements' do
it 'does not register an offense when using `break`' do
expect_no_offenses(<<~RUBY)
while x > 0
next if x.odd?
x += 1
break
end
RUBY
end
it 'does not register an offense when using `if-else` with all break branches' do
expect_no_offenses(<<~RUBY)
while x > 0
next if x.odd?
if condition
break
else
raise MyError
end
end
RUBY
end
it 'does not register an offense when using `case-when-else` with all break branches' do
expect_no_offenses(<<~RUBY)
while x > 0
redo if x.odd?
case x
when 1
break
else
raise MyError
end
end
RUBY
end
end
context 'with an enumerator method' do
context 'as the last item in a method chain' do
it 'registers an offense' do
expect_offense(<<~RUBY)
string.split('-').map { raise StandardError }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This loop will have at most one iteration.
RUBY
end
end
context 'not chained' do
it 'registers an offense' do
expect_offense(<<~RUBY)
string.map { raise StandardError }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This loop will have at most one iteration.
RUBY
end
end
context 'in the middle of a method chain' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
exactly(2).times.with(x) { raise StandardError }
RUBY
end
end
end
context 'with IgnoredPatterns' do
let(:cop_config) { { 'IgnoredPatterns' => [/exactly\(\d+\)\.times/] } }
context 'with a ignored method call' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
exactly(2).times { raise StandardError }
RUBY
end
end
context 'with a non ignored method call' do
it 'registers an offense' do
expect_offense(<<~RUBY)
2.times { raise StandardError }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This loop will have at most one iteration.
RUBY
end
end
end
it 'handles inner loops' do
expect_offense(<<~RUBY)
until x > 0
^^^^^^^^^^^ This loop will have at most one iteration.
items.each do |item|
next if item.odd?
break
end
if x > 0
break some_value
else
raise MyError
end
loop do
^^^^^^^ This loop will have at most one iteration.
case y
when 1
return something
when 2
break
else
throw :exit
end
end
end
RUBY
end
it 'does not register an offense when branch includes continue statement preceding break statement' do
expect_no_offenses(<<~RUBY)
while x > 0
if y
next if something
break
else
break
end
end
RUBY
end
it 'does not register an offense when using `return do_something(value) || next` in a loop' do
expect_no_offenses(<<~RUBY)
[nil, nil, 42].each do |value|
return do_something(value) || next
end
RUBY
end
it 'does not register an offense when using `return do_something(value) || redo` in a loop' do
expect_no_offenses(<<~RUBY)
[nil, nil, 42].each do |value|
return do_something(value) || redo
end
RUBY
end
it 'registers an offense when using `return do_something(value) || break` in a loop' do
expect_offense(<<~RUBY)
[nil, nil, 42].each do |value|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This loop will have at most one iteration.
return do_something(value) || break
end
RUBY
end
end