8000 Merge branch 'master' into require_void_expect_acts_inside_an_example… · rubocop/rubocop-rspec@427d19d · GitHub
[go: up one dir, main page]

Skip to content

Commit 427d19d

Browse files
Merge branch 'master' into require_void_expect_acts_inside_an_example_block
2 parents 7e3149d + 954a45f commit 427d19d

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## Master (Unreleased)
44

55
- Fix `RSpec/VoidExpect` to only operate inside an example block. ([@corsonknowles])
6+
- Change `RSpec/ContextWording` cop to always report an offense when both `Prefixes` and `AllowedPatterns` are empty. ([@ydah])
7+
- Fix an error for `RSpec/ChangeByZero` when `change (...) .by (0)` and `change (...)`, concatenated with `and` and `or`. ([@ydah])
68

79
## 3.1.0 (2024-10-01)
810

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ the configuration to meet project needs. Other acceptable prefixes may
720720
include `if`, `unless`, `for`, `before`, `after`, or `during`.
721721
They may consist of multiple words if desired.
722722
723+
If both `Prefixes` and `AllowedPatterns` are empty, this cop will always
724+
report an offense. So you need to set at least one of them.
725+
723726
This cop can be customized allowed context description pattern
724727
with `AllowedPatterns`. By default, there are no checking by pattern.
725728

lib/rubocop/cop/rspec/change_by_zero.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ def register_offense(node, change_node)
118118
# rubocop:enable Metrics/MethodLength
119119

120120
def compound_expectations?(node)
121-
%i[and or & |].include?(node.parent.method_name)
121+
if node.parent.send_type?
122+
%i[and or & |].include?(node.parent.method_name)
123+
else
124+
node.parent.and_type? || node.parent.or_type?
125+
end
122126
end
123127

124128
def message(change_node)

lib/rubocop/cop/rspec/context_wording.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ module RSpec
1212
#
1313
# @see http://www.betterspecs.org/#contexts
1414
#
15+
# If both `Prefixes` and `AllowedPatterns` are empty, this cop will always
16+
# report an offense. So you need to set at least one of them.
17+
#
1518
# @example `Prefixes` configuration
1619
# # .rubocop.yml
1720
# # RSpec/ContextWording:
@@ -58,7 +61,9 @@ module RSpec
5861
class ContextWording < Base
5962
include AllowedPattern
6063

61-
MSG = 'Context description should match %<patterns>s.'
64+
MSG_MATCH = 'Context description should match %<patterns>s.'
65+
MSG_ALWAYS = 'Current settings will always report an offense. Please ' \
66+
'add allowed words to `Prefixes` or `AllowedPatterns`.'
6267

6368
# @!method context_wording(node)
6469
def_node_matcher :context_wording, <<~PATTERN
@@ -67,8 +72,7 @@ class ContextWording < Base
6772

6873
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
6974
context_wording(node) do |context|
70-
if bad_pattern?(context)
71-
message = format(MSG, patterns: expect_patterns)
75+
unless matches_allowed_pattern?(description(context))
7276
add_offense(context, message: message)
7377
end
7478
end
@@ -84,12 +88,6 @@ def prefix_regexes
8488
@prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ }
8589
end
8690

87-
def bad_pattern?(node)
88-
return false if allowed_patterns.empty?
89-
90-
!matches_allowed_pattern?(description(node))
91-
end
92-
9391
def description(context)
9492
if context.xstr_type?
9593
context.value.value
@@ -98,6 +96,14 @@ def description(context)
9896
end
9997
end
10098

99+
def message
100+
if allowed_patterns.empty?
101+
MSG_ALWAYS
102+
else
103+
format(MSG_MATCH, patterns: expect_patterns)
104+
end
105+
end
106+
101107
def expect_patterns
102108
inspected = allowed_patterns.map do |pattern|
103109
pattern.inspect.gsub(/\A"|"\z/, '/')

spec/rubocop/cop/rspec/change_by_zero_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
expect { foo }.to change { Foo.bar }.by(0).and change { Foo.baz }.by(0)
6969
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
7070
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
71+
expect { foo }.to change(Foo, :bar).by(0).and change(Foo, :baz)
72+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
73+
expect { foo }.to change { Foo.bar }.and change { Foo.baz }.by(0)
74+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
7175
end
7276
RUBY
7377

@@ -84,6 +88,10 @@
8488
expect { foo }.to change { Foo.bar }.by(0) & change { Foo.baz }.by(0)
8589
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
8690
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
91+
expect { foo }.to change(Foo, :bar).by(0) & change(Foo, :baz)
92+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
93+
expect { foo }.to change { Foo.bar } & change { Foo.baz }.by(0)
94+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
8795
end
8896
RUBY
8997

@@ -100,6 +108,10 @@
100108
expect { foo }.to change { Foo.bar }.by(0).or change { Foo.baz }.by(0)
101109
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
102110
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
111+
expect { foo }.to change(Foo, :bar).by(0).or change(Foo, :baz)
112+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
113+
expect { foo }.to change { Foo.bar }.or change { Foo.baz }.by(0)
114+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
103115
end
104116
RUBY
105117

@@ -116,6 +128,10 @@
116128
expect { foo }.to change { Foo.bar }.by(0) | change { Foo.baz }.by(0)
117129
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
118130
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
131+
expect { foo }.to change(Foo, :bar) | change(Foo, :baz).by(0)
132+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
133+
expect { foo }.to change { Foo.bar }.by(0) | change { Foo.baz }
134+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
119135
end
120136
RUBY
121137

@@ -244,6 +260,14 @@
244260
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
245261
.and change { Foo.baz }.by(0)
246262
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
263+
expect { foo }
264+
.to change(Foo, :bar)
265+
.and change(Foo, :baz).by(0)
266+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
267+
expect { foo }
268+
.to change { Foo.bar }
269+
.and change { Foo.baz }.by(0)
270+
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
247271
end
248272
RUBY
249273

@@ -255,6 +279,12 @@
255279
expect { foo }
256280
.to not_change { Foo.bar }
257281
.and not_change { Foo.baz }
282+
expect { foo }
283+
.to change(Foo, :bar)
284+
.and not_change(Foo, :baz)
285+
expect { foo }
286+
.to change { Foo.bar }
287+
.and not_change { Foo.baz }
258288
end
259289
RUBY
260290
end

spec/rubocop/cop/rspec/context_wording_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,18 @@
214214
end
215215
end
216216
end
217+
218+
context 'when `AllowedPatterns:` and `Prefixes:` are both empty' do
219+
let(:cop_config) do
220+
{ 'Prefixes' => [], 'AllowedPatterns' => [] }
221+
end
222+
223+
it 'always registers an offense' do
224+
expect_offense(<<~RUBY)
225+
context 'this is an incorrect context' do
226+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Current settings will always report an offense. Please add allowed words to `Prefixes` or `AllowedPatterns`.
227+
end
228+
RUBY
229+
end
230+
end
217231
end

0 commit comments

Comments
 (0)
0