rubocop-minitest 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fc4f5cfff2920e3509292c6ff6fc813e67bf0fd4
4
- data.tar.gz: 0b84a930fc18cd0872fc26795d03e2579bc2c43f
2
+ SHA256:
3
+ metadata.gz: 2fe4a7de0dfd13839c19db673fa07861a917a78e2558fae305131eeb852ccb89
4
+ data.tar.gz: 6c2c47e6ecf6260635e87d86cbda171d4716a581fef79786af619b97b76a8dc4
5
5
  SHA512:
6
- metadata.gz: 011aadcd866b0ad8dafd2dc7d08bb0902f00080c26fd36aef654bc5326b82a9ff8a961a540e05d87474b388c817ae472575046ea747e4093f15d6b468b4c4bfb
7
- data.tar.gz: 75d3ef888f72db5d40bfdd7a33619c2280d69709ba633cfde388433c9b2b8757d727b710e169c86aaa2e612ef629bf815c2802fd22a2bb12274afb5a99479d20
6
+ metadata.gz: 9cf68be606a67b36392817fcf1436f5eb6b7c1b4bf1ea5c35516384dde2ff8a2a1b370bdbf900d3c58b96f0e4d0f5ae9c70084864ca08ef3fdbe0a8bf6a50a1d
7
+ data.tar.gz: 0f88de02da060b0b05c2ff6cb14d37989c1ca0d63b7c298809dda8fad8fb9271d41c98c816f90dff0282198a40096523c5a70151b7551fec0c4c6790a689bdfd
data/.gitattributes ADDED
@@ -0,0 +1 @@
1
+ CHANGELOG.md merge=union
data/.rubocop_todo.yml CHANGED
@@ -20,4 +20,4 @@ Metrics/MethodLength:
20
20
  # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
21
21
  # URISchemes: http, https
22
22
  Layout/LineLength:
23
- Max: 90
23
+ Max: 100
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 0.7.0 (2020-03-09)
6
+
7
+ ### New features
8
+
9
+ * [#60](https://github.com/rubocop-hq/rubocop-minitest/issues/60): Add new cop `Minitest/GlobalExpectations` to check for deprecated global expectations. ([@tejasbubane][])
10
+
11
+ ### Bug fixes
12
+
13
+ * [#58](https://github.com/rubocop-hq/rubocop-minitest/pull/58): Fix a false negative for `Minitest/AssertMatch` and `Minitest/RefuteMatch` when an argument is enclosed in redundant parentheses. ([@koic][])
14
+ * [#59](https://github.com/rubocop-hq/rubocop-minitest/pull/59): Fix a false negative for `Minitest/AssertRespondTo` and `Minitest/RefuteRespondTo` when an argument is enclosed in redundant parentheses. ([@koic][])
15
+ * [#61](https://github.com/rubocop-hq/rubocop-minitest/pull/61): Fix a false negative for `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` when an argument is enclosed in redundant parentheses. ([@koic][])
16
+ * [#62](https://github.com/rubocop-hq/rubocop-minitest/pull/62): Fix a false negative for `Minitest/AssertEmpty` and `Minitest/RefuteEmpty` when an argument is enclosed in redundant parentheses. ([@koic][])
17
+
5
18
  ## 0.6.2 (2020-02-19)
6
19
 
7
20
  ### Bug fixes
data/README.md CHANGED
@@ -73,6 +73,10 @@ Minitest/AssertNil:
73
73
  - test/my_file_to_ignore_test.rb
74
74
  ```
75
75
 
76
+ ## Documentation
77
+
78
+ You can read a lot more about RuboCop Minitest in its [official docs](https://docs.rubocop.org/projects/minitest/).
79
+
76
80
  ## Contributing
77
81
 
78
82
  Checkout the [contribution guidelines](CONTRIBUTING.md).
data/config/default.yml CHANGED
@@ -45,7 +45,7 @@ Minitest/AssertNil:
45
45
  VersionAdded: '0.1'
46
46
 
47
47
  Minitest/AssertRespondTo:
48
- Description: 'This cop enforces the test to use `assert_respond_to(object, :some_method)` over `assert(object.respond_to?(:some_method))`.'
48
+ Description: 'This cop enforces the test to use `assert_respond_to(object, :do_something)` over `assert(object.respond_to?(:do_something))`.'
49
49
  StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-responds-to-method'
50
50
  Enabled: true
51
51
  VersionAdded: '0.3'
@@ -56,6 +56,11 @@ Minitest/AssertTruthy:
56
56
  Enabled: true
57
57
  VersionAdded: '0.2'
58
58
 
59
+ Minitest/GlobalExpectations:
60
+ Description: 'This cop checks for deprecated global expectations.'
61
+ Enabled: true
62
+ VersionAdded: '0.7'
63
+
59
64
  Minitest/RefuteEmpty:
60
65
  Description: 'This cop enforces to use `refute_empty` instead of using `refute(object.empty?)`.'
61
66
  StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-empty'
@@ -99,7 +104,7 @@ Minitest/RefuteNil:
99
104
  VersionAdded: '0.2'
100
105
 
101
106
  Minitest/RefuteRespondTo:
102
- Description: 'This cop enforces the test to use `refute_respond_to(object, :some_method)` over `refute(object.respond_to?(:some_method))`.'
107
+ Description: 'This cop enforces the test to use `refute_respond_to(object, :do_something)` over `refute(object.respond_to?(:do_something))`.'
103
108
  StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-respond-to'
104
109
  Enabled: true
105
110
  VersionAdded: '0.4'
@@ -9,42 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert(object.empty?)
12
- # assert(object.empty?, 'the message')
12
+ # assert(object.empty?, 'message')
13
13
  #
14
14
  # # good
15
15
  # assert_empty(object)
16
- # assert_empty(object, 'the message')
16
+ # assert_empty(object, 'message')
17
17
  #
18
18
  class AssertEmpty < Cop
19
- include ArgumentRangeHelper
19
+ extend MinitestCopRule
20
20
 
21
- MSG = 'Prefer using `assert_empty(%<arguments>s)` over ' \
22
- '`assert(%<receiver>s)`.'
23
-
24
- def_node_matcher :assert_with_empty, <<~PATTERN
25
- (send nil? :assert $(send $_ :empty?) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- assert_with_empty(node) do |first_receiver_arg, actual, rest_receiver_arg|
30
- message = rest_receiver_arg.first
31
-
32
- arguments = [actual.source, message&.source].compact.join(', ')
33
- receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
34
-
35
- offense_message = format(MSG, arguments: arguments, receiver: receiver)
36
- add_offense(node, message: offense_message)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- assert_with_empty(node) do |_, actual_arg|
43
- corrector.replace(node.loc.selector, 'assert_empty')
44
- corrector.replace(first_argument_range(node), actual_arg.source)
45
- end
46
- end
47
- end
21
+ define_rule :assert, target_method: :empty?
48
22
  end
49
23
  end
50
24
  end
@@ -9,16 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert(collection.include?(object))
12
- # assert(collection.include?(object), 'the message')
12
+ # assert(collection.include?(object), 'message')
13
13
  #
14
14
  # # good
15
15
  # assert_includes(collection, object)
16
- # assert_includes(collection, object, 'the message')
16
+ # assert_includes(collection, object, 'message')
17
17
  #
18
18
  class AssertIncludes < Cop
19
- extend IncludesCopRule
19
+ extend MinitestCopRule
20
20
 
21
- rule target_method: :assert, prefer_method: :assert_includes
21
+ define_rule :assert, target_method: :include?, preferred_method: :assert_includes
22
22
  end
23
23
  end
24
24
  end
@@ -9,50 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert(object.instance_of?(Class))
12
- # assert(object.instance_of?(Class), 'the message')
12
+ # assert(object.instance_of?(Class), 'message')
13
13
  #
14
14
  # # good
15
15
  # assert_instance_of(Class, object)
16
- # assert_instance_of(Class, object, 'the message')
16
+ # assert_instance_of(Class, object, 'message')
17
17
  #
18
18
  class AssertInstanceOf < Cop
19
- include ArgumentRangeHelper
19
+ extend MinitestCopRule
20
20
 
21
- MSG = 'Prefer using `assert_instance_of(%<arguments>s)` over ' \
22
- '`assert(%<receiver>s)`.'
23
-
24
- def_node_matcher :assert_with_instance_of, <<~PATTERN
25
- (send nil? :assert $(send $_ :instance_of? $_) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- assert_with_instance_of(node) do |first_receiver_arg, object, method, rest_args|
30
- message = rest_args.first
31
- arguments = node_arguments(object, method, message)
32
- receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
33
-
34
- offense_message = format(MSG, arguments: arguments, receiver: receiver)
35
-
36
- add_offense(node, message: offense_message)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- assert_with_instance_of(node) do |_, object, method|
43
- corrector.replace(node.loc.selector, 'assert_instance_of')
44
-
45
- replacement = [method, object].map(&:source).join(', ')
46
- corrector.replace(first_argument_range(node), replacement)
47
- end
48
- end
49
- end
50
-
51
- private
52
-
53
- def node_arguments(object, method, message)
54
- [method, object, message].compact.map(&:source).join(', ')
55
- end
21
+ define_rule :assert, target_method: :instance_of?, inverse: true
56
22
  end
57
23
  end
58
24
  end
@@ -9,51 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert(matcher.match(string))
12
- # assert(matcher.match(string), 'the message')
12
+ # assert(matcher.match(string), 'message')
13
13
  #
14
14
  # # good
15
15
  # assert_match(regex, string)
16
- # assert_match(matcher, string, 'the message')
16
+ # assert_match(matcher, string, 'message')
17
17
  #
18
18
  class AssertMatch < Cop
19
- include ArgumentRangeHelper
19
+ extend MinitestCopRule
20
20
 
21
- MSG = 'Prefer using `assert_match(%<arguments>s)` over ' \
22
- '`assert(%<receiver>s)`.'
23
-
24
- def_node_matcher :assert_with_match, <<~PATTERN
25
- (send nil? :assert $(send $_ :match $_) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- assert_with_match(node) do
30
- |first_receiver_arg, matcher, actual, rest_receiver_arg|
31
- message = rest_receiver_arg.first
32
- arguments = node_arguments(matcher, actual, message)
33
- receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
34
-
35
- offense_message = format(MSG, arguments: arguments, receiver: receiver)
36
-
37
- add_offense(node, message: offense_message)
38
- end
39
- end
40
-
41
- def autocorrect(node)
42
- lambda do |corrector|
43
- assert_with_match(node) do |_, matcher, actual|
44
- corrector.replace(node.loc.selector, 'assert_match')
45
-
46
- replacement = [matcher, actual].map(&:source).join(', ')
47
- corrector.replace(first_argument_range(node), replacement)
48
- end
49
- end
50
- end
51
-
52
- private
53
-
54
- def node_arguments(matcher, actual, message)
55
- [matcher.source, actual.source, message&.source].compact.join(', ')
56
- end
21
+ define_rule :assert, target_method: :match
57
22
  end
58
23
  end
59
24
  end
@@ -9,11 +9,11 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert_equal(nil, actual)
12
- # assert_equal(nil, actual, 'the message')
12
+ # assert_equal(nil, actual, 'message')
13
13
  #
14
14
  # # good
15
15
  # assert_nil(actual)
16
- # assert_nil(actual, 'the message')
16
+ # assert_nil(actual, 'message')
17
17
  #
18
18
  class AssertNil < Cop
19
19
  include ArgumentRangeHelper
@@ -3,59 +3,24 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
- # This cop enforces the use of `assert_respond_to(object, :some_method)`
7
- # over `assert(object.respond_to?(:some_method))`.
6
+ # This cop enforces the use of `assert_respond_to(object, :do_something)`
7
+ # over `assert(object.respond_to?(:do_something))`.
8
8
  #
9
9
  # @example
10
10
  # # bad
11
- # assert(object.respond_to?(:some_method))
12
- # assert(object.respond_to?(:some_method), 'the message')
13
- # assert(respond_to?(:some_method))
11
+ # assert(object.respond_to?(:do_something))
12
+ # assert(object.respond_to?(:do_something), 'message')
13
+ # assert(respond_to?(:do_something))
14
14
  #
15
15
  # # good
16
- # assert_respond_to(object, :some_method)
17
- # assert_respond_to(object, :some_method, 'the message')
18
- # assert_respond_to(self, some_method)
16
+ # assert_respond_to(object, :do_something)
17
+ # assert_respond_to(object, :do_something, 'message')
18
+ # assert_respond_to(self, :do_something)
19
19
  #
20
20
  class AssertRespondTo < Cop
21
- include ArgumentRangeHelper
21
+ extend MinitestCopRule
22
22
 
23
- MSG = 'Prefer using `assert_respond_to(%<preferred>s)` over ' \
24
- '`assert(%<over>s)`.'
25
-
26
- def_node_matcher :assert_with_respond_to, <<~PATTERN
27
- (send nil? :assert $(send $_ :respond_to? $_) $...)
28
- PATTERN
29
-
30
- def on_send(node)
31
- assert_with_respond_to(node) do |over, object, method, rest_args|
32
- custom_message = rest_args.first
33
- preferred = build_preferred_arguments(object, method, custom_message)
34
- over = [over, custom_message].compact.map(&:source).join(', ')
35
- message = format(MSG, preferred: preferred, over: over)
36
- add_offense(node, message: message)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- assert_with_respond_to(node) do |_, object, method|
43
- corrector.replace(node.loc.selector, 'assert_respond_to')
44
-
45
- object = object ? object.source : 'self'
46
- replacement = [object, method.source].join(', ')
47
- corrector.replace(first_argument_range(node), replacement)
48
- end
49
- end
50
- end
51
-
52
- private
53
-
54
- def build_preferred_arguments(receiver, method, message)
55
- receiver = receiver ? receiver.source : 'self'
56
-
57
- [receiver, method.source, message&.source].compact.join(', ')
58
- end
23
+ define_rule :assert, target_method: :respond_to?
59
24
  end
60
25
  end
61
26
  end
@@ -9,11 +9,11 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert_equal(true, actual)
12
- # assert_equal(true, actual, 'the message')
12
+ # assert_equal(true, actual, 'message')
13
13
  #
14
14
  # # good
15
15
  # assert(actual)
16
- # assert(actual, 'the message')
16
+ # assert(actual, 'message')
17
17
  #
18
18
  class AssertTruthy < Cop
19
19
  include ArgumentRangeHelper
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This Cop checks for deprecated global expectations
7
+ # and autocorrects them to use expect format.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # n.must_equal 42
12
+ # n.wont_match b
13
+ #
14
+ # # good
15
+ # _(n).must_equal 42
16
+ # _(n).wont_match b
17
+ class GlobalExpectations < Cop
18
+ MSG = 'Prefer using `%<corrected>s`.'
19
+
20
+ VALUE_MATCHERS = %i[
21
+ be be_close_to be_empty be_instance_of be_kind_of
22
+ be_nil be_same_as be_silent be_within_epsilon equal
23
+ include match respond_to must_exist
24
+ ].map do |matcher|
25
+ [:"must_#{matcher}", :"wont_#{matcher}"]
26
+ end.flatten.freeze
27
+
28
+ BLOCK_MATCHERS = %i[must_output must_raise must_throw].freeze
29
+
30
+ MATCHERS_STR = (VALUE_MATCHERS + BLOCK_MATCHERS).map do |m|
31
+ ":#{m}"
32
+ end.join(' ').freeze
33
+
34
+ def_node_matcher :global_expectation?, <<~PATTERN
35
+ (send (send _ _) {#{MATCHERS_STR}} ...)
36
+ PATTERN
37
+
38
+ def on_send(node)
39
+ return unless global_expectation?(node)
40
+
41
+ message = format(MSG, corrected: correct_suggestion(node))
42
+ add_offense(node, message: message)
43
+ end
44
+
45
+ def autocorrect(node)
46
+ return unless global_expectation?(node)
47
+
48
+ lambda do |corrector|
49
+ receiver = node.receiver.loc.selector
50
+
51
+ if BLOCK_MATCHERS.include?(node.method_name)
52
+ corrector.insert_before(receiver, '_ { ')
53
+ corrector.insert_after(receiver, ' }')
54
+ else
55
+ corrector.insert_before(receiver, '_(')
56
+ corrector.insert_after(receiver, ')')
57
+ end
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def correct_suggestion(node)
64
+ source = node.receiver.source
65
+ if BLOCK_MATCHERS.include?(node.method_name)
66
+ node.source.sub(source, "_ { #{source} }")
67
+ else
68
+ node.source.sub(source, "_(#{source})")
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -9,42 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # refute(object.empty?)
12
- # refute(object.empty?, 'the message')
12
+ # refute(object.empty?, 'message')
13
13
  #
14
14
  # # good
15
15
  # refute_empty(object)
16
- # refute_empty(object, 'the message')
16
+ # refute_empty(object, 'message')
17
17
  #
18
18
  class RefuteEmpty < Cop
19
- include ArgumentRangeHelper
19
+ extend MinitestCopRule
20
20
 
21
- MSG = 'Prefer using `refute_empty(%<arguments>s)` over ' \
22
- '`refute(%<receiver>s)`.'
23
-
24
- def_node_matcher :refute_with_empty, <<~PATTERN
25
- (send nil? :refute $(send $_ :empty?) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- refute_with_empty(node) do |first_receiver_arg, object, rest_receiver_arg|
30
- message = rest_receiver_arg.first
31
-
32
- arguments = [object.source, message&.source].compact.join(', ')
33
- receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
34
-
35
- offense_message = format(MSG, arguments: arguments, receiver: receiver)
36
- add_offense(node, message: offense_message)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- refute_with_empty(node) do |_, actual_arg|
43
- corrector.replace(node.loc.selector, 'refute_empty')
44
- corrector.replace(first_argument_range(node), actual_arg.source)
45
- end
46
- end
47
- end
21
+ define_rule :refute, target_method: :empty?
48
22
  end
49
23
  end
50
24
  end