rubocop-rspec 2.25.0 → 2.27.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -0
  3. data/config/default.yml +39 -4
  4. data/lib/rubocop/cop/rspec/around_block.rb +3 -3
  5. data/lib/rubocop/cop/rspec/be.rb +1 -1
  6. data/lib/rubocop/cop/rspec/be_empty.rb +1 -0
  7. data/lib/rubocop/cop/rspec/be_eq.rb +1 -1
  8. data/lib/rubocop/cop/rspec/be_eql.rb +1 -1
  9. data/lib/rubocop/cop/rspec/be_nil.rb +2 -2
  10. data/lib/rubocop/cop/rspec/before_after_all.rb +7 -13
  11. data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +2 -2
  12. data/lib/rubocop/cop/rspec/change_by_zero.rb +30 -4
  13. data/lib/rubocop/cop/rspec/context_method.rb +2 -2
  14. data/lib/rubocop/cop/rspec/context_wording.rb +1 -1
  15. data/lib/rubocop/cop/rspec/describe_symbol.rb +1 -1
  16. data/lib/rubocop/cop/rspec/described_class.rb +33 -11
  17. data/lib/rubocop/cop/rspec/empty_example_group.rb +2 -2
  18. data/lib/rubocop/cop/rspec/empty_line_after_example.rb +2 -2
  19. data/lib/rubocop/cop/rspec/example_without_description.rb +11 -2
  20. data/lib/rubocop/cop/rspec/example_wording.rb +11 -2
  21. data/lib/rubocop/cop/rspec/excessive_docstring_spacing.rb +1 -1
  22. data/lib/rubocop/cop/rspec/expect_actual.rb +5 -2
  23. data/lib/rubocop/cop/rspec/expect_change.rb +2 -2
  24. data/lib/rubocop/cop/rspec/expect_output.rb +1 -4
  25. data/lib/rubocop/cop/rspec/focus.rb +2 -2
  26. data/lib/rubocop/cop/rspec/hook_argument.rb +2 -2
  27. data/lib/rubocop/cop/rspec/hooks_before_examples.rb +1 -1
  28. data/lib/rubocop/cop/rspec/implicit_block_expectation.rb +2 -2
  29. data/lib/rubocop/cop/rspec/implicit_expect.rb +1 -1
  30. data/lib/rubocop/cop/rspec/implicit_subject.rb +2 -2
  31. data/lib/rubocop/cop/rspec/instance_spy.rb +2 -2
  32. data/lib/rubocop/cop/rspec/instance_variable.rb +2 -2
  33. data/lib/rubocop/cop/rspec/is_expected_specify.rb +45 -0
  34. data/lib/rubocop/cop/rspec/iterated_expectation.rb +3 -3
  35. data/lib/rubocop/cop/rspec/let_before_examples.rb +1 -1
  36. data/lib/rubocop/cop/rspec/let_setup.rb +1 -1
  37. data/lib/rubocop/cop/rspec/message_expectation.rb +1 -2
  38. data/lib/rubocop/cop/rspec/message_spies.rb +0 -2
  39. data/lib/rubocop/cop/rspec/mixin/skip_or_pending.rb +2 -2
  40. data/lib/rubocop/cop/rspec/multiple_expectations.rb +12 -7
  41. data/lib/rubocop/cop/rspec/pending.rb +1 -1
  42. data/lib/rubocop/cop/rspec/pending_without_reason.rb +1 -1
  43. data/lib/rubocop/cop/rspec/predicate_matcher.rb +6 -6
  44. data/lib/rubocop/cop/rspec/rails/avoid_setup_hook.rb +1 -1
  45. data/lib/rubocop/cop/rspec/rails/have_http_status.rb +34 -10
  46. data/lib/rubocop/cop/rspec/rails/http_status.rb +1 -1
  47. data/lib/rubocop/cop/rspec/rails/minitest_assertions.rb +314 -22
  48. data/lib/rubocop/cop/rspec/receive_counts.rb +1 -1
  49. data/lib/rubocop/cop/rspec/receive_messages.rb +1 -1
  50. data/lib/rubocop/cop/rspec/redundant_predicate_matcher.rb +67 -0
  51. data/lib/rubocop/cop/rspec/remove_const.rb +40 -0
  52. data/lib/rubocop/cop/rspec/repeated_example_group_body.rb +1 -1
  53. data/lib/rubocop/cop/rspec/repeated_example_group_description.rb +2 -2
  54. data/lib/rubocop/cop/rspec/repeated_include_example.rb +1 -1
  55. data/lib/rubocop/cop/rspec/repeated_subject_call.rb +124 -0
  56. data/lib/rubocop/cop/rspec/return_from_stub.rb +1 -1
  57. data/lib/rubocop/cop/rspec/shared_context.rb +1 -1
  58. data/lib/rubocop/cop/rspec/shared_examples.rb +66 -20
  59. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +2 -3
  60. data/lib/rubocop/cop/rspec/stubbed_mock.rb +1 -1
  61. data/lib/rubocop/cop/rspec/subject_stub.rb +4 -4
  62. data/lib/rubocop/cop/rspec/unspecified_exception.rb +2 -2
  63. data/lib/rubocop/cop/rspec/variable_definition.rb +2 -2
  64. data/lib/rubocop/cop/rspec/verified_doubles.rb +1 -1
  65. data/lib/rubocop/cop/rspec/void_expect.rb +2 -2
  66. data/lib/rubocop/cop/rspec_cops.rb +4 -0
  67. data/lib/rubocop/rspec/language.rb +8 -8
  68. data/lib/rubocop/rspec/version.rb +1 -1
  69. data/lib/rubocop/rspec/wording.rb +8 -0
  70. metadata +10 -6
@@ -3,9 +3,13 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module RSpec
6
- # Enforces use of string to titleize shared examples.
6
+ # Checks for consistent style for shared example names.
7
7
  #
8
- # @example
8
+ # Enforces either `string` or `symbol` for shared example names.
9
+ #
10
+ # This cop can be configured using the `EnforcedStyle` option
11
+ #
12
+ # @example `EnforcedStyle: string` (default)
9
13
  # # bad
10
14
  # it_behaves_like :foo_bar_baz
11
15
  # it_should_behave_like :foo_bar_baz
@@ -20,33 +24,66 @@ module RuboCop
20
24
  # shared_examples_for 'foo bar baz'
21
25
  # include_examples 'foo bar baz'
22
26
  #
27
+ # @example `EnforcedStyle: symbol`
28
+ # # bad
29
+ # it_behaves_like 'foo bar baz'
30
+ # it_should_behave_like 'foo bar baz'
31
+ # shared_examples 'foo bar baz'
32
+ # shared_examples_for 'foo bar baz'
33
+ # include_examples 'foo bar baz'
34
+ #
35
+ # # good
36
+ # it_behaves_like :foo_bar_baz
37
+ # it_should_behave_like :foo_bar_baz
38
+ # shared_examples :foo_bar_baz
39
+ # shared_examples_for :foo_bar_baz
40
+ # include_examples :foo_bar_baz
41
+ #
23
42
  class SharedExamples < Base
24
43
  extend AutoCorrector
44
+ include ConfigurableEnforcedStyle
25
45
 
26
46
  # @!method shared_examples(node)
27
47
  def_node_matcher :shared_examples, <<~PATTERN
28
48
  {
29
- (send #rspec? #SharedGroups.all ...)
30
- (send nil? #Includes.all ...)
49
+ (send #rspec? #SharedGroups.all $_ ...)
50
+ (send nil? #Includes.all $_ ...)
31
51
  }
32
52
  PATTERN
33
53
 
34
54
  def on_send(node)
35
- shared_examples(node) do
36
- ast_node = node.first_argument
37
- next unless ast_node&.sym_type?
55
+ shared_examples(node) do |ast_node|
56
+ next unless offense?(ast_node)
38
57
 
39
- checker = Checker.new(ast_node)
40
- add_offense(checker.node, message: checker.message) do |corrector|
41
- corrector.replace(checker.node, checker.preferred_style)
58
+ checker = new_checker(ast_node)
59
+ add_offense(ast_node, message: checker.message) do |corrector|
60
+ corrector.replace(ast_node, checker.preferred_style)
42
61
  end
43
62
  end
44
63
  end
45
64
 
65
+ private
66
+
67
+ def offense?(ast_node)
68
+ if style == :symbol
69
+ ast_node.str_type?
70
+ else # string
71
+ ast_node.sym_type?
72
+ end
73
+ end
74
+
75
+ def new_checker(ast_node)
76
+ if style == :symbol
77
+ SymbolChecker.new(ast_node)
78
+ else # string
79
+ StringChecker.new(ast_node)
80
+ end
81
+ end
82
+
46
83
  # :nodoc:
47
- class Checker
84
+ class SymbolChecker
48
85
  MSG = 'Prefer %<prefer>s over `%<current>s` ' \
49
- 'to titleize shared examples.'
86
+ 'to symbolize shared examples.'
50
87
 
51
88
  attr_reader :node
52
89
 
@@ -55,22 +92,31 @@ module RuboCop
55
92
  end
56
93
 
57
94
  def message
58
- format(MSG, prefer: preferred_style, current: symbol.inspect)
95
+ format(MSG, prefer: preferred_style, current: node.value.inspect)
59
96
  end
60
97
 
61
98
  def preferred_style
62
- string = symbol.to_s.tr('_', ' ')
63
- wrap_with_single_quotes(string)
99
+ ":#{node.value.to_s.downcase.tr(' ', '_')}"
64
100
  end
101
+ end
102
+
103
+ # :nodoc:
104
+ class StringChecker
105
+ MSG = 'Prefer %<prefer>s over `%<current>s` ' \
106
+ 'to titleize shared examples.'
65
107
 
66
- private
108
+ attr_reader :node
67
109
 
68
- def symbol
69
- node.value
110
+ def initialize(node)
111
+ @node = node
70
112
  end
71
113
 
72
- def wrap_with_single_quotes(string)
73
- "'#{string}'"
114
+ def message
115
+ format(MSG, prefer: preferred_style, current: node.value.inspect)
116
+ end
117
+
118
+ def preferred_style
119
+ "'#{node.value.to_s.tr('_', ' ')}'"
74
120
  end
75
121
  end
76
122
  end
@@ -24,7 +24,7 @@ module RuboCop
24
24
  RESTRICT_ON_SEND = %i[receive_message_chain stub_chain].freeze
25
25
 
26
26
  # @!method message_chain(node)
27
- def_node_matcher :message_chain, <<-PATTERN
27
+ def_node_matcher :message_chain, <<~PATTERN
28
28
  (send _ {:receive_message_chain :stub_chain} $_)
29
29
  PATTERN
30
30
 
@@ -81,8 +81,7 @@ module RuboCop
81
81
  end
82
82
 
83
83
  def key_to_arg(node)
84
- key, = *node # rubocop:disable InternalAffairs/NodeDestructuring
85
- node.sym_type? ? ":#{key}" : node.source
84
+ node.sym_type? ? ":#{node.value}" : node.source
86
85
  end
87
86
 
88
87
  def replacement(method)
@@ -31,7 +31,7 @@ module RuboCop
31
31
  #
32
32
  # @param node [RuboCop::AST::Node]
33
33
  # @return [Array<RuboCop::AST::Node>] matching nodes
34
- def_node_matcher :message_expectation?, <<-PATTERN
34
+ def_node_matcher :message_expectation?, <<~PATTERN
35
35
  {
36
36
  (send nil? { :receive :receive_message_chain } ...) # receive(:foo)
37
37
  (send (send nil? :receive ...) :with ...) # receive(:foo).with('bar')
@@ -68,7 +68,7 @@ module RuboCop
68
68
  # @param node [RuboCop::AST::Node]
69
69
  #
70
70
  # @yield [Symbol] subject name
71
- def_node_matcher :subject?, <<-PATTERN
71
+ def_node_matcher :subject?, <<~PATTERN
72
72
  (block
73
73
  (send nil?
74
74
  { #Subjects.all (sym $_) | $#Subjects.all }
@@ -77,7 +77,7 @@ module RuboCop
77
77
 
78
78
  # @!method let?(node)
79
79
  # Find a memoized helper
80
- def_node_matcher :let?, <<-PATTERN
80
+ def_node_matcher :let?, <<~PATTERN
81
81
  (block
82
82
  (send nil? :let (sym $_)
83
83
  ) args ...)
@@ -94,7 +94,7 @@ module RuboCop
94
94
  # expect(foo).to receive(:bar).with(1)
95
95
  # expect(foo).to receive(:bar).with(1).and_return(2)
96
96
  #
97
- def_node_matcher :message_expectation?, <<-PATTERN
97
+ def_node_matcher :message_expectation?, <<~PATTERN
98
98
  (send
99
99
  {
100
100
  (send nil? { :expect :allow } (send nil? %))
@@ -106,7 +106,7 @@ module RuboCop
106
106
  PATTERN
107
107
 
108
108
  # @!method message_expectation_matcher?(node)
109
- def_node_search :message_expectation_matcher?, <<-PATTERN
109
+ def_node_search :message_expectation_matcher?, <<~PATTERN
110
110
  (send nil? {
111
111
  :receive :receive_messages :receive_message_chain :have_received
112
112
  } ...)
@@ -35,7 +35,7 @@ module RuboCop
35
35
  RESTRICT_ON_SEND = %i[to].freeze
36
36
 
37
37
  # @!method empty_raise_error_or_exception(node)
38
- def_node_matcher :empty_raise_error_or_exception, <<-PATTERN
38
+ def_node_matcher :empty_raise_error_or_exception, <<~PATTERN
39
39
  (send
40
40
  (block
41
41
  (send nil? :expect) ...)
@@ -57,7 +57,7 @@ module RuboCop
57
57
  end
58
58
 
59
59
  def block_with_args?(node)
60
- return unless node&.block_type?
60
+ return false unless node&.block_type?
61
61
 
62
62
  node.arguments?
63
63
  end
@@ -60,8 +60,8 @@ module RuboCop
60
60
  end
61
61
 
62
62
  def style_offense?(variable)
63
- style == :symbols && string?(variable) ||
64
- style == :strings && symbol?(variable)
63
+ (style == :symbols && string?(variable)) ||
64
+ (style == :strings && symbol?(variable))
65
65
  end
66
66
 
67
67
  def string?(node)
@@ -28,7 +28,7 @@ module RuboCop
28
28
  RESTRICT_ON_SEND = %i[double spy].freeze
29
29
 
30
30
  # @!method unverified_double(node)
31
- def_node_matcher :unverified_double, <<-PATTERN
31
+ def_node_matcher :unverified_double, <<~PATTERN
32
32
  {(send nil? {:double :spy} $...)}
33
33
  PATTERN
34
34
 
@@ -18,12 +18,12 @@ module RuboCop
18
18
  RESTRICT_ON_SEND = %i[expect].freeze
19
19
 
20
20
  # @!method expect?(node)
21
- def_node_matcher :expect?, <<-PATTERN
21
+ def_node_matcher :expect?, <<~PATTERN
22
22
  (send nil? :expect ...)
23
23
  PATTERN
24
24
 
25
25
  # @!method expect_block?(node)
26
- def_node_matcher :expect_block?, <<-PATTERN
26
+ def_node_matcher :expect_block?, <<~PATTERN
27
27
  (block #expect? (args) _body)
28
28
  PATTERN
29
29
 
@@ -78,6 +78,7 @@ require_relative 'rspec/implicit_subject'
78
78
  require_relative 'rspec/indexed_let'
79
79
  require_relative 'rspec/instance_spy'
80
80
  require_relative 'rspec/instance_variable'
81
+ require_relative 'rspec/is_expected_specify'
81
82
  require_relative 'rspec/it_behaves_like'
82
83
  require_relative 'rspec/iterated_expectation'
83
84
  require_relative 'rspec/leading_subject'
@@ -106,11 +107,14 @@ require_relative 'rspec/receive_counts'
106
107
  require_relative 'rspec/receive_messages'
107
108
  require_relative 'rspec/receive_never'
108
109
  require_relative 'rspec/redundant_around'
110
+ require_relative 'rspec/redundant_predicate_matcher'
111
+ require_relative 'rspec/remove_const'
109
112
  require_relative 'rspec/repeated_description'
110
113
  require_relative 'rspec/repeated_example'
111
114
  require_relative 'rspec/repeated_example_group_body'
112
115
  require_relative 'rspec/repeated_example_group_description'
113
116
  require_relative 'rspec/repeated_include_example'
117
+ require_relative 'rspec/repeated_subject_call'
114
118
  require_relative 'rspec/return_from_stub'
115
119
  require_relative 'rspec/scattered_let'
116
120
  require_relative 'rspec/scattered_setup'
@@ -42,7 +42,7 @@ module RuboCop
42
42
  PATTERN
43
43
 
44
44
  # @!method example_group_with_body?(node)
45
- def_node_matcher :example_group_with_body?, <<-PATTERN
45
+ def_node_matcher :example_group_with_body?, <<~PATTERN
46
46
  (block (send #rspec? #ExampleGroups.all ...) args !nil?)
47
47
  PATTERN
48
48
 
@@ -50,15 +50,15 @@ module RuboCop
50
50
  def_node_matcher :example?, '(block (send nil? #Examples.all ...) ...)'
51
51
 
52
52
  # @!method hook?(node)
53
- def_node_matcher :hook?, <<-PATTERN
54
- {
55
- (numblock (send nil? #Hooks.all ...) ...)
56
- (block (send nil? #Hooks.all ...) ...)
57
- }
53
+ def_node_matcher :hook?, <<~PATTERN
54
+ {
55
+ (numblock (send nil? #Hooks.all ...) ...)
56
+ (block (send nil? #Hooks.all ...) ...)
57
+ }
58
58
  PATTERN
59
59
 
60
60
  # @!method let?(node)
61
- def_node_matcher :let?, <<-PATTERN
61
+ def_node_matcher :let?, <<~PATTERN
62
62
  {
63
63
  (block (send nil? #Helpers.all ...) ...)
64
64
  (send nil? #Helpers.all _ block_pass)
@@ -66,7 +66,7 @@ module RuboCop
66
66
  PATTERN
67
67
 
68
68
  # @!method include?(node)
69
- def_node_matcher :include?, <<-PATTERN
69
+ def_node_matcher :include?, <<~PATTERN
70
70
  {
71
71
  (block (send nil? #Includes.all ...) ...)
72
72
  (send nil? #Includes.all ...)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module RSpec
5
5
  # Version information for the RSpec RuboCop plugin.
6
6
  module Version
7
- STRING = '2.25.0'
7
+ STRING = '2.27.1'
8
8
  end
9
9
  end
10
10
  end
@@ -6,6 +6,8 @@ module RuboCop
6
6
  class Wording
7
7
  SHOULDNT_PREFIX = /\Ashould(?:n't| not)\b/i.freeze
8
8
  SHOULDNT_BE_PREFIX = /#{SHOULDNT_PREFIX} be\b/i.freeze
9
+ WILL_NOT_PREFIX = /\Awill not\b/i.freeze
10
+ WONT_PREFIX = /\Awon't\b/i.freeze
9
11
  ES_SUFFIX_PATTERN = /(?:o|s|x|ch|sh|z)\z/i.freeze
10
12
  IES_SUFFIX_PATTERN = /[^aeou]y\z/i.freeze
11
13
 
@@ -15,16 +17,22 @@ module RuboCop
15
17
  @replacements = replace
16
18
  end
17
19
 
20
+ # rubocop:disable Metrics/MethodLength
18
21
  def rewrite
19
22
  case text
20
23
  when SHOULDNT_BE_PREFIX
21
24
  replace_prefix(SHOULDNT_BE_PREFIX, 'is not')
22
25
  when SHOULDNT_PREFIX
23
26
  replace_prefix(SHOULDNT_PREFIX, 'does not')
27
+ when WILL_NOT_PREFIX
28
+ replace_prefix(WILL_NOT_PREFIX, 'does not')
29
+ when WONT_PREFIX
30
+ replace_prefix(WONT_PREFIX, 'does not')
24
31
  else
25
32
  remove_should_and_pluralize
26
33
  end
27
34
  end
35
+ # rubocop:enable Metrics/MethodLength
28
36
 
29
37
  private
30
38
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.25.0
4
+ version: 2.27.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Backus
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2023-10-27 00:00:00.000000000 Z
13
+ date: 2024-03-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -54,9 +54,9 @@ dependencies:
54
54
  - - "~>"
55
55
  - !ruby/object:Gem::Version
56
56
  version: '2.22'
57
- description: |2
58
- Code style checking for RSpec files.
59
- A plugin for the RuboCop code style enforcing & linting tool.
57
+ description: |
58
+ Code style checking for RSpec files.
59
+ A plugin for the RuboCop code style enforcing & linting tool.
60
60
  email:
61
61
  - johncbackus@gmail.com
62
62
  - ian@nevir.net
@@ -139,6 +139,7 @@ files:
139
139
  - lib/rubocop/cop/rspec/indexed_let.rb
140
140
  - lib/rubocop/cop/rspec/instance_spy.rb
141
141
  - lib/rubocop/cop/rspec/instance_variable.rb
142
+ - lib/rubocop/cop/rspec/is_expected_specify.rb
142
143
  - lib/rubocop/cop/rspec/it_behaves_like.rb
143
144
  - lib/rubocop/cop/rspec/iterated_expectation.rb
144
145
  - lib/rubocop/cop/rspec/leading_subject.rb
@@ -185,11 +186,14 @@ files:
185
186
  - lib/rubocop/cop/rspec/receive_messages.rb
186
187
  - lib/rubocop/cop/rspec/receive_never.rb
187
188
  - lib/rubocop/cop/rspec/redundant_around.rb
189
+ - lib/rubocop/cop/rspec/redundant_predicate_matcher.rb
190
+ - lib/rubocop/cop/rspec/remove_const.rb
188
191
  - lib/rubocop/cop/rspec/repeated_description.rb
189
192
  - lib/rubocop/cop/rspec/repeated_example.rb
190
193
  - lib/rubocop/cop/rspec/repeated_example_group_body.rb
191
194
  - lib/rubocop/cop/rspec/repeated_example_group_description.rb
192
195
  - lib/rubocop/cop/rspec/repeated_include_example.rb
196
+ - lib/rubocop/cop/rspec/repeated_subject_call.rb
193
197
  - lib/rubocop/cop/rspec/return_from_stub.rb
194
198
  - lib/rubocop/cop/rspec/scattered_let.rb
195
199
  - lib/rubocop/cop/rspec/scattered_setup.rb
@@ -249,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
253
  - !ruby/object:Gem::Version
250
254
  version: '0'
251
255
  requirements: []
252
- rubygems_version: 3.4.17
256
+ rubygems_version: 3.4.21
253
257
  signing_key:
254
258
  specification_version: 4
255
259
  summary: Code style checking for RSpec files