rubocop-minitest 0.29.0 → 0.30.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
2
  SHA256:
3
- metadata.gz: 938714064905717403a5e63e0cfcca49356b3240c85f2fc26247faefa08ff65f
4
- data.tar.gz: 2db37113c89cc16952a020d3a42f4cf3b94ee067b85b3460c0ef9786c69fe338
3
+ metadata.gz: 04d404efc9a07a82ed435c474b1c4370afa1d36e92f11ec1176911dbe677577c
4
+ data.tar.gz: '079e6fe08096eae97e74790ffda49a120fd5d1b06d15968666d83e046ec821a7'
5
5
  SHA512:
6
- metadata.gz: c9322ed01a4e6598b65742584421c1e92609d8340e6e077df7eb4126abdf842e646ff33a7ae6577ab1649bc2a9ab212645d607fe3db467596a0e8d61517f823e
7
- data.tar.gz: bd62a8d102301b4898ab9c23696b8159bb0ba4c3257d37c7e91ba70263d72bf887cfb257e6e0fa060101305f011cf707d47fa69c82e9b1ce4701a47252ab7b35
6
+ metadata.gz: adf9211bbf6d285987b8bea806bafd15961a8307ea8f6baec4b7ab8d248a571782883ba619bb3542fb440a7ed218daf73b8166e09c33537b52042bcd306aa81e
7
+ data.tar.gz: 94b7021203ca9992df0278ca2d753a7e6c03a54b8a5f49ca3231caf7b8f9b1dd40db791d3504f08bce49bee9e84e1a658cf741785ad74daa03df661dd68ac704
data/README.md CHANGED
@@ -10,8 +10,8 @@ The library is based on the guidelines outlined in the community [Minitest Style
10
10
 
11
11
  Just install the `rubocop-minitest` gem
12
12
 
13
- ```bash
14
- gem install rubocop-minitest
13
+ ```sh
14
+ $ gem install rubocop-minitest
15
15
  ```
16
16
 
17
17
  or if you use bundler put this in your `Gemfile`
@@ -46,8 +46,8 @@ cops together with the standard cops.
46
46
 
47
47
  ### Command line
48
48
 
49
- ```bash
50
- rubocop --require rubocop-minitest
49
+ ```sh
50
+ $ rubocop --require rubocop-minitest
51
51
  ```
52
52
 
53
53
  ### Rake task
data/config/default.yml CHANGED
@@ -181,6 +181,11 @@ Minitest/NoAssertions:
181
181
  Enabled: false
182
182
  VersionAdded: '0.12'
183
183
 
184
+ Minitest/NoTestCases:
185
+ Description: 'Checks if test class contains any test cases.'
186
+ Enabled: false
187
+ VersionAdded: '0.30'
188
+
184
189
  Minitest/NonPublicTestMethod:
185
190
  Description: 'Detects non `public` (marked as `private` or `protected`) test methods.'
186
191
  Enabled: pending
@@ -11,14 +11,30 @@ module RuboCop
11
11
  # assert(object.instance_of?(Class))
12
12
  # assert(object.instance_of?(Class), 'message')
13
13
  #
14
+ # # bad
15
+ # assert_equal(Class, object.class)
16
+ # assert_equal(Class, object.class, 'message')
17
+ #
14
18
  # # good
15
19
  # assert_instance_of(Class, object)
16
20
  # assert_instance_of(Class, object, 'message')
17
21
  #
18
22
  class AssertInstanceOf < Base
19
- extend MinitestCopRule
23
+ include InstanceOfAssertionHandleable
24
+ extend AutoCorrector
25
+
26
+ RESTRICT_ON_SEND = %i[assert assert_equal].freeze
27
+
28
+ def_node_matcher :instance_of_assertion?, <<~PATTERN
29
+ {
30
+ (send nil? :assert (send $_ :instance_of? $const) $_?)
31
+ (send nil? :assert_equal $const (send $_ :class) $_?)
32
+ }
33
+ PATTERN
20
34
 
21
- define_rule :assert, target_method: :instance_of?, inverse: true
35
+ def on_send(node)
36
+ investigate(node, :assert)
37
+ end
22
38
  end
23
39
  end
24
40
  end
@@ -96,7 +96,9 @@ module RuboCop
96
96
  wont_be_kind_of wont_match wont_be_nil wont_be wont_respond_to wont_be_same_as
97
97
  ].freeze
98
98
 
99
- BLOCK_MATCHERS = %i[must_output must_raise must_be_silent must_throw].freeze
99
+ BLOCK_MATCHERS = %i[
100
+ must_output must_pattern_match must_raise must_be_silent must_throw wont_pattern_match
101
+ ].freeze
100
102
 
101
103
  RESTRICT_ON_SEND = VALUE_MATCHERS + BLOCK_MATCHERS
102
104
 
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Checks if test class contains any test cases.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # class FooTest < Minitest::Test
11
+ # def do_something
12
+ # end
13
+ # end
14
+ #
15
+ # # good
16
+ # class FooTest < Minitest::Test
17
+ # def test_something
18
+ # assert true
19
+ # end
20
+ # end
21
+ #
22
+ class NoTestCases < Base
23
+ include MinitestExplorationHelpers
24
+
25
+ MSG = 'Test class should have test cases.'
26
+
27
+ def on_class(node)
28
+ return unless test_class?(node)
29
+
30
+ add_offense(node) if test_cases(node).empty?
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -36,7 +36,7 @@ module RuboCop
36
36
  corrector.replace(node.loc.selector, 'refute_equal')
37
37
 
38
38
  replacement = [expected, actual].map(&:source).join(', ')
39
- corrector.replace(first_argument_range(node), replacement)
39
+ corrector.replace(node.first_argument, replacement)
40
40
  end
41
41
  end
42
42
  end
@@ -66,7 +66,7 @@ module RuboCop
66
66
  if node.method?(:assert_equal)
67
67
  corrector.replace(first_and_second_arguments_range(node), actual.source)
68
68
  else
69
- corrector.replace(first_argument_range(node), actual.source)
69
+ corrector.replace(node.first_argument, actual.source)
70
70
  end
71
71
  end
72
72
  end
@@ -11,14 +11,30 @@ module RuboCop
11
11
  # refute(object.instance_of?(Class))
12
12
  # refute(object.instance_of?(Class), 'message')
13
13
  #
14
+ # # bad
15
+ # refute_equal(Class, object.class)
16
+ # refute_equal(Class, object.class, 'message')
17
+ #
14
18
  # # good
15
19
  # refute_instance_of(Class, object)
16
20
  # refute_instance_of(Class, object, 'message')
17
21
  #
18
22
  class RefuteInstanceOf < Base
19
- extend MinitestCopRule
23
+ include InstanceOfAssertionHandleable
24
+ extend AutoCorrector
25
+
26
+ RESTRICT_ON_SEND = %i[refute refute_equal].freeze
27
+
28
+ def_node_matcher :instance_of_assertion?, <<~PATTERN
29
+ {
30
+ (send nil? :refute (send $_ :instance_of? $const) $_?)
31
+ (send nil? :refute_equal $const (send $_ :class) $_?)
32
+ }
33
+ PATTERN
20
34
 
21
- define_rule :refute, target_method: :instance_of?, inverse: true
35
+ def on_send(node)
36
+ investigate(node, :refute)
37
+ end
22
38
  end
23
39
  end
24
40
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'mixin/argument_range_helper'
4
4
  require_relative 'mixin/in_delta_mixin'
5
+ require_relative 'mixin/instance_of_assertion_handleable'
5
6
  require_relative 'mixin/minitest_cop_rule'
6
7
  require_relative 'mixin/minitest_exploration_helpers'
7
8
  require_relative 'mixin/nil_assertion_handleable'
@@ -34,6 +35,7 @@ require_relative 'minitest/lifecycle_hooks_order'
34
35
  require_relative 'minitest/literal_as_actual_argument'
35
36
  require_relative 'minitest/multiple_assertions'
36
37
  require_relative 'minitest/no_assertions'
38
+ require_relative 'minitest/no_test_cases'
37
39
  require_relative 'minitest/non_public_test_method'
38
40
  require_relative 'minitest/refute_empty'
39
41
  require_relative 'minitest/refute_false'
@@ -9,12 +9,6 @@ module RuboCop
9
9
 
10
10
  private
11
11
 
12
- def first_argument_range(node)
13
- first_argument = node.first_argument
14
-
15
- range_between(first_argument.source_range.begin_pos, first_argument.source_range.end_pos)
16
- end
17
-
18
12
  def first_and_second_arguments_range(node)
19
13
  first_argument = node.first_argument
20
14
  second_argument = node.arguments[1]
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Common functionality for `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` cops.
7
+ # @api private
8
+ module InstanceOfAssertionHandleable
9
+ include ArgumentRangeHelper
10
+
11
+ MSG = 'Prefer using `%<prefer>s`.'
12
+
13
+ private
14
+
15
+ def investigate(node, assertion_type)
16
+ return unless (first_capture, second_capture, message = instance_of_assertion?(node))
17
+
18
+ required_arguments = build_required_arguments(node, assertion_type, first_capture, second_capture)
19
+ full_arguments = [required_arguments, message.first&.source].compact.join(', ')
20
+ prefer = "#{assertion_type}_instance_of(#{full_arguments})"
21
+
22
+ add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
23
+ range = replacement_range(node, assertion_type)
24
+
25
+ corrector.replace(node.loc.selector, "#{assertion_type}_instance_of")
26
+ corrector.replace(range, required_arguments)
27
+ end
28
+ end
29
+
30
+ def build_required_arguments(node, method_name, first_capture, second_capture)
31
+ if node.method?(method_name)
32
+ [second_capture, first_capture]
33
+ else
34
+ [first_capture, second_capture]
35
+ end.map(&:source).join(', ')
36
+ end
37
+
38
+ def replacement_range(node, method_name)
39
+ if node.method?(method_name)
40
+ node.first_argument
41
+ else
42
+ first_and_second_arguments_range(node)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -64,7 +64,7 @@ module RuboCop
64
64
  new_arguments = '(' + new_arguments + ')'
65
65
  end
66
66
 
67
- corrector.replace(first_argument_range(node), new_arguments)
67
+ corrector.replace(node.first_argument, new_arguments)
68
68
  end
69
69
 
70
70
  private
@@ -28,7 +28,7 @@ module RuboCop
28
28
 
29
29
  new_arguments = new_arguments(arguments).join(', ')
30
30
 
31
- corrector.replace(first_argument_range(node), new_arguments)
31
+ corrector.replace(node.first_argument, new_arguments)
32
32
  end
33
33
 
34
34
  private
@@ -79,7 +79,7 @@ module RuboCop
79
79
  cop_name = self.class.to_s.delete_suffix('Test')
80
80
  return unless RuboCop::Cop::Minitest.const_defined?(cop_name)
81
81
 
82
- @cop = RuboCop::Cop::Minitest.const_get(cop_name).new
82
+ @cop = RuboCop::Cop::Minitest.const_get(cop_name).new(configuration)
83
83
  end
84
84
 
85
85
  def format_offense(source, **replacements)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Minitest
5
5
  # This module holds the RuboCop Minitest version information.
6
6
  module Version
7
- STRING = '0.29.0'
7
+ STRING = '0.30.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
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.29.0
4
+ version: 0.30.0
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: 2023-03-06 00:00:00.000000000 Z
13
+ date: 2023-04-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -72,6 +72,7 @@ files:
72
72
  - lib/rubocop/cop/minitest/literal_as_actual_argument.rb
73
73
  - lib/rubocop/cop/minitest/multiple_assertions.rb
74
74
  - lib/rubocop/cop/minitest/no_assertions.rb
75
+ - lib/rubocop/cop/minitest/no_test_cases.rb
75
76
  - lib/rubocop/cop/minitest/non_public_test_method.rb
76
77
  - lib/rubocop/cop/minitest/refute_empty.rb
77
78
  - lib/rubocop/cop/minitest/refute_equal.rb
@@ -96,6 +97,7 @@ files:
96
97
  - lib/rubocop/cop/minitest_cops.rb
97
98
  - lib/rubocop/cop/mixin/argument_range_helper.rb
98
99
  - lib/rubocop/cop/mixin/in_delta_mixin.rb
100
+ - lib/rubocop/cop/mixin/instance_of_assertion_handleable.rb
99
101
  - lib/rubocop/cop/mixin/minitest_cop_rule.rb
100
102
  - lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
101
103
  - lib/rubocop/cop/mixin/nil_assertion_handleable.rb
@@ -112,7 +114,7 @@ metadata:
112
114
  homepage_uri: https://docs.rubocop.org/rubocop-minitest/
113
115
  changelog_uri: https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md
114
116
  source_code_uri: https://github.com/rubocop/rubocop-minitest
115
- documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.29
117
+ documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.30
116
118
  bug_tracker_uri: https://github.com/rubocop/rubocop-minitest/issues
117
119
  rubygems_mfa_required: 'true'
118
120
  post_install_message:
@@ -130,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
132
  - !ruby/object:Gem::Version
131
133
  version: '0'
132
134
  requirements: []
133
- rubygems_version: 3.4.1
135
+ rubygems_version: 3.4.6
134
136
  signing_key:
135
137
  specification_version: 4
136
138
  summary: Automatic Minitest code style checking tool.