rubocop-minitest 0.2.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
- # Check if your test uses `assert_nil` instead of `assert_equal(nil, something)`.
6
+ # This cop enforces the test to use `assert_nil`
7
+ # instead of using `assert_equal(nil, something)`.
7
8
  #
8
9
  # @example
9
10
  # # bad
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the use of `assert_respond_to(object, :some_method)`
7
+ # over `assert(object.respond_to?(:some_method))`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # assert(object.respond_to?(:some_method))
12
+ # assert(object.respond_to?(:some_method), 'the message')
13
+ # assert(respond_to?(:some_method))
14
+ #
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)
19
+ #
20
+ class AssertRespondTo < Cop
21
+ MSG = 'Prefer using `assert_respond_to(%<preferred>s)` over ' \
22
+ '`assert(%<over>s)`.'
23
+
24
+ def_node_matcher :assert_with_respond_to, <<~PATTERN
25
+ (send nil? :assert $(send $_ :respond_to? $_) $...)
26
+ PATTERN
27
+
28
+ def on_send(node)
29
+ assert_with_respond_to(node) do |over, object, method, rest_args|
30
+ custom_message = rest_args.first
31
+ preferred = build_preferred_arguments(object, method, custom_message)
32
+ over = [over, custom_message].compact.map(&:source).join(', ')
33
+ message = format(MSG, preferred: preferred, over: over)
34
+ add_offense(node, message: message)
35
+ end
36
+ end
37
+
38
+ def autocorrect(node)
39
+ lambda do |corrector|
40
+ assert_with_respond_to(node) do |_, object, method, rest_args|
41
+ custom_message = rest_args.first
42
+ preferred = build_preferred_arguments(object, method, custom_message)
43
+ replacement = "assert_respond_to(#{preferred})"
44
+ corrector.replace(node.loc.expression, replacement)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def build_preferred_arguments(receiver, method, message)
52
+ receiver = receiver ? receiver.source : 'self'
53
+
54
+ [receiver, method.source, message&.source].compact.join(', ')
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -3,8 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
- # Check if your test uses `assert(actual)`
7
- # instead of `assert_equal(true, actual)`.
6
+ # This cop enforces the test to use `assert(actual)`
7
+ # instead of using `assert_equal(true, actual)`.
8
8
  #
9
9
  # @example
10
10
  # # bad
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces to use `refute_empty` instead of
7
+ # using `refute(object.empty?)`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # refute(object.empty?)
12
+ # refute(object.empty?, 'the message')
13
+ #
14
+ # # good
15
+ # refute_empty(object)
16
+ # refute_empty(object, 'the message')
17
+ #
18
+ class RefuteEmpty < Cop
19
+ MSG = 'Prefer using `refute_empty(%<arguments>s)` over ' \
20
+ '`refute(%<receiver>s)`.'
21
+
22
+ def_node_matcher :refute_with_empty, <<~PATTERN
23
+ (send nil? :refute $(send $_ :empty?) $...)
24
+ PATTERN
25
+
26
+ def on_send(node)
27
+ refute_with_empty(node) do |first_receiver_arg, object, rest_receiver_arg|
28
+ message = rest_receiver_arg.first
29
+
30
+ arguments = [object.source, message&.source].compact.join(', ')
31
+ receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
32
+
33
+ offense_message = format(MSG, arguments: arguments, receiver: receiver)
34
+ add_offense(node, message: offense_message)
35
+ end
36
+ end
37
+
38
+ def autocorrect(node)
39
+ lambda do |corrector|
40
+ refute_with_empty(node) do |_first_receiver_arg, object, rest_receiver_arg|
41
+ message = rest_receiver_arg.first
42
+
43
+ replacement = [object.source, message&.source].compact.join(', ')
44
+ corrector.replace(node.loc.expression, "refute_empty(#{replacement})")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the use of `refute_equal(expected, object)`
7
+ # over `assert_equal(expected != actual)` or `assert(! expected == actual)`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # assert("rubocop-minitest" != actual)
12
+ # assert(! "rubocop-minitest" == actual)
13
+ #
14
+ # # good
15
+ # refute_equal("rubocop-minitest", actual)
16
+ #
17
+ class RefuteEqual < Cop
18
+ MSG = 'Prefer using `refute_equal(%<preferred>s)` over ' \
19
+ '`assert(%<over>s)`.'
20
+
21
+ def_node_matcher :assert_not_equal, <<~PATTERN
22
+ (send nil? :assert ${(send $_ :!= $_) (send (send $_ :! ) :== $_) } $... )
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ preferred, over = process_not_equal(node)
27
+ return unless preferred && over
28
+
29
+ message = format(MSG, preferred: preferred, over: over)
30
+ add_offense(node, message: message)
31
+ end
32
+
33
+ def autocorrect(node)
34
+ lambda do |corrector|
35
+ assert_not_equal(node) do |_, first_arg, second_arg, rest_args|
36
+ autocorrect_node(node, corrector, first_arg, second_arg, rest_args)
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def autocorrect_node(node, corrector, first_arg, second_arg, rest_args)
44
+ custom_message = rest_args.first
45
+ replacement = preferred_usage(first_arg, second_arg, custom_message)
46
+ corrector.replace(node.loc.expression, "refute_equal(#{replacement})")
47
+ end
48
+
49
+ def preferred_usage(first_arg, second_arg, custom_message = nil)
50
+ [first_arg, second_arg, custom_message]
51
+ .compact.map(&:source).join(', ')
52
+ end
53
+
54
+ def original_usage(first_part, custom_message)
55
+ [first_part, custom_message].compact.join(', ')
56
+ end
57
+
58
+ def process_not_equal(node)
59
+ assert_not_equal(node) do |over, first_arg, second_arg, rest_args|
60
+ custom_message = rest_args.first
61
+ preferred = preferred_usage(first_arg, second_arg, custom_message)
62
+ over = original_usage(over.source, custom_message&.source)
63
+ return [preferred, over]
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the use of `refute(object)`
7
+ # over `assert_equal(false, object)`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # assert_equal(false, actual)
12
+ # assert_equal(false, actual, 'the message')
13
+ #
14
+ # # good
15
+ # refute(actual)
16
+ # refute(actual, 'the message')
17
+ #
18
+ class RefuteFalse < Cop
19
+ MSG = 'Prefer using `refute(%<arguments>s)` over ' \
20
+ '`assert_equal(false, %<arguments>s)`.'
21
+
22
+ def_node_matcher :assert_equal_with_false, <<~PATTERN
23
+ (send nil? :assert_equal false $_ $...)
24
+ PATTERN
25
+
26
+ def on_send(node)
27
+ assert_equal_with_false(node) do |actual, rest_receiver_arg|
28
+ message = rest_receiver_arg.first
29
+
30
+ arguments = [actual.source, message&.source].compact.join(', ')
31
+
32
+ add_offense(node, message: format(MSG, arguments: arguments))
33
+ end
34
+ end
35
+
36
+ def autocorrect(node)
37
+ lambda do |corrector|
38
+ arguments = node.arguments.reject(&:false_type?)
39
+ replacement = arguments.map(&:source).join(', ')
40
+ corrector.replace(node.loc.expression, "refute(#{replacement})")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the test to use `refute_includes`
7
+ # instead of using `refute(collection.include?(object))`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # refute(collection.include?(object))
12
+ # refute(collection.include?(object), 'the message')
13
+ #
14
+ # # good
15
+ # refute_includes(collection, object)
16
+ # refute_includes(collection, object, 'the message')
17
+ #
18
+ class RefuteIncludes < Cop
19
+ MSG = 'Prefer using `refute_includes(%<arguments>s)` over ' \
20
+ '`refute(%<receiver>s)`.'
21
+
22
+ def_node_matcher :refute_with_includes, <<~PATTERN
23
+ (send nil? :refute $(send $_ :include? $_) $...)
24
+ PATTERN
25
+
26
+ def on_send(node)
27
+ refute_with_includes(node) do
28
+ |first_receiver_arg, collection, object, rest_receiver_arg|
29
+
30
+ message = rest_receiver_arg.first
31
+ arguments = node_arguments(collection, object, 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
+ refute_with_includes(node) do
43
+ |_receiver, collection, object, rest_receiver_arg|
44
+
45
+ message = rest_receiver_arg.first
46
+ replacement = node_arguments(collection, object, message)
47
+ corrector.replace(node.loc.expression, "refute_includes(#{replacement})")
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def node_arguments(collection, object, message)
55
+ [collection.source, object.source, message&.source].compact.join(', ')
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the use of `refute_instance_of(Class, object)`
7
+ # over `refute(object.instance_of?(Class))`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # refute(object.instance_of?(Class))
12
+ # refute(object.instance_of?(Class), 'the message')
13
+ #
14
+ # # good
15
+ # refute_instance_of(Class, object)
16
+ # refute_instance_of(Class, object, 'the message')
17
+ #
18
+ class RefuteInstanceOf < Cop
19
+ MSG = 'Prefer using `refute_instance_of(%<arguments>s)` over ' \
20
+ '`refute(%<receiver>s)`.'
21
+
22
+ def_node_matcher :refute_with_instance_of, <<~PATTERN
23
+ (send nil? :refute $(send $_ :instance_of? $_) $...)
24
+ PATTERN
25
+
26
+ def on_send(node)
27
+ refute_with_instance_of(node) do
28
+ |first_receiver_arg, object, method, rest_args|
29
+
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
+ refute_with_instance_of(node) do |_, object, method, rest_args|
43
+ message = rest_args.first
44
+ arguments = node_arguments(object, method, message)
45
+
46
+ replacement = "refute_instance_of(#{arguments})"
47
+ corrector.replace(node.loc.expression, replacement)
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def node_arguments(object, method, message)
55
+ [method, object, message].compact.map(&:source).join(', ')
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -3,7 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
- # Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.
6
+ # This cop enforces the test to use `refute_nil`
7
+ # instead of using `refute_equal(nil, something)`.
7
8
  #
8
9
  # @example
9
10
  # # bad
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the test to use `refute_respond_to(object, :some_method)`
7
+ # over `refute(object.respond_to?(:some_method))`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # refute(object.respond_to?(:some_method))
12
+ # refute(object.respond_to?(:some_method), 'the message')
13
+ # refute(respond_to?(:some_method))
14
+ #
15
+ # # good
16
+ # refute_respond_to(object, :some_method)
17
+ # refute_respond_to(object, :some_method, 'the message')
18
+ # refute_respond_to(self, :some_method)
19
+ #
20
+ class RefuteRespondTo < Cop
21
+ MSG = 'Prefer using `refute_respond_to(%<preferred>s)` over ' \
22
+ '`refute(%<over>s)`.'
23
+
24
+ def_node_matcher :refute_with_respond_to, <<~PATTERN
25
+ (send nil? :refute $(send $_ :respond_to? $_) $...)
26
+ PATTERN
27
+
28
+ def on_send(node)
29
+ refute_with_respond_to(node) do |over, object, method, rest_args|
30
+ custom_message = rest_args.first
31
+ preferred = build_preferred_arguments(object, method, custom_message)
32
+ over = [over, custom_message].compact.map(&:source).join(', ')
33
+ message = format(MSG, preferred: preferred, over: over)
34
+ add_offense(node, message: message)
35
+ end
36
+ end
37
+
38
+ def autocorrect(node)
39
+ lambda do |corrector|
40
+ refute_with_respond_to(node) do |_, object, method, rest_args|
41
+ custom_message = rest_args.first
42
+ preferred = build_preferred_arguments(object, method, custom_message)
43
+ replacement = "refute_respond_to(#{preferred})"
44
+ corrector.replace(node.loc.expression, replacement)
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def build_preferred_arguments(receiver, method, message)
52
+ receiver = receiver ? receiver.source : 'self'
53
+
54
+ [receiver, method.source, message&.source].compact.join(', ')
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,7 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'minitest/assert_empty'
4
+ require_relative 'minitest/assert_empty_literal'
5
+ require_relative 'minitest/assert_equal'
4
6
  require_relative 'minitest/assert_nil'
5
7
  require_relative 'minitest/assert_includes'
8
+ require_relative 'minitest/assert_instance_of'
9
+ require_relative 'minitest/assert_respond_to'
6
10
  require_relative 'minitest/assert_truthy'
11
+ require_relative 'minitest/refute_empty'
12
+ require_relative 'minitest/refute_false'
13
+ require_relative 'minitest/refute_equal'
7
14
  require_relative 'minitest/refute_nil'
15
+ require_relative 'minitest/refute_includes'
16
+ require_relative 'minitest/refute_instance_of'
17
+ require_relative 'minitest/refute_respond_to'