rubocop-minitest 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  == Minitest/AssertEmpty
4
4
 
5
5
  |===
6
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
6
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
7
7
 
8
8
  | Enabled
9
9
  | Yes
@@ -35,17 +35,17 @@ assert_empty(object, 'message')
35
35
  == Minitest/AssertEmptyLiteral
36
36
 
37
37
  |===
38
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
38
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
39
39
 
40
40
  | Enabled
41
41
  | Yes
42
- | Yes (Unsafe)
42
+ | Yes
43
43
  | 0.5
44
44
  | 0.11
45
45
  |===
46
46
 
47
47
  This cop enforces the test to use `assert_empty`
48
- instead of using `assert_equal([], object)`.
48
+ instead of using `assert_equal([], object)` or `assert_equal({}, object)`.
49
49
 
50
50
  === Examples
51
51
 
@@ -62,7 +62,7 @@ assert_empty(object)
62
62
  == Minitest/AssertEqual
63
63
 
64
64
  |===
65
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
65
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
66
66
 
67
67
  | Enabled
68
68
  | Yes
@@ -92,7 +92,7 @@ assert_equal("rubocop-minitest", actual)
92
92
  == Minitest/AssertInDelta
93
93
 
94
94
  |===
95
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
95
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
96
96
 
97
97
  | Pending
98
98
  | Yes
@@ -124,7 +124,7 @@ assert_in_delta(0.2, actual, 0.001, 'message')
124
124
  == Minitest/AssertIncludes
125
125
 
126
126
  |===
127
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
127
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
128
128
 
129
129
  | Enabled
130
130
  | Yes
@@ -156,7 +156,7 @@ assert_includes(collection, object, 'message')
156
156
  == Minitest/AssertInstanceOf
157
157
 
158
158
  |===
159
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
159
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
160
160
 
161
161
  | Enabled
162
162
  | Yes
@@ -188,7 +188,7 @@ assert_instance_of(Class, object, 'message')
188
188
  == Minitest/AssertKindOf
189
189
 
190
190
  |===
191
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
191
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
192
192
 
193
193
  | Pending
194
194
  | Yes
@@ -220,7 +220,7 @@ assert_kind_of(Class, object, 'message')
220
220
  == Minitest/AssertMatch
221
221
 
222
222
  |===
223
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
223
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
224
224
 
225
225
  | Enabled
226
226
  | Yes
@@ -252,7 +252,7 @@ assert_match(matcher, string, 'message')
252
252
  == Minitest/AssertNil
253
253
 
254
254
  |===
255
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
255
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
256
256
 
257
257
  | Enabled
258
258
  | Yes
@@ -286,7 +286,7 @@ assert_nil(actual, 'message')
286
286
  == Minitest/AssertOutput
287
287
 
288
288
  |===
289
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
289
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
290
290
 
291
291
  | Pending
292
292
  | Yes
@@ -318,7 +318,7 @@ assert_output(expected) { puts object.method }
318
318
  == Minitest/AssertPathExists
319
319
 
320
320
  |===
321
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
321
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
322
322
 
323
323
  | Pending
324
324
  | Yes
@@ -350,7 +350,7 @@ assert_path_exists(path, 'message')
350
350
  == Minitest/AssertRespondTo
351
351
 
352
352
  |===
353
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
353
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
354
354
 
355
355
  | Enabled
356
356
  | Yes
@@ -384,7 +384,7 @@ assert_respond_to(self, :do_something)
384
384
  == Minitest/AssertSilent
385
385
 
386
386
  |===
387
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
387
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
388
388
 
389
389
  | Pending
390
390
  | Yes
@@ -414,7 +414,7 @@ assert_silent { puts object.do_something }
414
414
  == Minitest/AssertTruthy
415
415
 
416
416
  |===
417
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
417
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
418
418
 
419
419
  | Enabled
420
420
  | Yes
@@ -446,7 +446,7 @@ assert(actual, 'message')
446
446
  == Minitest/AssertWithExpectedArgument
447
447
 
448
448
  |===
449
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
449
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
450
450
 
451
451
  | Pending
452
452
  | No
@@ -458,7 +458,9 @@ assert(actual, 'message')
458
458
  This cop tries to detect when a user accidentally used
459
459
  `assert` when they meant to use `assert_equal`.
460
460
 
461
- It is marked as unsafe because it is not possible to determine
461
+ === Safety
462
+
463
+ This cop is unsafe because it is not possible to determine
462
464
  whether the second argument of `assert` is a message or not.
463
465
 
464
466
  === Examples
@@ -478,7 +480,7 @@ assert(foo, 'message')
478
480
  == Minitest/AssertionInLifecycleHook
479
481
 
480
482
  |===
481
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
483
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
482
484
 
483
485
  | Pending
484
486
  | Yes
@@ -511,13 +513,13 @@ end
511
513
  == Minitest/GlobalExpectations
512
514
 
513
515
  |===
514
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
516
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
515
517
 
516
518
  | Enabled
517
519
  | Yes
518
520
  | Yes
519
521
  | 0.7
520
- | -
522
+ | 0.16
521
523
  |===
522
524
 
523
525
  This cop checks for deprecated global expectations
@@ -525,6 +527,54 @@ and autocorrects them to use expect format.
525
527
 
526
528
  === Examples
527
529
 
530
+ ==== EnforcedStyle: _
531
+
532
+ [source,ruby]
533
+ ----
534
+ # bad
535
+ musts.must_equal expected_musts
536
+ wonts.wont_match expected_wonts
537
+ musts.must_raise TypeError
538
+
539
+ expect(musts).must_equal expected_musts
540
+ expect(wonts).wont_match expected_wonts
541
+ expect { musts }.must_raise TypeError
542
+
543
+ value(musts).must_equal expected_musts
544
+ value(wonts).wont_match expected_wonts
545
+ value { musts }.must_raise TypeError
546
+
547
+ # good
548
+ _(musts).must_equal expected_musts
549
+ _(wonts).wont_match expected_wonts
550
+ _ { musts }.must_raise TypeError
551
+ ----
552
+
553
+ ==== EnforcedStyle: any (default)
554
+
555
+ [source,ruby]
556
+ ----
557
+ # bad
558
+ musts.must_equal expected_musts
559
+ wonts.wont_match expected_wonts
560
+ musts.must_raise TypeError
561
+
562
+ # good
563
+ _(musts).must_equal expected_musts
564
+ _(wonts).wont_match expected_wonts
565
+ _ { musts }.must_raise TypeError
566
+
567
+ expect(musts).must_equal expected_musts
568
+ expect(wonts).wont_match expected_wonts
569
+ expect { musts }.must_raise TypeError
570
+
571
+ value(musts).must_equal expected_musts
572
+ value(wonts).wont_match expected_wonts
573
+ value { musts }.must_raise TypeError
574
+ ----
575
+
576
+ ==== EnforcedStyle: expect
577
+
528
578
  [source,ruby]
529
579
  ----
530
580
  # bad
@@ -532,12 +582,57 @@ musts.must_equal expected_musts
532
582
  wonts.wont_match expected_wonts
533
583
  musts.must_raise TypeError
534
584
 
585
+ _(musts).must_equal expected_musts
586
+ _(wonts).wont_match expected_wonts
587
+ _ { musts }.must_raise TypeError
588
+
589
+ value(musts).must_equal expected_musts
590
+ value(wonts).wont_match expected_wonts
591
+ value { musts }.must_raise TypeError
592
+
535
593
  # good
594
+ expect(musts).must_equal expected_musts
595
+ expect(wonts).wont_match expected_wonts
596
+ expect { musts }.must_raise TypeError
597
+ ----
598
+
599
+ ==== EnforcedStyle: value
600
+
601
+ [source,ruby]
602
+ ----
603
+ # bad
604
+ musts.must_equal expected_musts
605
+ wonts.wont_match expected_wonts
606
+ musts.must_raise TypeError
607
+
536
608
  _(musts).must_equal expected_musts
537
609
  _(wonts).wont_match expected_wonts
538
610
  _ { musts }.must_raise TypeError
611
+
612
+ expect(musts).must_equal expected_musts
613
+ expect(wonts).wont_match expected_wonts
614
+ expect { musts }.must_raise TypeError
615
+
616
+ # good
617
+ value(musts).must_equal expected_musts
618
+ value(wonts).wont_match expected_wonts
619
+ value { musts }.must_raise TypeError
539
620
  ----
540
621
 
622
+ === Configurable attributes
623
+
624
+ |===
625
+ | Name | Default value | Configurable values
626
+
627
+ | EnforcedStyle
628
+ | `any`
629
+ | `_`, `any`, `expect`, `value`
630
+
631
+ | Include
632
+ | `+**/test/**/*+`, `+**/*_test.rb+`, `+**/spec/**/*+`, `+**/*_spec.rb+`
633
+ | Array
634
+ |===
635
+
541
636
  === References
542
637
 
543
638
  * https://minitest.rubystyle.guide#global-expectations
@@ -545,7 +640,7 @@ _ { musts }.must_raise TypeError
545
640
  == Minitest/LiteralAsActualArgument
546
641
 
547
642
  |===
548
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
643
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
549
644
 
550
645
  | Pending
551
646
  | Yes
@@ -579,7 +674,7 @@ assert_equal [1, 2], foo, 'message'
579
674
  == Minitest/MultipleAssertions
580
675
 
581
676
  |===
582
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
677
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
583
678
 
584
679
  | Pending
585
680
  | Yes
@@ -630,7 +725,7 @@ end
630
725
  == Minitest/NoAssertions
631
726
 
632
727
  |===
633
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
728
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
634
729
 
635
730
  | Disabled
636
731
  | Yes
@@ -662,7 +757,7 @@ end
662
757
  == Minitest/RefuteEmpty
663
758
 
664
759
  |===
665
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
760
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
666
761
 
667
762
  | Enabled
668
763
  | Yes
@@ -694,7 +789,7 @@ refute_empty(object, 'message')
694
789
  == Minitest/RefuteEqual
695
790
 
696
791
  |===
697
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
792
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
698
793
 
699
794
  | Enabled
700
795
  | Yes
@@ -725,7 +820,7 @@ refute_equal("rubocop-minitest", actual)
725
820
  == Minitest/RefuteFalse
726
821
 
727
822
  |===
728
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
823
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
729
824
 
730
825
  | Enabled
731
826
  | Yes
@@ -760,7 +855,7 @@ refute(actual, 'message')
760
855
  == Minitest/RefuteInDelta
761
856
 
762
857
  |===
763
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
858
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
764
859
 
765
860
  | Pending
766
861
  | Yes
@@ -792,7 +887,7 @@ refute_in_delta(0.2, actual, 0.001, 'message')
792
887
  == Minitest/RefuteIncludes
793
888
 
794
889
  |===
795
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
890
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
796
891
 
797
892
  | Enabled
798
893
  | Yes
@@ -824,7 +919,7 @@ refute_includes(collection, object, 'message')
824
919
  == Minitest/RefuteInstanceOf
825
920
 
826
921
  |===
827
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
922
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
828
923
 
829
924
  | Enabled
830
925
  | Yes
@@ -856,7 +951,7 @@ refute_instance_of(Class, object, 'message')
856
951
  == Minitest/RefuteKindOf
857
952
 
858
953
  |===
859
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
954
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
860
955
 
861
956
  | Pending
862
957
  | Yes
@@ -888,7 +983,7 @@ refute_kind_of(Class, object, 'message')
888
983
  == Minitest/RefuteMatch
889
984
 
890
985
  |===
891
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
986
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
892
987
 
893
988
  | Enabled
894
989
  | Yes
@@ -920,7 +1015,7 @@ refute_match(matcher, string, 'message')
920
1015
  == Minitest/RefuteNil
921
1016
 
922
1017
  |===
923
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
1018
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
924
1019
 
925
1020
  | Enabled
926
1021
  | Yes
@@ -954,7 +1049,7 @@ refute_nil(actual, 'message')
954
1049
  == Minitest/RefutePathExists
955
1050
 
956
1051
  |===
957
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
1052
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
958
1053
 
959
1054
  | Pending
960
1055
  | Yes
@@ -986,7 +1081,7 @@ refute_path_exists(path, 'message')
986
1081
  == Minitest/RefuteRespondTo
987
1082
 
988
1083
  |===
989
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
1084
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
990
1085
 
991
1086
  | Enabled
992
1087
  | Yes
@@ -1020,7 +1115,7 @@ refute_respond_to(self, :do_something)
1020
1115
  == Minitest/TestMethodName
1021
1116
 
1022
1117
  |===
1023
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
1118
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
1024
1119
 
1025
1120
  | Pending
1026
1121
  | Yes
@@ -1060,7 +1155,7 @@ end
1060
1155
  == Minitest/UnreachableAssertion
1061
1156
 
1062
1157
  |===
1063
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
1158
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
1064
1159
 
1065
1160
  | Pending
1066
1161
  | Yes
@@ -1092,7 +1187,7 @@ assert_equal('foo', obj.bar)
1092
1187
  == Minitest/UnspecifiedException
1093
1188
 
1094
1189
  |===
1095
- | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
1190
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
1096
1191
 
1097
1192
  | Pending
1098
1193
  | Yes
@@ -19,6 +19,18 @@ module RuboCop
19
19
  extend MinitestCopRule
20
20
 
21
21
  define_rule :assert, target_method: :empty?
22
+
23
+ remove_method :on_send
24
+ def on_send(node)
25
+ return unless node.method?(:assert)
26
+ return unless (arguments = peel_redundant_parentheses_from(node.arguments))
27
+ return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:empty?)
28
+ return unless arguments.first.arguments.empty?
29
+
30
+ add_offense(node, message: offense_message(arguments)) do |corrector|
31
+ autocorrect(corrector, node, arguments)
32
+ end
33
+ end
22
34
  end
23
35
  end
24
36
  end
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
6
  # This cop enforces the test to use `assert_empty`
7
- # instead of using `assert_equal([], object)`.
7
+ # instead of using `assert_equal([], object)` or `assert_equal({}, object)`.
8
8
  #
9
9
  # @example
10
10
  # # bad
@@ -6,8 +6,9 @@ module RuboCop
6
6
  # This cop tries to detect when a user accidentally used
7
7
  # `assert` when they meant to use `assert_equal`.
8
8
  #
9
- # It is marked as unsafe because it is not possible to determine
10
- # whether the second argument of `assert` is a message or not.
9
+ # @safety
10
+ # This cop is unsafe because it is not possible to determine
11
+ # whether the second argument of `assert` is a message or not.
11
12
  #
12
13
  # @example
13
14
  # # bad
@@ -6,17 +6,83 @@ module RuboCop
6
6
  # This cop checks for deprecated global expectations
7
7
  # and autocorrects them to use expect format.
8
8
  #
9
- # @example
9
+ # @example EnforcedStyle: _
10
10
  # # bad
11
11
  # musts.must_equal expected_musts
12
12
  # wonts.wont_match expected_wonts
13
13
  # musts.must_raise TypeError
14
14
  #
15
+ # expect(musts).must_equal expected_musts
16
+ # expect(wonts).wont_match expected_wonts
17
+ # expect { musts }.must_raise TypeError
18
+ #
19
+ # value(musts).must_equal expected_musts
20
+ # value(wonts).wont_match expected_wonts
21
+ # value { musts }.must_raise TypeError
22
+ #
15
23
  # # good
16
24
  # _(musts).must_equal expected_musts
17
25
  # _(wonts).wont_match expected_wonts
18
26
  # _ { musts }.must_raise TypeError
27
+ #
28
+ # @example EnforcedStyle: any (default)
29
+ # # bad
30
+ # musts.must_equal expected_musts
31
+ # wonts.wont_match expected_wonts
32
+ # musts.must_raise TypeError
33
+ #
34
+ # # good
35
+ # _(musts).must_equal expected_musts
36
+ # _(wonts).wont_match expected_wonts
37
+ # _ { musts }.must_raise TypeError
38
+ #
39
+ # expect(musts).must_equal expected_musts
40
+ # expect(wonts).wont_match expected_wonts
41
+ # expect { musts }.must_raise TypeError
42
+ #
43
+ # value(musts).must_equal expected_musts
44
+ # value(wonts).wont_match expected_wonts
45
+ # value { musts }.must_raise TypeError
46
+ #
47
+ # @example EnforcedStyle: expect
48
+ # # bad
49
+ # musts.must_equal expected_musts
50
+ # wonts.wont_match expected_wonts
51
+ # musts.must_raise TypeError
52
+ #
53
+ # _(musts).must_equal expected_musts
54
+ # _(wonts).wont_match expected_wonts
55
+ # _ { musts }.must_raise TypeError
56
+ #
57
+ # value(musts).must_equal expected_musts
58
+ # value(wonts).wont_match expected_wonts
59
+ # value { musts }.must_raise TypeError
60
+ #
61
+ # # good
62
+ # expect(musts).must_equal expected_musts
63
+ # expect(wonts).wont_match expected_wonts
64
+ # expect { musts }.must_raise TypeError
65
+ #
66
+ # @example EnforcedStyle: value
67
+ # # bad
68
+ # musts.must_equal expected_musts
69
+ # wonts.wont_match expected_wonts
70
+ # musts.must_raise TypeError
71
+ #
72
+ # _(musts).must_equal expected_musts
73
+ # _(wonts).wont_match expected_wonts
74
+ # _ { musts }.must_raise TypeError
75
+ #
76
+ # expect(musts).must_equal expected_musts
77
+ # expect(wonts).wont_match expected_wonts
78
+ # expect { musts }.must_raise TypeError
79
+ #
80
+ # # good
81
+ # value(musts).must_equal expected_musts
82
+ # value(wonts).wont_match expected_wonts
83
+ # value { musts }.must_raise TypeError
19
84
  class GlobalExpectations < Base
85
+ include ConfigurableEnforcedStyle
20
86
  extend AutoCorrector
21
87
 
22
88
  MSG = 'Use `%<preferred>s` instead.'
@@ -34,58 +100,63 @@ module RuboCop
34
100
 
35
101
  RESTRICT_ON_SEND = VALUE_MATCHERS + BLOCK_MATCHERS
36
102
 
37
- VALUE_MATCHERS_STR = VALUE_MATCHERS.map do |m|
38
- ":#{m}"
39
- end.join(' ').freeze
103
+ # There are aliases for the `_` method - `expect` and `value`
104
+ DSL_METHODS = %i[_ expect value].freeze
40
105
 
41
- BLOCK_MATCHERS_STR = BLOCK_MATCHERS.map do |m|
42
- ":#{m}"
43
- end.join(' ').freeze
106
+ def on_send(node)
107
+ receiver = node.receiver
108
+ return unless receiver
44
109
 
45
- # There are aliases for the `_` method - `expect` and `value`
46
- DSL_METHODS_LIST = %w[_ value expect].map do |n|
47
- ":#{n}"
48
- end.join(' ').freeze
110
+ method = block_receiver?(receiver) || value_receiver?(receiver)
111
+ return if method == preferred_method || (method && style == :any)
112
+
113
+ register_offense(node, method)
114
+ end
49
115
 
50
- def_node_matcher :value_global_expectation?, <<~PATTERN
51
- (send !(send nil? {#{DSL_METHODS_LIST}} _) {#{VALUE_MATCHERS_STR}} ...)
116
+ private
117
+
118
+ def_node_matcher :block_receiver?, <<~PATTERN
119
+ (block (send nil? $#method_allowed?) _ _)
52
120
  PATTERN
53
121
 
54
- def_node_matcher :block_global_expectation?, <<~PATTERN
55
- (send
56
- [
57
- !(send nil? {#{DSL_METHODS_LIST}} _)
58
- !(block (send nil? {#{DSL_METHODS_LIST}}) _ _)
59
- ]
60
- {#{BLOCK_MATCHERS_STR}}
61
- _
62
- )
122
+ def_node_matcher :value_receiver?, <<~PATTERN
123
+ (send nil? $#method_allowed? _)
63
124
  PATTERN
64
125
 
65
- def on_send(node)
66
- return unless value_global_expectation?(node) || block_global_expectation?(node)
126
+ def method_allowed?(method)
127
+ DSL_METHODS.include?(method)
128
+ end
67
129
 
68
- message = format(MSG, preferred: preferred_receiver(node))
130
+ def preferred_method
131
+ style == :any ? :_ : style
132
+ end
69
133
 
70
- add_offense(node.receiver.source_range, message: message) do |corrector|
71
- receiver = node.receiver.source_range
134
+ def preferred_receiver(node)
135
+ receiver = node.receiver
72
136
 
73
- if BLOCK_MATCHERS.include?(node.method_name)
74
- corrector.wrap(receiver, '_ { ', ' }')
75
- else
76
- corrector.wrap(receiver, '_(', ')')
77
- end
137
+ if BLOCK_MATCHERS.include?(node.method_name)
138
+ body = receiver.lambda? ? receiver.body : receiver
139
+ "#{preferred_method} { #{body.source} }"
140
+ else
141
+ "#{preferred_method}(#{receiver.source})"
78
142
  end
79
143
  end
80
144
 
81
- private
145
+ def register_offense(node, method)
146
+ receiver = node.receiver
82
147
 
83
- def preferred_receiver(node)
84
- source = node.receiver.source
85
- if BLOCK_MATCHERS.include?(node.method_name)
86
- "_ { #{source} }"
148
+ if method
149
+ preferred = preferred_method
150
+ replacement = receiver.source.sub(method.to_s, preferred_method.to_s)
87
151
  else
88
- "_(#{source})"
152
+ preferred = preferred_receiver(node)
153
+ replacement = preferred
154
+ end
155
+
156
+ message = format(MSG, preferred: preferred)
157
+
158
+ add_offense(receiver, message: message) do |corrector|
159
+ corrector.replace(receiver, replacement)
89
160
  end
90
161
  end
91
162
  end
@@ -27,17 +27,16 @@ module RuboCop
27
27
  def on_send(node)
28
28
  return unless node.method?(:assert_equal)
29
29
 
30
- actual = node.arguments[1]
30
+ expected, actual, message = *node.arguments
31
31
  return unless actual&.recursive_basic_literal?
32
+ return if expected.recursive_basic_literal?
32
33
 
33
34
  add_offense(all_arguments_range(node)) do |corrector|
34
- autocorrect(corrector, node)
35
+ autocorrect(corrector, node, expected, actual, message)
35
36
  end
36
37
  end
37
38
 
38
- def autocorrect(corrector, node)
39
- expected, actual, message = *node.arguments
40
-
39
+ def autocorrect(corrector, node, expected, actual, message)
41
40
  new_actual_source = if actual.hash_type? && !actual.braces?
42
41
  "{#{actual.source}}"
43
42
  else