/
indentation_width_spec.rb
1577 lines (1401 loc) · 40.4 KB
/
indentation_width_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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::IndentationWidth, :config do
let(:config) do
RuboCop::Config.new(
'Layout/IndentationWidth' => cop_config,
'Layout/AccessModifierIndentation' => access_modifier_config,
'Layout/IndentationConsistency' => consistency_config,
'Layout/EndAlignment' => end_alignment_config,
'Layout/DefEndAlignment' => def_end_alignment_config
)
end
let(:access_modifier_config) { { 'EnforcedStyle' => 'indent' } }
let(:consistency_config) { { 'EnforcedStyle' => 'normal' } }
let(:end_alignment_config) { { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'variable' } }
let(:def_end_alignment_config) do
{ 'Enabled' => true, 'EnforcedStyleAlignWith' => 'start_of_line' }
end
context 'with Width set to 4' do
let(:cop_config) { { 'Width' => 4 } }
context 'for a file with byte order mark' do
let(:bom) { "\xef\xbb\xbf" }
it 'accepts correctly indented method definition' do
expect_no_offenses(<<~RUBY)
#{bom}class Test
def method
end
end
RUBY
end
end
context 'with ignored patterns set' do
let(:cop_config) do
{
'Width' => 4,
'IgnoredPatterns' => ['^\s*module', '^\s*(els)?if.*[A-Z][a-z]+']
}
end
it 'accepts unindented lines for those keywords' do
expect_no_offenses(<<~RUBY)
module Foo
class Test
if blah
if blah == Apple
puts "sweet"
elsif blah == Lemon
puts "sour"
elsif blah == popcorn
puts "salty"
end
end
end
end
RUBY
end
end
context 'with if statement' do
it 'registers an offense for bad indentation of an if body' do
expect_offense(<<~RUBY)
if cond
func
^ Use 4 (not 1) spaces for indentation.
end
RUBY
end
end
it 'registers and corrects offense for bad indentation' do
expect_offense(<<~RUBY)
if a1
b1
^^^ Use 4 (not 3) spaces for indentation.
b1
elsif a2
b2
^ Use 4 (not 1) spaces for indentation.
else
c
end
RUBY
# The second `b1` will be corrected by IndentationConsistency.
expect_correction(<<~RUBY)
if a1
b1
b1
elsif a2
b2
else
c
end
RUBY
end
end
context 'with Width set to 2' do
let(:cop_config) { { 'Width' => 2 } }
context 'with if statement' do
it 'registers an offense for bad indentation of an if body' do
expect_offense(<<~RUBY)
if cond
func
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'registers an offense for bad indentation of an else body' do
expect_offense(<<~RUBY)
if cond
func1
else
func2
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'registers an offense for bad indentation of an else body when if body contains no code' do
expect_offense(<<~RUBY)
if cond
# nothing here
else
func2
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'registers an offense for bad indentation of an else body when if ' \
'and elsif body contains no code' do
expect_offense(<<~RUBY)
if cond
# nothing here
elsif cond2
# nothing here either
else
func2
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'registers an offense for bad indentation of an elsif body' do
expect_offense(<<~RUBY)
if a1
b1
elsif a2
b2
^ Use 2 (not 1) spaces for indentation.
else
c
end
RUBY
end
it 'registers offense for bad indentation of ternary inside else' do
expect_offense(<<~RUBY)
if a
b
else
x ? y : z
^^^^^ Use 2 (not 5) spaces for indentation.
end
RUBY
end
it 'registers offense for bad indentation of modifier if in else' do
expect_offense(<<~RUBY)
if a
b
else
x if y
^^^ Use 2 (not 3) spaces for indentation.
end
RUBY
end
it 'accepts indentation after if on new line after assignment' do
expect_no_offenses(<<~RUBY)
Rails.application.config.ideal_postcodes_key =
if Rails.env.production? || Rails.env.staging?
"AAAA-AAAA-AAAA-AAAA"
end
RUBY
end
it 'accepts `rescue` after an empty body' do
expect_no_offenses(<<~RUBY)
begin
rescue
handle_error
end
RUBY
end
it 'accepts `ensure` after an empty body' do
expect_no_offenses(<<~RUBY)
begin
ensure
something
end
RUBY
end
it 'accepts `rescue`/`ensure` after an empty body' do
expect_no_offenses(<<~RUBY)
begin
rescue
handle_error
ensure
something
end
RUBY
end
it 'accepts `rescue` after an empty def' do
expect_no_offenses(<<~RUBY)
def foo
rescue
handle_error
end
RUBY
end
it 'accepts `ensure` after an empty def' do
expect_no_offenses(<<~RUBY)
def foo
ensure
something
end
RUBY
end
it 'accepts `rescue`/`ensure` after an empty def' do
expect_no_offenses(<<~RUBY)
def foo
rescue
handle_error
ensure
something
end
RUBY
end
it 'does not raise any error with empty braces' do
expect_no_offenses(<<~RUBY)
if cond
()
else
()
end
RUBY
end
it 'registers and corrects on offense for bad indentation' do
expect_offense(<<~RUBY)
if a1
b1
^^^ Use 2 (not 3) spaces for indentation.
b1
elsif a2
b2
^ Use 2 (not 1) spaces for indentation.
else
c
^^^^ Use 2 (not 4) spaces for indentation.
end
RUBY
# The second `b1` will be corrected by IndentationConsistency.
expect_correction(<<~RUBY)
if a1
b1
b1
elsif a2
b2
else
c
end
RUBY
end
it 'does not correct in scopes that contain block comments' do
expect_offense(<<~RUBY)
module Foo
# The class has a block comment within, so it's not corrected.
class Bar
^{} Use 2 (not 0) spaces for indentation.
=begin
This is a nice long
comment
which spans a few lines
=end
# The method has no block comment inside,
# but it's within a class that has a block
# comment, so it's not corrected either.
def baz
^{} Use 2 (not 0) spaces for indentation.
do_something
^{} Use 2 (not 0) spaces for indentation.
end
end
end
RUBY
expect_no_corrections
end
it 'does not indent heredoc strings' do
expect_offense(<<~'RUBY')
module Foo
module Bar
^{} Use 2 (not 0) spaces for indentation.
SOMETHING = <<GOO
text
more text
foo
GOO
def baz
do_something("#{x}")
end
end
end
RUBY
expect_correction(<<~'RUBY')
module Foo
module Bar
SOMETHING = <<GOO
text
more text
foo
GOO
def baz
do_something("#{x}")
end
end
end
RUBY
end
it 'indents parenthesized expressions' do
expect_offense(<<~RUBY)
var1 = nil
array_list = []
if var1.attr1 != 0 || array_list.select{ |w|
(w.attr2 == var1.attr2)
^^^^^^^ Use 2 (not 7) spaces for indentation.
}.blank?
array_list << var1
end
RUBY
expect_correction(<<~RUBY)
var1 = nil
array_list = []
if var1.attr1 != 0 || array_list.select{ |w|
(w.attr2 == var1.attr2)
}.blank?
array_list << var1
end
RUBY
end
it 'leaves rescue ; end unchanged' do
expect_no_offenses(<<~RUBY)
if variable
begin
do_something
rescue ; end # consume any exception
end
RUBY
end
it 'leaves block unchanged if block end is not on its own line' do
expect_no_offenses(<<~RUBY)
a_function {
# a comment
result = AObject.find_by_attr(attr) if attr
result || AObject.make(
:attr => attr,
:attr2 => Other.get_value(),
:attr3 => Another.get_value()) }
RUBY
end
it 'handles lines with only whitespace' do
expect_offense(<<~RUBY)
def x
y
^^^^ Use 2 (not 4) spaces for indentation.
rescue
end
RUBY
expect_correction(<<~RUBY)
def x
y
rescue
end
RUBY
end
it 'accepts a one line if statement' do
expect_no_offenses('if cond then func1 else func2 end')
end
it 'accepts a correctly aligned if/elsif/else/end' do
expect_no_offenses(<<~RUBY)
if a1
b1
elsif a2
b2
else
c
end
RUBY
end
it 'accepts a correctly aligned if/elsif/else/end as a method argument' do
expect_no_offenses(<<~RUBY)
foo(
if a1
b1
elsif a2
b2
else
c
end
)
RUBY
end
it 'accepts if/elsif/else/end laid out as a table' do
expect_no_offenses(<<~RUBY)
if @io == $stdout then str << "$stdout"
elsif @io == $stdin then str << "$stdin"
elsif @io == $stderr then str << "$stderr"
else str << @io.class.to_s
end
RUBY
end
it 'accepts if/then/else/end laid out as another table' do
expect_no_offenses(<<~RUBY)
if File.exist?('config.save')
then ConfigTable.load
else ConfigTable.new
end
RUBY
end
it 'accepts an empty if' do
expect_no_offenses(<<~RUBY)
if a
else
end
RUBY
end
context 'with assignment' do
shared_examples 'assignment with if statement' do
context 'and end is aligned with variable' do
it 'accepts an if with end aligned with setter' do
expect_no_offenses(<<~RUBY)
foo.bar = if baz
derp
end
RUBY
end
it 'accepts an if with end aligned with element assignment' do
expect_no_offenses(<<~RUBY)
foo[bar] = if baz
derp
end
RUBY
end
it 'accepts an if with end aligned with variable' do
expect_no_offenses(<<~RUBY)
var = if a
0
end
@var = if a
0
end
$var = if a
0
end
var ||= if a
0
end
var &&= if a
0
end
var -= if a
0
end
VAR = if a
0
end
RUBY
end
it 'accepts an if/else' do
expect_no_offenses(<<~RUBY)
var = if a
0
else
1
end
RUBY
end
it 'accepts an if/else with chaining after the end' do
expect_no_offenses(<<~RUBY)
var = if a
0
else
1
end.abc.join("")
RUBY
end
it 'accepts an if/else with chaining with a block after the end' do
expect_no_offenses(<<~RUBY)
var = if a
0
else
1
end.abc.tap {}
RUBY
end
end
context 'and end is aligned with keyword' do
it 'registers an offense for an if with setter' do
expect_offense(<<~RUBY)
foo.bar = if baz
derp
^^^^^^^^^^^^ Use 2 (not 12) spaces for indentation.
end
RUBY
end
it 'registers an offense for an if with element assignment' do
expect_offense(<<~RUBY)
foo[bar] = if baz
derp
^^^^^^^^^^^^^ Use 2 (not 13) spaces for indentation.
end
RUBY
end
it 'registers an offense for an if' do
expect_offense(<<~RUBY)
var = if a
0
^^^^^^^^ Use 2 (not 8) spaces for indentation.
end
RUBY
end
it 'accepts an if/else in assignment on next line' do
expect_no_offenses(<<~RUBY)
var =
if a
0
else
1
end
RUBY
end
it 'registers an offense for a while' do
expect_offense(<<~RUBY)
var = while a
b
^^^^^^^^ Use 2 (not 8) spaces for indentation.
end
RUBY
end
it 'registers an offense for an until' do
expect_offense(<<~RUBY)
var = until a
b
^^^^^^^^ Use 2 (not 8) spaces for indentation.
end
RUBY
end
end
context 'and end is aligned randomly' do
it 'registers an offense for an if' do
expect_offense(<<~RUBY)
var = if a
0
^^^^^^^^^^ Use 2 (not 10) spaces for indentation.
end
RUBY
end
it 'registers an offense for a while' do
expect_offense(<<~RUBY)
var = while a
b
^^^^^^^^^^ Use 2 (not 10) spaces for indentation.
end
RUBY
end
it 'registers an offense for an until' do
expect_offense(<<~RUBY)
var = until a
b
^^^^^^^^^^ Use 2 (not 10) spaces for indentation.
end
RUBY
end
end
end
context 'when alignment style is variable' do
let(:end_alignment_config) do
{ 'Enabled' => true, 'EnforcedStyleAlignWith' => 'variable' }
end
include_examples 'assignment with if statement'
end
context 'when alignment style is start_of_line' do
let(:end_alignment_config) do
{ 'Enabled' => true, 'EnforcedStyleAlignWith' => 'start_of_line' }
end
include_examples 'assignment with if statement'
end
context 'when alignment style is keyword' do
let(:end_alignment_config) do
{ 'Enabled' => true, 'EnforcedStyleAlignWith' => 'keyword' }
end
context 'and end is aligned with variable' do
it 'registers an offense for an if' do
expect_offense(<<~RUBY)
var = if a
0
^ Use 2 (not -4) spaces for indentation.
end
RUBY
end
it 'registers an offense for a while' do
expect_offense(<<~RUBY)
var = while a
b
^ Use 2 (not -4) spaces for indentation.
end
RUBY
end
it 'registers and corrects bad indentation' do
expect_offense(<<~RUBY)
var = if a
b
^ Use 2 (not -4) spaces for indentation.
end
var = while a
b
^ Use 2 (not -4) spaces for indentation.
end
RUBY
# Not this cop's job to fix the `end`.
expect_correction(<<~RUBY)
var = if a
b
end
var = while a
b
end
RUBY
end
end
context 'and end is aligned with keyword' do
it 'accepts an if in assignment' do
expect_no_offenses(<<~RUBY)
var = if a
0
end
RUBY
end
it 'accepts an if/else in assignment' do
expect_no_offenses(<<~RUBY)
var = if a
0
else
1
end
RUBY
end
it 'accepts an if/else in assignment on next line' do
expect_no_offenses(<<~RUBY)
var =
if a
0
else
1
end
RUBY
end
it 'accepts a while in assignment' do
expect_no_offenses(<<~RUBY)
var = while a
b
end
RUBY
end
it 'accepts an until in assignment' do
expect_no_offenses(<<~RUBY)
var = until a
b
end
RUBY
end
end
end
end
it 'accepts an if/else branches with rescue clauses' do
# Because of how the rescue clauses come out of Parser, these are
# special and need to be tested.
expect_no_offenses(<<~RUBY)
if a
a rescue nil
else
a rescue nil
end
RUBY
end
end
context 'with unless' do
it 'registers an offense for bad indentation of an unless body' do
expect_offense(<<~RUBY)
unless cond
func
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'accepts an empty unless' do
expect_no_offenses(<<~RUBY)
unless a
else
end
RUBY
end
end
context 'with case' do
it 'registers an offense for bad indentation in a case/when body' do
expect_offense(<<~RUBY)
case a
when b
c
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'registers an offense for bad indentation in a case/else body' do
expect_offense(<<~RUBY)
case a
when b
c
when d
e
else
f
^^^ Use 2 (not 3) spaces for indentation.
end
RUBY
end
it 'accepts correctly indented case/when/else' do
expect_no_offenses(<<~RUBY)
case a
when b
c
c
when d
else
f
end
RUBY
end
it 'accepts aligned values in when clause' do
expect_no_offenses(<<~'RUBY')
case superclass
when /\A(#{NAMESPACEMATCH})(?:\s|\Z)/,
/\A(Struct|OStruct)\.new/,
/\ADelegateClass\((.+?)\)\s*\Z/,
/\A(#{NAMESPACEMATCH})\(/
$1
when "self"
namespace.path
end
RUBY
end
it 'accepts case/when/else laid out as a table' do
expect_no_offenses(<<~RUBY)
case sexp.loc.keyword.source
when 'if' then cond, body, _else = *sexp
when 'unless' then cond, _else, body = *sexp
else cond, body = *sexp
end
RUBY
end
it 'accepts case/when/else with then beginning a line' do
expect_no_offenses(<<~RUBY)
case sexp.loc.keyword.source
when 'if'
then cond, body, _else = *sexp
end
RUBY
end
it 'accepts indented when/else plus indented body' do
# "Indent when as deep as case" is the job of another cop.
expect_no_offenses(<<~RUBY)
case code_type
when 'ruby', 'sql', 'plain'
code_type
when 'erb'
'ruby; html-script: true'
when "html"
'xml'
else
'plain'
end
RUBY
end
end
context 'with case match', :ruby27 do
it 'registers an offense for bad indentation in a case/in body' do
expect_offense(<<~RUBY)
case a
in b
c
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'registers an offense for bad indentation in a case/else body' do
expect_offense(<<~RUBY)
case a
in b
c
in d
e
else
f
^^^ Use 2 (not 3) spaces for indentation.
end
RUBY
end
it 'accepts correctly indented case/in/else' do
expect_no_offenses(<<~RUBY)
case a
in b
c
c
in d
else
f
end
RUBY
end
it 'accepts aligned values in `in` clause' do
expect_no_offenses(<<~'RUBY')
case condition
in [42]
foo
in [43]
bar
end
RUBY
end
it 'accepts case/in/else laid out as a table' do
expect_no_offenses(<<~RUBY)
case sexp.loc.keyword.source
in 'if' then cond, body, _else = *sexp
in 'unless' then cond, _else, body = *sexp
else cond, body = *sexp
end
RUBY
end
it 'accepts case/in/else with then beginning a line' do
expect_no_offenses(<<~RUBY)
case sexp.loc.keyword.source
in 'if'
then cond, body, _else = *sexp
end
RUBY
end
it 'accepts indented in/else plus indented body' do
# "Indent `in` as deep as `case`" is the job of another cop.
expect_no_offenses(<<~RUBY)
case code_type
in 'ruby' | 'sql' | 'plain'
code_type
in 'erb'
'ruby; html-script: true'
in "html"
'xml'
else
'plain'
end
RUBY
end
end
context 'with while/until' do
it 'registers an offense for bad indentation of a while body' do
expect_offense(<<~RUBY)
while cond
func
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'registers an offense for bad indentation of begin/end/while' do
expect_offense(<<~RUBY)
something = begin
func1
^ Use 2 (not 1) spaces for indentation.
func2
end while cond
RUBY
end
it 'registers an offense for bad indentation of an until body' do
expect_offense(<<~RUBY)
until cond
func
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end
it 'accepts an empty while' do
expect_no_offenses(<<~RUBY)
while a
end
RUBY
end
end
context 'with for' do
it 'registers an offense for bad indentation of a for body' do
expect_offense(<<~RUBY)
for var in 1..10
func
^ Use 2 (not 1) spaces for indentation.
end
RUBY
end