Skip to content

Commit 954a45f

Browse files
authored
Merge pull request #1984 from rubocop/fixcbz
Fix an error for `RSpec/ChangeByZero` when `change (...) .by (0)` and `change (...)`, concatenated with `and` and `or`
2 parents ff444c2 + 130c564 commit 954a45f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG.md

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

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

78
## 3.1.0 (2024-10-01)
89

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)

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

0 commit comments

Comments
 (0)