rubocop-minitest 0.6.0 → 0.6.1

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
2
  SHA1:
3
- metadata.gz: b6f7bc7c5d1f40800643ddc8cfbdb5b3b716484e
4
- data.tar.gz: d2f4fedb8db4422c4f601a4410b04b77ef51bf12
3
+ metadata.gz: ba7306fba69394bb98154a2d39b7f755587ad0e4
4
+ data.tar.gz: 9d20cb250e999413a04620cfbe0eb5c930a3b83e
5
5
  SHA512:
6
- metadata.gz: 0eaa51499fa109978fae23597f05cb11c0003fb7a4d887249b17c4966f6a2422aec2ee29c2c62baa044b006eb2965076072c306a3863b821382019324274c549
7
- data.tar.gz: 79225d37425d04fcc425ad661b7970b99ab291f93f2337cf7cb7008ffa55aeb9d29cc0a79d9129f1e089c7eb5f00af95187b785a002f63ffde8b92ea0cd0df52
6
+ metadata.gz: c9e12069624b30051284458118b7447fd5d52697d97cbd150f1062898cbd2a5975d2bb24d13185afd94b13ad40702a242ba180c1484e2b6761f0218f30e0bab3
7
+ data.tar.gz: f33c1179084c1ac358d702c4537b84eba697851368a6c3d9245e54abf20e4acd4149c7d0bfb833ee3c9b42537cde7d0bfca6bd30420fd1e1480e5386490d0d6c
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 0.6.1 (2020-02-18)
6
+
7
+ ### Bug fixes
8
+
9
+ * [#52](https://github.com/rubocop-hq/rubocop-minitest/issues/52): Make `Minitest/RefuteFalse` cop aware of `assert(!test)`. ([@koic][])
10
+ * [#52](https://github.com/rubocop-hq/rubocop-minitest/issues/52): Fix a false negative for `Minitest/AssertIncludes` and `Minitest/RefuteIncludes` when an argument is enclosed in redundant parentheses. ([@koic][])
11
+
5
12
  ## 0.6.0 (2020-02-07)
6
13
 
7
14
  ### New features
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Bozhidar Batsov, Jonas Arvidsson, Koichi ITO
3
+ Copyright (c) 2019-2020 Bozhidar Batsov, Jonas Arvidsson, Koichi ITO
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -16,45 +16,9 @@ module RuboCop
16
16
  # assert_includes(collection, object, 'the message')
17
17
  #
18
18
  class AssertIncludes < Cop
19
- include ArgumentRangeHelper
19
+ extend IncludesCopRule
20
20
 
21
- MSG = 'Prefer using `assert_includes(%<arguments>s)` over ' \
22
- '`assert(%<receiver>s)`.'
23
-
24
- def_node_matcher :assert_with_includes, <<~PATTERN
25
- (send nil? :assert $(send $_ :include? $_) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- assert_with_includes(node) do
30
- |first_receiver_arg, collection, actual, rest_receiver_arg|
31
-
32
- message = rest_receiver_arg.first
33
- arguments = node_arguments(collection, actual, 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
- assert_with_includes(node) do |_, collection, actual|
45
- corrector.replace(node.loc.selector, 'assert_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, actual, message)
56
- [collection.source, actual.source, message&.source].compact.join(', ')
57
- end
21
+ rule target_method: :assert, prefer_method: :assert_includes
58
22
  end
59
23
  end
60
24
  end
@@ -11,6 +11,9 @@ module RuboCop
11
11
  # assert_equal(false, actual)
12
12
  # assert_equal(false, actual, 'the message')
13
13
  #
14
+ # assert(!test)
15
+ # assert(!test, 'the message')
16
+ #
14
17
  # # good
15
18
  # refute(actual)
16
19
  # refute(actual, 'the message')
@@ -18,31 +21,52 @@ module RuboCop
18
21
  class RefuteFalse < Cop
19
22
  include ArgumentRangeHelper
20
23
 
21
- MSG = 'Prefer using `refute(%<arguments>s)` over ' \
24
+ MSG_FOR_ASSERT_EQUAL = 'Prefer using `refute(%<arguments>s)` over ' \
22
25
  '`assert_equal(false, %<arguments>s)`.'
26
+ MSG_FOR_ASSERT = 'Prefer using `refute(%<arguments>s)` over ' \
27
+ '`assert(!%<arguments>s)`.'
23
28
 
24
29
  def_node_matcher :assert_equal_with_false, <<~PATTERN
25
30
  (send nil? :assert_equal false $_ $...)
26
31
  PATTERN
27
32
 
33
+ def_node_matcher :assert_with_bang_argument, <<~PATTERN
34
+ (send nil? :assert (send $_ :!) $...)
35
+ PATTERN
36
+
28
37
  def on_send(node)
29
- assert_equal_with_false(node) do |actual, rest_receiver_arg|
30
- message = rest_receiver_arg.first
38
+ actual, rest_receiver_arg = assert_equal_with_false(node) ||
39
+ assert_with_bang_argument(node)
40
+ return unless actual
31
41
 
32
- arguments = [actual.source, message&.source].compact.join(', ')
42
+ message_argument = rest_receiver_arg.first
33
43
 
34
- add_offense(node, message: format(MSG, arguments: arguments))
35
- end
44
+ arguments = [actual.source, message_argument&.source].compact.join(', ')
45
+
46
+ message = if node.method?(:assert_equal)
47
+ MSG_FOR_ASSERT_EQUAL
48
+ else
49
+ MSG_FOR_ASSERT
50
+ end
51
+
52
+ add_offense(node, message: format(message, arguments: arguments))
36
53
  end
37
54
 
38
55
  def autocorrect(node)
39
56
  lambda do |corrector|
57
+ corrector.replace(node.loc.selector, 'refute')
58
+
40
59
  assert_equal_with_false(node) do |actual|
41
- corrector.replace(node.loc.selector, 'refute')
42
60
  corrector.replace(
43
61
  first_and_second_arguments_range(node), actual.source
44
62
  )
45
63
  end
64
+
65
+ assert_with_bang_argument(node) do |actual|
66
+ corrector.replace(
67
+ first_argument_range(node), actual.source
68
+ )
69
+ end
46
70
  end
47
71
  end
48
72
  end
@@ -16,45 +16,9 @@ module RuboCop
16
16
  # refute_includes(collection, object, 'the message')
17
17
  #
18
18
  class RefuteIncludes < Cop
19
- include ArgumentRangeHelper
19
+ extend IncludesCopRule
20
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
21
+ rule target_method: :refute, prefer_method: :refute_includes
58
22
  end
59
23
  end
60
24
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'mixin/argument_range_helper'
4
+ require_relative 'mixin/includes_cop_rule'
4
5
  require_relative 'minitest/assert_empty'
5
6
  require_relative 'minitest/assert_empty_literal'
6
7
  require_relative 'minitest/assert_equal'
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Define the rule for `Minitest/AssertIncludes` and `Minitest/RefuteIncludes` cops.
6
+ module IncludesCopRule
7
+ def rule(target_method:, prefer_method:)
8
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
9
+ include ArgumentRangeHelper
10
+
11
+ MSG = 'Prefer using `#{prefer_method}(%<new_arguments>s)` over ' \
12
+ '`#{target_method}(%<original_arguments>s)`.'
13
+
14
+ def on_send(node)
15
+ return unless node.method?(:#{target_method})
16
+ return unless (arguments = peel_redundant_parentheses_from(node.arguments))
17
+ return unless arguments.first.method?(:include?)
18
+
19
+ add_offense(node, message: offense_message(arguments))
20
+ end
21
+
22
+ def autocorrect(node)
23
+ lambda do |corrector|
24
+ corrector.replace(node.loc.selector, '#{prefer_method}')
25
+
26
+ arguments = peel_redundant_parentheses_from(node.arguments)
27
+
28
+ new_arguments = [
29
+ arguments.first.receiver.source,
30
+ arguments.first.arguments.map(&:source)
31
+ ].join(', ')
32
+
33
+ if enclosed_in_redundant_parentheses?(node)
34
+ new_arguments = '(' + new_arguments + ')'
35
+ end
36
+
37
+ corrector.replace(first_argument_range(node), new_arguments)
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def peel_redundant_parentheses_from(arguments)
44
+ return arguments unless arguments.first.begin_type?
45
+
46
+ peel_redundant_parentheses_from(arguments.first.children)
47
+ end
48
+
49
+ def offense_message(arguments)
50
+ new_arguments = new_arguments(arguments)
51
+
52
+ original_arguments = arguments.map(&:source).join(', ')
53
+
54
+ format(
55
+ MSG,
56
+ new_arguments: new_arguments,
57
+ original_arguments: original_arguments
58
+ )
59
+ end
60
+
61
+ def new_arguments(arguments)
62
+ message_argument = arguments.last if arguments.first != arguments.last
63
+
64
+ [
65
+ arguments.first.receiver,
66
+ arguments.first.arguments.first,
67
+ message_argument
68
+ ].compact.map(&:source).join(', ')
69
+ end
70
+
71
+ def enclosed_in_redundant_parentheses?(node)
72
+ node.arguments.first.begin_type?
73
+ end
74
+ RUBY
75
+ end
76
+ end
77
+ end
78
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Minitest
5
- VERSION = '0.6.0'
5
+ VERSION = '0.6.1'
6
6
  end
7
7
  end
@@ -285,6 +285,9 @@ over `assert_equal(false, object)`.
285
285
  assert_equal(false, actual)
286
286
  assert_equal(false, actual, 'the message')
287
287
 
288
+ assert(!test)
289
+ assert(!test, 'the message')
290
+
288
291
  # good
289
292
  refute(actual)
290
293
  refute(actual, 'the message')
@@ -0,0 +1,6 @@
1
+ ### Bug fixes
2
+
3
+ * [#52](https://github.com/rubocop-hq/rubocop-minitest/issues/52): Make `Minitest/RefuteFalse` cop aware of `assert(!test)`. ([@koic][])
4
+ * [#52](https://github.com/rubocop-hq/rubocop-minitest/issues/52): Fix a false negative for `Minitest/AssertIncludes` and `Minitest/RefuteIncludes` when an argument is enclosed in redundant parentheses. ([@koic][])
5
+
6
+ [@koic]: https://github.com/koic
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.required_ruby_version = '>= 2.3.0'
20
20
  spec.metadata = {
21
- 'homepage_uri' => 'https://docs.rubocop.org/projects/minitest',
21
+ 'homepage_uri' => 'https://docs.rubocop.org/projects/minitest/',
22
22
  'changelog_uri' => 'https://github.com/rubocop-hq/rubocop-minitest/blob/master/CHANGELOG.md',
23
23
  'source_code_uri' => 'https://github.com/rubocop-hq/rubocop-minitest',
24
- 'documentation_uri' => 'https://docs.rubocop.org/projects/minitest',
24
+ 'documentation_uri' => 'https://docs.rubocop.org/projects/minitest/',
25
25
  'bug_tracker_uri' => 'https://github.com/rubocop-hq/rubocop-minitest/issues'
26
26
  }
27
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-minitest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-02-07 00:00:00.000000000 Z
13
+ date: 2020-02-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -85,6 +85,7 @@ files:
85
85
  - lib/rubocop/cop/minitest/refute_respond_to.rb
86
86
  - lib/rubocop/cop/minitest_cops.rb
87
87
  - lib/rubocop/cop/mixin/argument_range_helper.rb
88
+ - lib/rubocop/cop/mixin/includes_cop_rule.rb
88
89
  - lib/rubocop/minitest.rb
89
90
  - lib/rubocop/minitest/inject.rb
90
91
  - lib/rubocop/minitest/version.rb
@@ -104,6 +105,7 @@ files:
104
105
  - relnotes/v0.5.0.md
105
106
  - relnotes/v0.5.1.md
106
107
  - relnotes/v0.6.0.md
108
+ - relnotes/v0.6.1.md
107
109
  - rubocop-minitest.gemspec
108
110
  - tasks/cops_documentation.rake
109
111
  - tasks/cut_release.rake
@@ -111,10 +113,10 @@ homepage:
111
113
  licenses:
112
114
  - MIT
113
115
  metadata:
114
- homepage_uri: https://docs.rubocop.org/projects/minitest
116
+ homepage_uri: https://docs.rubocop.org/projects/minitest/
115
117
  changelog_uri: https://github.com/rubocop-hq/rubocop-minitest/blob/master/CHANGELOG.md
116
118
  source_code_uri: https://github.com/rubocop-hq/rubocop-minitest
117
- documentation_uri: https://docs.rubocop.org/projects/minitest
119
+ documentation_uri: https://docs.rubocop.org/projects/minitest/
118
120
  bug_tracker_uri: https://github.com/rubocop-hq/rubocop-minitest/issues
119
121
  post_install_message:
120
122
  rdoc_options: []