forked from rubocop/rubocop
/
if_unless_modifier_spec.rb
625 lines (540 loc) · 15.2 KB
/
if_unless_modifier_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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::IfUnlessModifier do
include StatementModifierHelper
subject(:cop) { described_class.new(config) }
let(:config) do
RuboCop::Config.new('Layout/LineLength' => line_length_config)
end
let(:line_length_config) do
{
'Enabled' => true,
'Max' => 80,
'AllowURI' => allow_uri,
'IgnoreCopDirectives' => ignore_cop_directives,
'URISchemes' => %w[http https]
}
end
let(:allow_uri) { true }
let(:ignore_cop_directives) { true }
context 'modifier if that does not fit on one line' do
let(:spaces) { ' ' * 59 }
let(:source) { "puts '#{spaces}' if condition" }
let(:long_url) do
'https://some.example.com/with/a/rather?long&and=very&complicated=path'
end
context 'when Layout/LineLength is enabled' do
it 'corrects it to normal form' do
expect(source.length).to be(79) # That's 81 including indentation.
expect_offense(<<~RUBY)
def f
# Comment 1
#{source} # Comment 2
#{spaces} ^^ Modifier form of `if` makes the line too long.
end
RUBY
expect_correction(<<~RUBY)
def f
# Comment 1
if condition
puts '#{spaces}'
end # Comment 2
end
RUBY
end
context 'and the long line is allowed because AllowURI is true' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
puts 1 if url == '#{long_url}'
RUBY
end
end
context 'and the long line is too long because AllowURI is false' do
let(:allow_uri) { false }
it 'registers an offense' do
expect_offense(<<~RUBY)
puts 1 if url == '#{long_url}'
^^ Modifier form of `if` makes the line too long.
RUBY
expect_correction(<<~RUBY)
if url == '#{long_url}'
puts 1
end
RUBY
end
end
describe 'IgnoreCopDirectives' do
let(:spaces) { ' ' * 57 }
let(:comment) { '# rubocop:disable Style/For' }
let(:source) { "puts '#{spaces}' if condition" }
context 'and the long line is allowed because IgnoreCopDirectives is ' \
'true' do
it 'accepts' do
expect(source.length).to eq(77) # That's 79 including indentation.
expect_no_offenses(<<~RUBY)
def f
#{source} #{comment}
end
RUBY
end
end
context 'and the long line is too long because IgnoreCopDirectives ' \
'is false' do
let(:ignore_cop_directives) { false }
it 'registers an offense' do
expect_offense(<<~RUBY)
def f
#{source} #{comment}
#{spaces} ^^ Modifier form of `if` makes the line too long.
end
RUBY
end
end
end
end
context 'when Layout/LineLength is disabled in configuration' do
let(:line_length_config) { { 'Enabled' => false, 'Max' => 80 } }
it 'accepts' do
expect_no_offenses(<<~RUBY)
def f
#{source}
end
RUBY
end
end
context 'when Layout/LineLength is disabled with enable/disable ' \
'comments' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
def f
# rubocop:disable Layout/LineLength
#{source}
# rubocop:enable Layout/LineLength
end
RUBY
end
end
context 'when Layout/LineLength is disabled with an EOL comment' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
def f
#{source} # rubocop:disable Layout/LineLength
end
RUBY
end
end
end
context 'multiline if that fits on one line' do
let(:source) do
# Empty lines should make no difference.
<<~RUBY
if #{condition}
#{body}
end
RUBY
end
let(:condition) { 'a' * 38 }
let(:body) { 'b' * 38 }
it 'registers an offense' do
# This if statement fits exactly on one line if written as a
# modifier.
expect("#{body} if #{condition}".length).to eq(80)
inspect_source(source)
expect(cop.messages).to eq(
['Favor modifier `if` usage when having a single-line' \
' body. Another good alternative is the usage of control flow' \
' `&&`/`||`.']
)
end
it 'does auto-correction' do
corrected = autocorrect_source(source)
expect(corrected).to eq "#{body} if #{condition}\n"
end
context 'and has two statements separated by semicolon' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
if condition
do_this; do_that
end
RUBY
end
end
end
context 'modifier if that does not fit on one line, but is not the only' \
' statement on the line' do
let(:spaces) { ' ' * 59 }
let(:source) { "puts '#{spaces}' if condition; some_method_call" }
# long lines which have multiple statements on the same line can be flagged
# by Layout/LineLength, Style/Semicolon, etc.
# if they are handled by Style/IfUnlessModifier, there is a danger of
# creating infinite autocorrect loops when autocorrecting
it 'accepts' do
expect_no_offenses(<<~RUBY)
def f
#{source}
end
RUBY
end
end
context 'multiline if that fits on one line with comment on first line' do
let(:source) do
<<~RUBY
if a # comment
b
end
RUBY
end
it 'registers an offense' do
expect_offense(<<~RUBY)
if a # comment
^^ Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
b
end
RUBY
end
it 'does auto-correction and preserves comment' do
corrected = autocorrect_source(source)
expect(corrected).to eq "b if a # comment\n"
end
end
context 'multiline if that fits on one line with comment near end' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
if a
b
end # comment
if a
b
# comment
end
RUBY
end
end
context 'short multiline if near an else etc' do
let(:source) do
<<~RUBY
if x
y
elsif x1
y1
else
z
end
n = a ? 0 : 1
m = 3 if m0
if a
b
end
RUBY
end
it 'registers an offense' do
expect_offense(<<~RUBY)
if x
y
elsif x1
y1
else
z
end
n = a ? 0 : 1
m = 3 if m0
if a
^^ Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
b
end
RUBY
end
it 'does auto-correction' do
corrected = autocorrect_source(source)
expect(corrected).to eq(<<~RUBY)
if x
y
elsif x1
y1
else
z
end
n = a ? 0 : 1
m = 3 if m0
b if a
RUBY
end
end
it "accepts multiline if that doesn't fit on one line" do
check_too_long('if')
end
it 'accepts multiline if whose body is more than one line' do
check_short_multiline('if')
end
context 'multiline unless that fits on one line' do
let(:source) do
<<~RUBY
unless a
b
end
RUBY
end
it 'registers an offense' do
expect_offense(<<~RUBY)
unless a
^^^^^^ Favor modifier `unless` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
b
end
RUBY
end
it 'does auto-correction' do
corrected = autocorrect_source(source)
expect(corrected).to eq "b unless a\n"
end
end
it 'accepts code with EOL comment since user might want to keep it' do
expect_no_offenses(<<~RUBY)
unless a
b # A comment
end
RUBY
end
it 'accepts if-else-end' do
expect_no_offenses(<<~RUBY)
if args.last.is_a? Hash then args.pop else Hash.new end
RUBY
end
it 'accepts an empty condition' do
check_empty('if')
check_empty('unless')
end
it 'accepts if/elsif' do
expect_no_offenses(<<~RUBY)
if test
something
elsif test2
something_else
end
RUBY
end
context 'with implicit match conditional' do
let(:source) do
<<-RUBY.strip_margin('|')
| if #{conditional}
| #{body}
| end
RUBY
end
let(:body) { 'b' * 36 }
context 'when a multiline if fits on one line' do
let(:conditional) { "/#{'a' * 36}/" }
it 'registers an offense' do
expect(" #{body} if #{conditional}".length).to eq(80)
inspect_source(source)
expect(cop.offenses.size).to eq(1)
end
it 'does auto-correction' do
corrected = autocorrect_source(source)
expect(corrected).to eq " #{body} if #{conditional}\n"
end
end
context "when a multiline if doesn't fit on one line" do
let(:conditional) { "/#{'a' * 37}/" }
it 'accepts' do
expect(" #{body} if #{conditional}".length).to eq(81)
expect_no_offenses(source)
end
end
end
it 'accepts if-end followed by a chained call using `.`' do
expect_no_offenses(<<~RUBY)
if test
something
end.inspect
RUBY
end
it 'accepts if-end followed by a chained call using `&.`' do
expect_no_offenses(<<~RUBY)
if test
something
end&.inspect
RUBY
end
it "doesn't break if-end when used as RHS of local var assignment" do
corrected = autocorrect_source(<<~RUBY)
a = if b
1
end
RUBY
expect(corrected).to eq "a = (1 if b)\n"
end
context 'when the code before condition pushes inline length over limit' do
let(:line_length_config) { { 'Enabled' => true, 'Max' => 100 } }
it 'does not result in infinite loop with assignment' do
expect_no_offenses(<<~RUBY)
timespan = if params[:timespan] && POSSIBLE_TIMESPANS.include?(params[:timespan])
params[:timespan].to_sym
end
RUBY
end
it 'does not result in infinite loop with any other statement' do
expect_no_offenses(<<~RUBY)
puts 'ab'; if params[:timespan] && POSSIBLE_TIMESPANS.include?(params[:timespan])
params[:timespan].to_sym
end
RUBY
end
end
it "doesn't break if-end when used as RHS of instance var assignment" do
corrected = autocorrect_source(<<~RUBY)
@a = if b
1
end
RUBY
expect(corrected).to eq "@a = (1 if b)\n"
end
it "doesn't break if-end when used as RHS of class var assignment" do
corrected = autocorrect_source(<<~RUBY)
@@a = if b
1
end
RUBY
expect(corrected).to eq "@@a = (1 if b)\n"
end
it "doesn't break if-end when used as RHS of constant assignment" do
corrected = autocorrect_source(<<~RUBY)
A = if b
1
end
RUBY
expect(corrected).to eq "A = (1 if b)\n"
end
it "doesn't break if-end when used as RHS of binary arithmetic" do
corrected = autocorrect_source(<<~RUBY)
a + if b
1
end
RUBY
expect(corrected).to eq "a + (1 if b)\n"
end
it 'accepts if-end when used as LHS of binary arithmetic' do
expect_no_offenses(<<~RUBY)
if test
1
end + 2
RUBY
end
context 'if-end is argument to a parenthesized method call' do
it "doesn't add redundant parentheses" do
corrected = autocorrect_source(<<~RUBY)
puts("string", if a
1
end)
RUBY
expect(corrected).to eq "puts(\"string\", 1 if a)\n"
end
end
context 'if-end is argument to a non-parenthesized method call' do
it 'adds parentheses so as not to change meaning' do
corrected = autocorrect_source(<<~RUBY)
puts "string", if a
1
end
RUBY
expect(corrected).to eq "puts \"string\", (1 if a)\n"
end
end
context 'if-end with conditional as body' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
if condition
foo ? "bar" : "baz"
end
RUBY
end
end
context 'unless-end with conditional as body' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
unless condition
foo ? "bar" : "baz"
end
RUBY
end
end
context 'with a named regexp capture on the LHS' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
if /(?<foo>\d)/ =~ "bar"
foo
end
RUBY
end
end
context 'with tabs used for indentation' do
shared_examples 'with tabs indentation' do
let(:source) do
# Empty lines should make no difference.
<<-RUBY
if #{condition}
#{body}
end
RUBY
end
let(:body) { 'bbb' }
context 'it fits on one line' do
let(:condition) { 'aaa' }
it 'registers an offense' do
# This if statement fits exactly on one line if written as a
# modifier.
expect("#{body} if #{condition}".length).to eq(10)
inspect_source(source)
expect(cop.messages).to eq(
['Favor modifier `if` usage when having a single-line' \
' body. Another good alternative is the usage of control flow' \
' `&&`/`||`.']
)
end
end
context "it doesn't fit on one line" do
let(:condition) { 'aaaa' }
it "doesn't register an offense" do
# This if statement fits exactly on one line if written as a
# modifier.
expect("#{body} if #{condition}".length).to eq(11)
expect_no_offenses(source)
end
end
end
context 'with Layout/IndentationStyle: IndentationWidth config' do
let(:config) do
RuboCop::Config.new(
'Layout/IndentationStyle' => { 'IndentationWidth' => 2 },
'Layout/LineLength' => { 'Max' => 10 + 12 } # 12 is indentation
)
end
it_behaves_like 'with tabs indentation'
end
context 'with Layout/IndentationWidth: Width config' do
let(:config) do
RuboCop::Config.new(
'Layout/IndentationWidth' => { 'Width' => 3 },
'Layout/LineLength' => { 'Max' => 10 + 18 } # 18 is indentation
)
end
it_behaves_like 'with tabs indentation'
end
end
context 'when Layout/LineLength is disabled' do
let(:config) do
RuboCop::Config.new(
'Layout/LineLength' => {
'Enabled' => false,
'Max' => 80
}
)
end
it 'registers an offense even for a long modifier statement' do
expect_offense(<<~RUBY)
if foo
^^ Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
"This string would make the line longer than eighty characters if combined with the statement."
end
RUBY
end
end
end