/
if_unless_modifier_spec.rb
772 lines (657 loc) · 19.3 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# frozen_string_literal: true
######
# Note: most of these tests probably belong in the shared context "condition modifier cop"
######
RSpec.describe RuboCop::Cop::Style::IfUnlessModifier, :config do
let(:ignore_cop_directives) { true }
let(:allow_uri) { true }
let(:line_length_config) do
{
'Enabled' => true,
'Max' => 80,
'AllowURI' => allow_uri,
'IgnoreCopDirectives' => ignore_cop_directives,
'URISchemes' => %w[http https]
}
end
let(:other_cops) { { 'Layout/LineLength' => line_length_config } }
extra = ' Another good alternative is the usage of control flow `&&`/`||`.'
it_behaves_like 'condition modifier cop', :if, extra
it_behaves_like 'condition modifier cop', :unless, extra
context 'modifier if that does not fit on one line' do
let(:spaces) { ' ' * 59 }
let(:body) { "puts '#{spaces}'" }
let(:source) { "#{body} 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, body: body)
def f
# Comment 1
%{body} if condition # Comment 2
_{body} ^^ 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(:body) { "puts '#{spaces}'" }
context 'and the long line is allowed because IgnoreCopDirectives is ' \
'true' do
it 'accepts' do
expect("#{body} if condition".length).to eq(77) # That's 79 including indentation.
expect_no_offenses(<<~RUBY)
def f
#{body} if condition #{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, body: body)
def f
%{body} if condition #{comment}
_{body} ^^ Modifier form of `if` makes the line too long.
end
RUBY
expect_correction(<<~RUBY)
def f
if condition
#{body}
end #{comment}
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(: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)
# Empty lines should make no difference.
expect_offense(<<~RUBY)
if #{condition}
^^ Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
#{body}
end
RUBY
expect_correction(<<~RUBY)
#{body} if #{condition}
RUBY
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 }
# 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
puts '#{spaces}' if condition; some_method_call
end
RUBY
end
end
context 'multiline if that fits on one line with comment on first line' do
it 'registers an offense and preserves comment' 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
expect_correction(<<~RUBY)
b if a # comment
RUBY
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
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
expect_correction(<<~RUBY)
if x
y
elsif x1
y1
else
z
end
n = a ? 0 : 1
m = 3 if m0
b if a
RUBY
end
end
context 'multiline unless that fits on one line' do
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
expect_correction(<<~RUBY)
b unless a
RUBY
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 if/elsif' do
expect_no_offenses(<<~RUBY)
if test
something
elsif test2
something_else
end
RUBY
end
context 'with implicit match conditional' do
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)
expect_offense(<<-RUBY.strip_margin('|'))
| if #{conditional}
| ^^ Favor modifier `if` usage when having a single-line body. [...]
| #{body}
| end
RUBY
expect_correction(" #{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(<<-RUBY.strip_margin('|'))
| if #{conditional}
| #{body}
| end
RUBY
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
expect_offense(<<~RUBY)
a = if b
^^ Favor modifier `if` usage when having a single-line body. [...]
1
end
RUBY
expect_correction(<<~RUBY)
a = (1 if b)
RUBY
end
it "doesn't break if-end when used as RHS of instance var assignment" do
expect_offense(<<~RUBY)
@a = if b
^^ Favor modifier `if` usage when having a single-line body. [...]
1
end
RUBY
expect_correction(<<~RUBY)
@a = (1 if b)
RUBY
end
it "doesn't break if-end when used as RHS of class var assignment" do
expect_offense(<<~RUBY)
@@a = if b
^^ Favor modifier `if` usage when having a single-line body. [...]
1
end
RUBY
expect_correction(<<~RUBY)
@@a = (1 if b)
RUBY
end
it "doesn't break if-end when used as RHS of constant assignment" do
expect_offense(<<~RUBY)
A = if b
^^ Favor modifier `if` usage when having a single-line body. [...]
1
end
RUBY
expect_correction(<<~RUBY)
A = (1 if b)
RUBY
end
it "doesn't break if-end when used as RHS of binary arithmetic" do
expect_offense(<<~RUBY)
a + if b
^^ Favor modifier `if` usage when having a single-line body. [...]
1
end
RUBY
expect_correction(<<~RUBY)
a + (1 if b)
RUBY
end
it 'adds parens in autocorrect when if-end used with `||` operator' do
expect_offense(<<~RUBY)
a || if b
^^ Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
1
end
RUBY
expect_correction(<<~RUBY)
a || (1 if b)
RUBY
end
it 'adds parens in autocorrect when if-end used with `&&` operator' do
expect_offense(<<~RUBY)
a && if b
^^ Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
1
end
RUBY
expect_correction(<<~RUBY)
a && (1 if b)
RUBY
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
expect_offense(<<~RUBY)
puts("string", if a
^^ Favor modifier `if` usage when having a single-line body. [...]
1
end)
RUBY
expect_correction(<<~RUBY)
puts("string", 1 if a)
RUBY
end
end
context 'if-end is argument to a non-parenthesized method call' do
it 'adds parentheses so as not to change meaning' do
expect_offense(<<~RUBY)
puts "string", if a
^^ Favor modifier `if` usage when having a single-line body. [...]
1
end
RUBY
expect_correction(<<~RUBY)
puts "string", (1 if a)
RUBY
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(:indent) { "\t" * 6 }
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)
expect_offense(<<~RUBY, indent: indent)
%{indent}if #{condition}
_{indent}^^ Favor modifier `if` usage when having a single-line body. [...]
%{indent}\t#{body}
%{indent}end
RUBY
expect_correction(<<~RUBY)
#{indent}#{body} if #{condition}
RUBY
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(<<~RUBY)
#{indent}if #{condition}
#{indent}\t#{body}
#{indent}end
RUBY
end
end
end
context 'with Layout/IndentationStyle: IndentationWidth config' do
let(:other_cops) do
{
'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(:other_cops) do
{
'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(:line_length_config) { { 'Enabled' => false } }
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
expect_correction(<<~RUBY)
"This string would make the line longer than eighty characters if combined with the statement." if foo
RUBY
end
end
context 'when if-end condition is assigned to a variable' do
context 'with variable being on the same line' do
let(:source) do
<<~RUBY
x = if a
#{'b' * body_length}
end
RUBY
end
context 'when it is short enough to fit on a single line' do
let(:body_length) { 69 }
it 'corrects it to the single-line form' do
corrected = autocorrect_source(source)
expect(corrected).to eq "x = (#{'b' * body_length} if a)\n"
end
end
context 'when it is not short enough to fit on a single line' do
let(:body_length) { 70 }
it 'accepts it in the multiline form' do
expect_no_offenses(source)
end
end
end
context 'with variable being on the previous line' do
let(:source) do
<<~RUBY
x =
if a
#{'b' * body_length}
end
RUBY
end
context 'when it is short enough to fit on a single line' do
let(:body_length) { 71 }
it 'corrects it to the single-line form' do
corrected = autocorrect_source(source)
expect(corrected).to eq "x =\n (#{'b' * body_length} if a)\n"
end
end
context 'when it is not short enough to fit on a single line' do
let(:body_length) { 72 }
it 'accepts it in the multiline form' do
expect_no_offenses(source)
end
end
end
end
context 'when if-end condition is an element of an array' do
let(:source) do
<<~RUBY
[
if a
#{'b' * body_length}
end
]
RUBY
end
context 'when short enough to fit on a single line' do
let(:body_length) { 71 }
it 'corrects it to the single-line form' do
corrected = autocorrect_source(source)
expect(corrected).to eq "[\n (#{'b' * body_length} if a)\n]\n"
end
end
context 'when not short enough to fit on a single line' do
let(:body_length) { 72 }
it 'accepts it in the multiline form' do
expect_no_offenses(source)
end
end
end
context 'when if-end condition is a value in a hash' do
let(:source) do
<<~RUBY
{
x: if a
#{'b' * body_length}
end
}
RUBY
end
context 'when it is short enough to fit on a single line' do
let(:body_length) { 68 }
it 'corrects it to the single-line form' do
corrected = autocorrect_source(source)
expect(corrected).to eq "{\n x: (#{'b' * body_length} if a)\n}\n"
end
end
context 'when it is not short enough to fit on a single line' do
let(:body_length) { 69 }
it 'accepts it in the multiline form' do
expect_no_offenses(source)
end
end
end
context 'when if-end condition has a first line comment' do
let(:source) do
<<~RUBY
if foo # #{'c' * comment_length}
bar
end
RUBY
end
context 'when it is short enough to fit on a single line' do
let(:comment_length) { 67 }
it 'corrects it to the single-line form' do
corrected = autocorrect_source(source)
expect(corrected).to eq "bar if foo # #{'c' * comment_length}\n"
end
end
context 'when it is not short enough to fit on a single line' do
let(:comment_length) { 68 }
it 'accepts it in the multiline form' do
expect_no_offenses(source)
end
end
end
context 'when if-end condition has some code after the end keyword' do
let(:source) do
<<~RUBY
[
1, if foo
#{'b' * body_length}
end, 300_000_000
]
RUBY
end
context 'when it is short enough to fit on a single line' do
let(:body_length) { 53 }
it 'corrects it to the single-line form' do
corrected = autocorrect_source(source)
expect(corrected).to eq "[\n 1, (#{'b' * body_length} if foo), 300_000_000\n]\n"
end
end
context 'when it is not short enough to fit on a single line' do
let(:body_length) { 54 }
it 'accepts it in the multiline form' do
expect_no_offenses(source)
end
end
end
end