rubocop-minitest 0.2.1 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +7 -0
- data/.rubocop_todo.yml +5 -5
- data/CHANGELOG.md +44 -0
- data/Gemfile +6 -2
- data/README.md +2 -1
- data/config/default.yml +70 -10
- data/lib/rubocop-minitest.rb +0 -3
- data/lib/rubocop/cop/minitest/assert_empty.rb +11 -10
- data/lib/rubocop/cop/minitest/assert_empty_literal.rb +36 -0
- data/lib/rubocop/cop/minitest/assert_equal.rb +52 -0
- data/lib/rubocop/cop/minitest/assert_includes.rb +13 -12
- data/lib/rubocop/cop/minitest/assert_instance_of.rb +59 -0
- data/lib/rubocop/cop/minitest/assert_nil.rb +10 -4
- data/lib/rubocop/cop/minitest/assert_respond_to.rb +62 -0
- data/lib/rubocop/cop/minitest/assert_truthy.rb +10 -5
- data/lib/rubocop/cop/minitest/refute_empty.rb +51 -0
- data/lib/rubocop/cop/minitest/refute_equal.rb +68 -0
- data/lib/rubocop/cop/minitest/refute_false.rb +51 -0
- data/lib/rubocop/cop/minitest/refute_includes.rb +61 -0
- data/lib/rubocop/cop/minitest/refute_instance_of.rb +59 -0
- data/lib/rubocop/cop/minitest/refute_nil.rb +10 -4
- data/lib/rubocop/cop/minitest/refute_respond_to.rb +62 -0
- data/lib/rubocop/cop/minitest_cops.rb +11 -0
- data/lib/rubocop/cop/mixin/argument_range_helper.rb +31 -0
- data/lib/rubocop/minitest/version.rb +1 -1
- data/manual/cops.md +10 -0
- data/manual/cops_minitest.md +264 -15
- data/relnotes/v0.1.0.md +7 -0
- data/relnotes/v0.2.0.md +9 -0
- data/relnotes/v0.2.1.md +5 -0
- data/relnotes/v0.3.0.md +16 -0
- data/relnotes/v0.4.0.md +14 -0
- data/relnotes/v0.4.1.md +5 -0
- data/relnotes/v0.5.0.md +5 -0
- data/relnotes/v0.5.1.md +3 -0
- data/tasks/cut_release.rake +60 -0
- metadata +24 -3
@@ -3,7 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Minitest
|
6
|
-
#
|
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
|
@@ -15,6 +16,8 @@ module RuboCop
|
|
15
16
|
# assert_nil(actual, 'the message')
|
16
17
|
#
|
17
18
|
class AssertNil < Cop
|
19
|
+
include ArgumentRangeHelper
|
20
|
+
|
18
21
|
MSG = 'Prefer using `assert_nil(%<arguments>s)` over ' \
|
19
22
|
'`assert_equal(nil, %<arguments>s)`.'
|
20
23
|
|
@@ -34,9 +37,12 @@ module RuboCop
|
|
34
37
|
|
35
38
|
def autocorrect(node)
|
36
39
|
lambda do |corrector|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
assert_equal_with_nil(node) do |actual|
|
41
|
+
corrector.replace(node.loc.selector, 'assert_nil')
|
42
|
+
corrector.replace(
|
43
|
+
first_and_second_arguments_range(node), actual.source
|
44
|
+
)
|
45
|
+
end
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|
@@ -0,0 +1,62 @@
|
|
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
|
+
include ArgumentRangeHelper
|
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
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Minitest
|
6
|
-
#
|
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
|
@@ -16,6 +16,8 @@ module RuboCop
|
|
16
16
|
# assert(actual, 'the message')
|
17
17
|
#
|
18
18
|
class AssertTruthy < Cop
|
19
|
+
include ArgumentRangeHelper
|
20
|
+
|
19
21
|
MSG = 'Prefer using `assert(%<arguments>s)` over ' \
|
20
22
|
'`assert_equal(true, %<arguments>s)`.'
|
21
23
|
|
@@ -35,9 +37,12 @@ module RuboCop
|
|
35
37
|
|
36
38
|
def autocorrect(node)
|
37
39
|
lambda do |corrector|
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
assert_equal_with_truthy(node) do |actual|
|
41
|
+
corrector.replace(node.loc.selector, 'assert')
|
42
|
+
corrector.replace(
|
43
|
+
first_and_second_arguments_range(node), actual.source
|
44
|
+
)
|
45
|
+
end
|
41
46
|
end
|
42
47
|
end
|
43
48
|
end
|
@@ -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
|
+
include ArgumentRangeHelper
|
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
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,68 @@
|
|
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
|
+
include ArgumentRangeHelper
|
19
|
+
|
20
|
+
MSG = 'Prefer using `refute_equal(%<preferred>s)` over ' \
|
21
|
+
'`assert(%<over>s)`.'
|
22
|
+
|
23
|
+
def_node_matcher :assert_not_equal, <<~PATTERN
|
24
|
+
(send nil? :assert ${(send $_ :!= $_) (send (send $_ :! ) :== $_) } $... )
|
25
|
+
PATTERN
|
26
|
+
|
27
|
+
def on_send(node)
|
28
|
+
preferred, over = process_not_equal(node)
|
29
|
+
return unless preferred && over
|
30
|
+
|
31
|
+
message = format(MSG, preferred: preferred, over: over)
|
32
|
+
add_offense(node, message: message)
|
33
|
+
end
|
34
|
+
|
35
|
+
def autocorrect(node)
|
36
|
+
lambda do |corrector|
|
37
|
+
assert_not_equal(node) do |_, expected, actual|
|
38
|
+
corrector.replace(node.loc.selector, 'refute_equal')
|
39
|
+
|
40
|
+
replacement = [expected, actual].map(&:source).join(', ')
|
41
|
+
corrector.replace(first_argument_range(node), replacement)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def preferred_usage(first_arg, second_arg, custom_message = nil)
|
49
|
+
[first_arg, second_arg, custom_message]
|
50
|
+
.compact.map(&:source).join(', ')
|
51
|
+
end
|
52
|
+
|
53
|
+
def original_usage(first_part, custom_message)
|
54
|
+
[first_part, custom_message].compact.join(', ')
|
55
|
+
end
|
56
|
+
|
57
|
+
def process_not_equal(node)
|
58
|
+
assert_not_equal(node) do |over, first_arg, second_arg, rest_args|
|
59
|
+
custom_message = rest_args.first
|
60
|
+
preferred = preferred_usage(first_arg, second_arg, custom_message)
|
61
|
+
over = original_usage(over.source, custom_message&.source)
|
62
|
+
return [preferred, over]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,51 @@
|
|
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
|
+
include ArgumentRangeHelper
|
20
|
+
|
21
|
+
MSG = 'Prefer using `refute(%<arguments>s)` over ' \
|
22
|
+
'`assert_equal(false, %<arguments>s)`.'
|
23
|
+
|
24
|
+
def_node_matcher :assert_equal_with_false, <<~PATTERN
|
25
|
+
(send nil? :assert_equal false $_ $...)
|
26
|
+
PATTERN
|
27
|
+
|
28
|
+
def on_send(node)
|
29
|
+
assert_equal_with_false(node) do |actual, rest_receiver_arg|
|
30
|
+
message = rest_receiver_arg.first
|
31
|
+
|
32
|
+
arguments = [actual.source, message&.source].compact.join(', ')
|
33
|
+
|
34
|
+
add_offense(node, message: format(MSG, arguments: arguments))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def autocorrect(node)
|
39
|
+
lambda do |corrector|
|
40
|
+
assert_equal_with_false(node) do |actual|
|
41
|
+
corrector.replace(node.loc.selector, 'refute')
|
42
|
+
corrector.replace(
|
43
|
+
first_and_second_arguments_range(node), actual.source
|
44
|
+
)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,61 @@
|
|
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
|
+
include ArgumentRangeHelper
|
20
|
+
|
21
|
+
MSG = 'Prefer using `refute_includes(%<arguments>s)` over ' \
|
22
|
+
'`refute(%<receiver>s)`.'
|
23
|
+
|
24
|
+
def_node_matcher :refute_with_includes, <<~PATTERN
|
25
|
+
(send nil? :refute $(send $_ :include? $_) $...)
|
26
|
+
PATTERN
|
27
|
+
|
28
|
+
def on_send(node)
|
29
|
+
refute_with_includes(node) do
|
30
|
+
|first_receiver_arg, collection, object, rest_receiver_arg|
|
31
|
+
|
32
|
+
message = rest_receiver_arg.first
|
33
|
+
arguments = node_arguments(collection, object, message)
|
34
|
+
receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
|
35
|
+
|
36
|
+
offense_message = format(MSG, arguments: arguments, receiver: receiver)
|
37
|
+
|
38
|
+
add_offense(node, message: offense_message)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def autocorrect(node)
|
43
|
+
lambda do |corrector|
|
44
|
+
refute_with_includes(node) do |_, collection, actual|
|
45
|
+
corrector.replace(node.loc.selector, 'refute_includes')
|
46
|
+
|
47
|
+
replacement = [collection, actual].map(&:source).join(', ')
|
48
|
+
corrector.replace(first_argument_range(node), replacement)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def node_arguments(collection, object, message)
|
56
|
+
[collection.source, object.source, message&.source].compact.join(', ')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -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 `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
|
+
include ArgumentRangeHelper
|
20
|
+
|
21
|
+
MSG = 'Prefer using `refute_instance_of(%<arguments>s)` over ' \
|
22
|
+
'`refute(%<receiver>s)`.'
|
23
|
+
|
24
|
+
def_node_matcher :refute_with_instance_of, <<~PATTERN
|
25
|
+
(send nil? :refute $(send $_ :instance_of? $_) $...)
|
26
|
+
PATTERN
|
27
|
+
|
28
|
+
def on_send(node)
|
29
|
+
refute_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
|
+
refute_with_instance_of(node) do |_, object, method|
|
43
|
+
corrector.replace(node.loc.selector, 'refute_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
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -3,7 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Minitest
|
6
|
-
#
|
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
|
@@ -15,6 +16,8 @@ module RuboCop
|
|
15
16
|
# refute_nil(actual, 'the message')
|
16
17
|
#
|
17
18
|
class RefuteNil < Cop
|
19
|
+
include ArgumentRangeHelper
|
20
|
+
|
18
21
|
MSG = 'Prefer using `refute_nil(%<arguments>s)` over ' \
|
19
22
|
'`refute_equal(nil, %<arguments>s)`.'
|
20
23
|
|
@@ -34,9 +37,12 @@ module RuboCop
|
|
34
37
|
|
35
38
|
def autocorrect(node)
|
36
39
|
lambda do |corrector|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
+
refute_equal_with_nil(node) do |actual|
|
41
|
+
corrector.replace(node.loc.selector, 'refute_nil')
|
42
|
+
corrector.replace(
|
43
|
+
first_and_second_arguments_range(node), actual.source
|
44
|
+
)
|
45
|
+
end
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|