/
guard_clause_spec.rb
423 lines (380 loc) · 10.4 KB
/
guard_clause_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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::GuardClause do
let(:cop) { described_class.new(config) }
let(:config) do
RuboCop::Config.new(
'Layout/LineLength' => {
'Enabled' => line_length_enabled,
'Max' => 80
},
'Style/GuardClause' => cop_config
)
end
let(:line_length_enabled) { true }
let(:cop_config) { {} }
shared_examples 'reports offense' do |body|
it 'reports an offense if method body is if / unless without else' do
expect_offense(<<~RUBY)
def func
if something
^^ Use a guard clause (`return unless something`) instead of wrapping the code inside a conditional expression.
#{body}
end
end
def func
unless something
^^^^^^ Use a guard clause (`return if something`) instead of wrapping the code inside a conditional expression.
#{body}
end
end
RUBY
end
it 'reports an offense if method body ends with if / unless without else' do
expect_offense(<<~RUBY)
def func
test
if something
^^ Use a guard clause (`return unless something`) instead of wrapping the code inside a conditional expression.
#{body}
end
end
def func
test
unless something
^^^^^^ Use a guard clause (`return if something`) instead of wrapping the code inside a conditional expression.
#{body}
end
end
RUBY
end
end
it_behaves_like('reports offense', 'work')
it_behaves_like('reports offense', '# TODO')
it 'does not report an offense if body is if..elsif..end' do
expect_no_offenses(<<~RUBY)
def func
if something
a
elsif something_else
b
end
end
RUBY
end
it "doesn't report an offense if condition has multiple lines" do
expect_no_offenses(<<~RUBY)
def func
if something &&
something_else
work
end
end
def func
unless something &&
something_else
work
end
end
RUBY
end
it 'accepts a method which body is if / unless with else' do
expect_no_offenses(<<~RUBY)
def func
if something
work
else
test
end
end
def func
unless something
work
else
test
end
end
RUBY
end
it 'registers an offense when using `|| raise` in `then` branch' do
expect_offense(<<~RUBY)
def func
if something
^^ Use a guard clause (`work || raise('message') if something`) instead of wrapping the code inside a conditional expression.
work || raise('message')
else
test
end
end
RUBY
end
it 'registers an offense when using `|| raise` in `else` branch' do
expect_offense(<<~RUBY)
def func
if something
^^ Use a guard clause (`test || raise('message') unless something`) instead of wrapping the code inside a conditional expression.
work
else
test || raise('message')
end
end
RUBY
end
it 'registers an offense when using `and return` in `then` branch' do
expect_offense(<<~RUBY)
def func
if something
^^ Use a guard clause (`work and return if something`) instead of wrapping the code inside a conditional expression.
work and return
else
test
end
end
RUBY
end
it 'registers an offense when using `and return` in `else` branch' do
expect_offense(<<~RUBY)
def func
if something
^^ Use a guard clause (`test and return unless something`) instead of wrapping the code inside a conditional expression.
work
else
test and return
end
end
RUBY
end
it 'accepts a method which body does not end with if / unless' do
expect_no_offenses(<<~RUBY)
def func
if something
work
end
test
end
def func
unless something
work
end
test
end
RUBY
end
it 'accepts a method whose body is a modifier if / unless' do
expect_no_offenses(<<~RUBY)
def func
work if something
end
def func
work unless something
end
RUBY
end
it 'accepts a method with empty parentheses as its body' do
expect_no_offenses(<<~RUBY)
def func
()
end
RUBY
end
it 'does not register an offense when assigning the result of ' \
'a guard condition with `else`' do
expect_no_offenses(<<~RUBY)
def func
result = if something
work || raise('message')
else
test
end
end
RUBY
end
context 'MinBodyLength: 1' do
let(:cop_config) do
{ 'MinBodyLength' => 1 }
end
it 'reports an offense for if whose body has 1 line' do
expect_offense(<<~RUBY)
def func
if something
^^ Use a guard clause (`return unless something`) instead of wrapping the code inside a conditional expression.
work
end
end
def func
unless something
^^^^^^ Use a guard clause (`return if something`) instead of wrapping the code inside a conditional expression.
work
end
end
RUBY
end
end
context 'MinBodyLength: 4' do
let(:cop_config) do
{ 'MinBodyLength' => 4 }
end
it 'accepts a method whose body has 3 lines' do
expect_no_offenses(<<~RUBY)
def func
if something
work
work
work
end
end
def func
unless something
work
work
work
end
end
RUBY
end
end
context 'Invalid MinBodyLength' do
let(:cop_config) do
{ 'MinBodyLength' => -2 }
end
it 'fails with an error' do
source = <<~RUBY
def func
if something
work
end
end
RUBY
expect { inspect_source(source) }
.to raise_error('MinBodyLength needs to be a positive integer!')
end
end
shared_examples 'on if nodes which exit current scope' do |kw|
it "registers an error with #{kw} in the if branch" do
expect_offense(<<~RUBY)
if something
^^ Use a guard clause (`#{kw} if something`) instead of wrapping the code inside a conditional expression.
#{kw}
else
puts "hello"
end
RUBY
end
it "registers an error with #{kw} in the else branch" do
expect_offense(<<~RUBY)
if something
^^ Use a guard clause (`#{kw} unless something`) instead of wrapping the code inside a conditional expression.
puts "hello"
else
#{kw}
end
RUBY
end
it "doesn't register an error if condition has multiple lines" do
expect_no_offenses(<<~RUBY)
if something &&
something_else
#{kw}
else
puts "hello"
end
RUBY
end
it "does not report an offense if #{kw} is inside elsif" do
expect_no_offenses(<<~RUBY)
if something
a
elsif something_else
#{kw}
end
RUBY
end
it "does not report an offense if #{kw} is inside if..elsif..else..end" do
expect_no_offenses(<<~RUBY)
if something
a
elsif something_else
b
else
#{kw}
end
RUBY
end
it "doesn't register an error if control flow expr has multiple lines" do
expect_no_offenses(<<~RUBY)
if something
#{kw} 'blah blah blah' \\
'blah blah blah'
else
puts "hello"
end
RUBY
end
it 'registers an error if non-control-flow branch has multiple lines' do
expect_offense(<<~RUBY)
if something
^^ Use a guard clause (`#{kw} if something`) instead of wrapping the code inside a conditional expression.
#{kw}
else
puts "hello" \\
"blah blah blah"
end
RUBY
end
end
context 'with Metrics/MaxLineLength enabled' do
it 'registers an offense with non-modifier example code if too long for ' \
'single line' do
expect_offense(<<~RUBY)
def test
if something && something_that_makes_the_guard_clause_too_long_to_fit_on_one_line
^^ Use a guard clause (`unless something && something_that_makes_the_guard_clause_too_long_to_fit_on_one_line; return; end`) instead of wrapping the code inside a conditional expression.
work
end
end
RUBY
end
end
context 'with Metrics/MaxLineLength disabled' do
let(:line_length_enabled) { false }
it 'registers an offense with modifier example code regardless of length' do
expect_offense(<<~RUBY)
def test
if something && something_that_makes_the_guard_clause_too_long_to_fit_on_one_line
^^ Use a guard clause (`return unless something && something_that_makes_the_guard_clause_too_long_to_fit_on_one_line`) instead of wrapping the code inside a conditional expression.
work
end
end
RUBY
end
end
include_examples('on if nodes which exit current scope', 'return')
include_examples('on if nodes which exit current scope', 'next')
include_examples('on if nodes which exit current scope', 'break')
include_examples('on if nodes which exit current scope', 'raise "error"')
context 'method in module' do
it 'registers an offense for instance method' do
expect_offense(<<~RUBY)
module CopTest
def test
if something
^^ Use a guard clause (`return unless something`) instead of wrapping the code inside a conditional expression.
work
end
end
end
RUBY
end
it 'registers an offense for singleton methods' do
expect_offense(<<~RUBY)
module CopTest
def self.test
if something && something_else
^^ Use a guard clause (`return unless something && something_else`) instead of wrapping the code inside a conditional expression.
work
end
end
end
RUBY
end
end
end