forked from rubocop/rubocop
/
method_call_with_args_parentheses_spec.rb
968 lines (814 loc) · 24.1 KB
/
method_call_with_args_parentheses_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::MethodCallWithArgsParentheses, :config do
shared_examples 'endless methods' do |omit: false|
context 'endless methods', :ruby30 do
context 'with arguments' do
it 'requires method calls to have parens' do
expect_no_offenses(<<~RUBY)
def x() = foo("bar")
RUBY
end
end
context 'without arguments' do
if omit
it 'registers an offense when there are parens' do
expect_offense(<<~RUBY)
def x() = foo()
^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
def x() = foo#{trailing_whitespace}
RUBY
end
else
it 'does not register an offense when there are parens' do
expect_no_offenses(<<~RUBY)
def x() = foo()
RUBY
end
end
it 'does not register an offense when there are no parens' do
expect_no_offenses(<<~RUBY)
def x() = foo
RUBY
end
end
end
end
context 'when EnforcedStyle is require_parentheses (default)' do
it_behaves_like 'endless methods'
it 'accepts no parens in method call without args' do
expect_no_offenses('top.test')
end
it 'accepts parens in method call with args' do
expect_no_offenses('top.test(a, b)')
end
it 'accepts parens in method call with do-end blocks' do
expect_no_offenses(<<~RUBY)
foo(:arg) do
bar
end
RUBY
end
it 'register an offense for method call without parens' do
expect_offense(<<~RUBY)
top.test a, b
^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
top.test(a, b)
RUBY
end
context 'when using safe navigation operator' do
it 'register an offense for method call without parens' do
expect_offense(<<~RUBY)
top&.test a, b
^^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
top&.test(a, b)
RUBY
end
end
it 'register an offense for non-receiver method call without parens' do
expect_offense(<<~RUBY)
def foo
test a, b
^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
test(a, b)
end
RUBY
end
it 'register an offense for methods starting with capital without parens' do
expect_offense(<<~RUBY)
def foo
Test a, b
^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
Test(a, b)
end
RUBY
end
it 'register an offense for superclass call without parens' do
expect_offense(<<~RUBY)
def foo
super a
^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
super(a)
end
RUBY
end
it 'register no offense for superclass call without args' do
expect_no_offenses('super')
end
it 'register no offense for yield without args' do
expect_no_offenses('yield')
end
it 'register no offense for superclass call with parens' do
expect_no_offenses('super(a)')
end
it 'register an offense for yield without parens' do
expect_offense(<<~RUBY)
def foo
yield a
^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
yield(a)
end
RUBY
end
it 'accepts no parens for operators' do
expect_no_offenses('top.test + a')
end
it 'accepts no parens for setter methods' do
expect_no_offenses('top.test = a')
end
it 'accepts no parens for unary operators' do
expect_no_offenses('!test')
end
it 'auto-corrects fully parenthesized args by removing space' do
expect_offense(<<~RUBY)
top.eq (1 + 2)
^^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
top.eq(1 + 2)
RUBY
end
it 'auto-corrects parenthesized args for local methods by removing space' do
expect_offense(<<~RUBY)
def foo
eq (1 + 2)
^^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
eq(1 + 2)
end
RUBY
end
it 'auto-corrects call with multiple args by adding braces' do
expect_offense(<<~RUBY)
def foo
eq 1, (2 + 3)
^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
eq 1, 2, 3
^^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
eq(1, (2 + 3))
eq(1, 2, 3)
end
RUBY
end
it 'auto-corrects partially parenthesized args by adding needed braces' do
expect_offense(<<~RUBY)
top.eq (1 + 2) + 3
^^^^^^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
top.eq((1 + 2) + 3)
RUBY
end
it 'auto-corrects calls with multiple args by adding needed braces' do
expect_offense(<<~RUBY)
top.eq (1 + 2), 3
^^^^^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
top.eq((1 + 2), 3)
RUBY
end
it 'auto-corrects calls where arg is method call' do
expect_offense(<<~RUBY)
def my_method
foo bar.baz(abc, xyz)
^^^^^^^^^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def my_method
foo(bar.baz(abc, xyz))
end
RUBY
end
it 'auto-corrects calls where multiple args are method calls' do
expect_offense(<<~RUBY)
def my_method
foo bar.baz(abc, xyz), foo(baz)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def my_method
foo(bar.baz(abc, xyz), foo(baz))
end
RUBY
end
it 'auto-corrects calls where the argument node is a constant' do
expect_offense(<<~RUBY)
def my_method
raise NotImplementedError
^^^^^^^^^^^^^^^^^^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def my_method
raise(NotImplementedError)
end
RUBY
end
it 'auto-corrects calls where the argument node is a number' do
expect_offense(<<~RUBY)
def my_method
sleep 1
^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def my_method
sleep(1)
end
RUBY
end
context 'with IgnoredMethods' do
context 'with a string' do
let(:cop_config) { { 'IgnoredMethods' => %w[puts] } }
it 'ignores method listed in IgnoredMethods' do
expect_no_offenses('puts :test')
end
end
context 'with a regex' do
let(:cop_config) { { 'IgnoredMethods' => [/puts/] } }
it 'ignores method listed in IgnoredMethods' do
expect_no_offenses('puts :test')
end
end
end
context 'when inspecting macro methods' do
let(:cop_config) do
{ 'IgnoreMacros' => 'true' }
end
context 'in a class body' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class Foo
bar :baz
end
RUBY
end
end
context 'in a module body' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
module Foo
bar :baz
end
RUBY
end
end
end
context 'IgnoredPatterns' do
let(:cop_config) do
{ 'IgnoredPatterns' => %w[^assert ^refute] }
end
it 'ignored methods listed in IgnoredPatterns' do
expect_no_offenses('assert 2 == 2')
expect_no_offenses('assert_equal 2, 2')
expect_no_offenses('assert_match /^yes/i, result')
expect_no_offenses('refute 2 == 3')
expect_no_offenses('refute_equal 2, 3')
expect_no_offenses('refute_match /^no/i, result')
end
end
end
context 'when EnforcedStyle is omit_parentheses' do
let(:cop_config) do
{ 'EnforcedStyle' => 'omit_parentheses' }
end
it_behaves_like 'endless methods', omit: true
context 'forwarded arguments in 2.7', :ruby27 do
it 'accepts parens for forwarded arguments' do
expect_no_offenses(<<~RUBY)
def delegated_call(...)
@proxy.call(...)
end
RUBY
end
end
context 'forwarded arguments in 3.0', :ruby30 do
it 'accepts parens for forwarded arguments' do
expect_no_offenses(<<~RUBY)
def method_missing(name, ...)
@proxy.call(name, ...)
end
RUBY
end
end
it 'register an offense for parens in method call without args' do
trailing_whitespace = ' '
expect_offense(<<~RUBY)
top.test()
^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
top.test#{trailing_whitespace}
RUBY
end
it 'register an offense for multi-line method calls' do
expect_offense(<<~RUBY)
test(
^ Omit parentheses for method calls with arguments.
foo: bar
)
RUBY
expect_correction(<<~RUBY)
test \\
foo: bar
RUBY
end
it 'register an offense for superclass call with parens' do
expect_offense(<<~RUBY)
def foo
super(a)
^^^ Omit parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
super a
end
RUBY
end
it 'register an offense for yield call with parens' do
expect_offense(<<~RUBY)
def foo
yield(a)
^^^ Omit parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
def foo
yield a
end
RUBY
end
it 'register an offense for parens in the last chain' do
expect_offense(<<~RUBY)
foo().bar(3).wait(4)
^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
foo().bar(3).wait 4
RUBY
end
it 'register an offense for parens in do-end blocks' do
expect_offense(<<~RUBY)
foo(:arg) do
^^^^^^ Omit parentheses for method calls with arguments.
bar
end
RUBY
expect_correction(<<~RUBY)
foo :arg do
bar
end
RUBY
end
it 'register an offense for hashes in keyword values' do
expect_offense(<<~RUBY)
method_call(hash: {foo: :bar})
^^^^^^^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
method_call hash: {foo: :bar}
RUBY
end
it 'register an offense for %r regex literal as arguments' do
expect_offense(<<~RUBY)
method_call(%r{foo})
^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
method_call %r{foo}
RUBY
end
it 'register an offense for parens in string interpolation' do
expect_offense(<<~'RUBY')
"#{t('no.parens')}"
^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~'RUBY')
"#{t 'no.parens'}"
RUBY
end
it 'register an offense in complex conditionals' do
expect_offense(<<~RUBY)
def foo
if cond.present? && verify?(:something)
h.do_with(kw: value)
^^^^^^^^^^^ Omit parentheses for method calls with arguments.
elsif cond.present? || verify?(:something_else)
h.do_with(kw: value)
^^^^^^^^^^^ Omit parentheses for method calls with arguments.
elsif whatevs?
h.do_with(kw: value)
^^^^^^^^^^^ Omit parentheses for method calls with arguments.
end
end
RUBY
expect_correction(<<~RUBY)
def foo
if cond.present? && verify?(:something)
h.do_with kw: value
elsif cond.present? || verify?(:something_else)
h.do_with kw: value
elsif whatevs?
h.do_with kw: value
end
end
RUBY
end
it 'register an offense in assignments' do
expect_offense(<<~RUBY)
foo = A::B.new(c)
^^^ Omit parentheses for method calls with arguments.
bar.foo = A::B.new(c)
^^^ Omit parentheses for method calls with arguments.
bar.foo(42).quux = A::B.new(c)
^^^ Omit parentheses for method calls with arguments.
bar.foo(42).quux &&= A::B.new(c)
^^^ Omit parentheses for method calls with arguments.
bar.foo(42).quux += A::B.new(c)
^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
foo = A::B.new c
bar.foo = A::B.new c
bar.foo(42).quux = A::B.new c
bar.foo(42).quux &&= A::B.new c
bar.foo(42).quux += A::B.new c
RUBY
end
it 'register an offense for camel-case methods with arguments' do
expect_offense(<<~RUBY)
Array(:arg)
^^^^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
Array :arg
RUBY
end
it 'register an offense in multi-line inheritance' do
expect_offense(<<~RUBY)
class Point < Struct.new(:x, :y)
^^^^^^^^ Omit parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
class Point < Struct.new :x, :y
end
RUBY
end
it 'accepts parens in single-line inheritance' do
expect_no_offenses(<<-RUBY)
class Point < Struct.new(:x, :y); end
RUBY
end
it 'accepts no parens in method call without args' do
expect_no_offenses('top.test')
end
it 'accepts no parens in method call with args' do
expect_no_offenses('top.test 1, 2, foo: bar')
end
it 'accepts parens in default argument value calls' do
expect_no_offenses(<<~RUBY)
def regular(arg = default(42))
nil
end
def seatle_style arg = default(42)
nil
end
RUBY
end
it 'accepts parens in default keyword argument value calls' do
expect_no_offenses(<<~RUBY)
def regular(arg: default(42))
nil
end
def seatle_style arg: default(42)
nil
end
RUBY
end
it 'accepts parens in method args' do
expect_no_offenses('top.test 1, 2, foo: bar(3)')
end
it 'accepts parens in nested method args' do
expect_no_offenses('top.test 1, 2, foo: [bar(3)]')
end
it 'accepts parens in calls with hash as arg' do
expect_no_offenses('top.test({foo: :bar})')
expect_no_offenses('top.test({foo: :bar}.merge(baz: :maz))')
expect_no_offenses('top.test(:first, {foo: :bar}.merge(baz: :maz))')
end
it 'accepts special lambda call syntax' do
expect_no_offenses('thing.()')
end
it 'accepts parens in chained method calls' do
expect_no_offenses('foo().bar(3).wait(4).it')
end
it 'accepts parens in chaining with operators' do
expect_no_offenses('foo().bar(3).wait(4) + 4')
end
it 'accepts parens in blocks with braces' do
expect_no_offenses('foo(1) { 2 }')
end
it 'accepts parens in calls with logical operators' do
expect_no_offenses('foo(a) && bar(b)')
expect_no_offenses('foo(a) || bar(b)')
expect_no_offenses(<<~RUBY)
foo(a) || bar(b) do
pass
end
RUBY
end
it 'accepts parens in calls with args with logical operators' do
expect_no_offenses('foo(a, b || c)')
expect_no_offenses('foo a, b || c')
expect_no_offenses('foo a, b(1) || c(2, d(3))')
end
it 'accepts parens in literals with unary operators as first argument' do
expect_no_offenses('foo(-1)')
expect_no_offenses('foo(+1)')
expect_no_offenses('foo(+"")')
expect_no_offenses('foo(-"")')
end
it 'accepts parens in args splat' do
expect_no_offenses('foo(*args)')
expect_no_offenses('foo *args')
expect_no_offenses('foo(**kwargs)')
expect_no_offenses('foo **kwargs')
end
it 'accepts parens in slash regexp literal as argument' do
expect_no_offenses('foo(/regexp/)')
end
it 'accepts parens in argument calls with braced blocks' do
expect_no_offenses('foo(bar(:arg) { 42 })')
end
it 'accepts parens in implicit #to_proc' do
expect_no_offenses('foo(&block)')
expect_no_offenses('foo &block')
end
it 'accepts parens in yield argument method calls' do
expect_no_offenses('yield File.basepath(path)')
expect_no_offenses('yield path, File.basepath(path)')
end
it 'accepts parens in super without args' do
expect_no_offenses('super()')
end
it 'accepts parens in super method calls as arguments' do
expect_no_offenses('super foo(bar)')
end
it 'accepts parens in super calls with braced blocks' do
expect_no_offenses('super(foo(bar)) { yield }')
end
it 'accepts parens in camel case method without args' do
expect_no_offenses('Array()')
end
it 'accepts parens in ternary condition calls' do
expect_no_offenses(<<-RUBY)
foo.include?(bar) ? bar : quux
RUBY
end
it 'accepts parens in args with ternary conditions' do
expect_no_offenses(<<-RUBY)
foo.include?(bar ? baz : quux)
RUBY
end
it 'accepts parens in splat calls' do
expect_no_offenses(<<-RUBY)
foo(*bar(args))
foo(**quux(args))
RUBY
end
it 'accepts parens in block passing calls' do
expect_no_offenses(<<-RUBY)
foo(&method(:args))
RUBY
end
it 'accepts parens in range literals' do
expect_no_offenses(<<-RUBY)
1..limit(n)
1...limit(n)
RUBY
end
it 'auto-corrects single-line calls' do
expect_offense(<<~RUBY)
top.test(1, 2, foo: bar(3))
^^^^^^^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
top.test 1, 2, foo: bar(3)
RUBY
end
it 'auto-corrects multi-line calls with trailing whitespace' do
trailing_whitespace = ' '
expect_offense(<<~RUBY)
foo(#{trailing_whitespace}
^^ Omit parentheses for method calls with arguments.
bar: 3
)
RUBY
expect_correction(<<~RUBY)
foo \\#{trailing_whitespace}
bar: 3
RUBY
end
it 'auto-corrects complex multi-line calls' do
expect_offense(<<~RUBY)
foo(arg,
^^^^^ Omit parentheses for method calls with arguments.
option: true
)
RUBY
expect_correction(<<~RUBY)
foo arg,
option: true
RUBY
end
it 'accepts parens in chaining with safe operators' do
expect_no_offenses('Something.find(criteria: given)&.field')
end
it 'accepts parens in operator method calls' do
expect_no_offenses(<<~RUBY)
data.[](value)
data&.[](value)
string.<<(even_more_string)
ruby.==(good)
ruby&.===(better)
RUBY
end
context 'allowing parenthesis in chaining' do
let(:cop_config) do
{
'EnforcedStyle' => 'omit_parentheses',
'AllowParenthesesInChaining' => true
}
end
it 'register offense for single-line chaining without previous parens' do
expect_offense(<<~RUBY)
Rails.convoluted.example.logger.error("something")
^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
Rails.convoluted.example.logger.error "something"
RUBY
end
it 'register offense for multi-line chaining without previous parens' do
expect_offense(<<~RUBY)
Rails
.convoluted
.example
.logger
.error("something")
^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.
RUBY
expect_correction(<<~RUBY)
Rails
.convoluted
.example
.logger
.error "something"
RUBY
end
it 'accepts no parens in the last call if previous calls with parens' do
expect_no_offenses('foo().bar(3).wait 4')
end
it 'accepts parens in the last call if any previous calls with parentheses' do
expect_no_offenses('foo().bar(3).quux.wait(4)')
end
end
context 'allowing parens in multi-line calls' do
let(:cop_config) do
{
'EnforcedStyle' => 'omit_parentheses',
'AllowParenthesesInMultilineCall' => true
}
end
it 'accepts parens for multi-line calls' do
expect_no_offenses(<<~RUBY)
test(
foo: bar
)
RUBY
end
end
context 'allowing parens in camel-case methods' do
let(:cop_config) do
{
'EnforcedStyle' => 'omit_parentheses',
'AllowParenthesesInCamelCaseMethod' => true
}
end
it 'accepts parens for camel-case method names' do
expect_no_offenses('Array(nil)')
end
end
end
context 'allowing parens in string interpolation' do
let(:cop_config) do
{
'EnforcedStyle' => 'omit_parentheses',
'AllowParenthesesInStringInterpolation' => true
}
end
it 'accepts parens for camel-case method names' do
expect_no_offenses(<<~'RUBY')
"#{t('this.is.good')}"
"#{t 'this.is.also.good'}"
RUBY
end
end
context 'when inspecting macro methods with IncludedMacros' do
let(:cop_config) do
{
'IgnoreMacros' => 'true',
'IncludedMacros' => ['bar']
}
end
it_behaves_like 'endless methods'
context 'in a class body' do
it 'finds offense' do
expect_offense(<<~RUBY)
class Foo
bar :abc
^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
class Foo
bar(:abc)
end
RUBY
end
end
context 'in a module body' do
it 'finds offense' do
expect_offense(<<~RUBY)
module Foo
bar :abc
^^^^^^^^ Use parentheses for method calls with arguments.
end
RUBY
expect_correction(<<~RUBY)
module Foo
bar(:abc)
end
RUBY
end
end
context 'for a macro not on the included list' do
it 'allows' do
expect_no_offenses(<<~RUBY)
module Foo
baz :abc
end
RUBY
end
end
context 'for a macro in both IncludedMacros and IgnoredMethods' do
let(:cop_config) do
{
'IgnoreMacros' => 'true',
'IncludedMacros' => ['bar'],
'IgnoredMethods' => ['bar']
}
end
it 'allows' do
expect_no_offenses(<<~RUBY)
module Foo
bar :abc
end
RUBY
end
end
end
end