Skip to content

Commit

Permalink
Fix an error for RSpec/ChangeByZero when change (...) .by (0) and…
Browse files Browse the repository at this point in the history
… `change (...)`, concatenated with `and` and `or`

fix: #1983
  • Loading branch information
ydah committed Oct 23, 2024
1 parent ff444c2 commit 130c564
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Master (Unreleased)

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

## 3.1.0 (2024-10-01)

Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/cop/rspec/change_by_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ def register_offense(node, change_node)
# rubocop:enable Metrics/MethodLength

def compound_expectations?(node)
%i[and or & |].include?(node.parent.method_name)
if node.parent.send_type?
%i[and or & |].include?(node.parent.method_name)
else
node.parent.and_type? || node.parent.or_type?
end
end

def message(change_node)
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/rspec/change_by_zero_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
expect { foo }.to change { Foo.bar }.by(0).and change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change(Foo, :bar).by(0).and change(Foo, :baz)
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change { Foo.bar }.and change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
end
RUBY

Expand All @@ -84,6 +88,10 @@
expect { foo }.to change { Foo.bar }.by(0) & change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change(Foo, :bar).by(0) & change(Foo, :baz)
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change { Foo.bar } & change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
end
RUBY

Expand All @@ -100,6 +108,10 @@
expect { foo }.to change { Foo.bar }.by(0).or change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change(Foo, :bar).by(0).or change(Foo, :baz)
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change { Foo.bar }.or change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
end
RUBY

Expand All @@ -116,6 +128,10 @@
expect { foo }.to change { Foo.bar }.by(0) | change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change(Foo, :bar) | change(Foo, :baz).by(0)
^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
expect { foo }.to change { Foo.bar }.by(0) | change { Foo.baz }
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`.
end
RUBY

Expand Down Expand Up @@ -244,6 +260,14 @@
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
.and change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
expect { foo }
.to change(Foo, :bar)
.and change(Foo, :baz).by(0)
^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
expect { foo }
.to change { Foo.bar }
.and change { Foo.baz }.by(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_change` with compound expectations over `change.by(0)`.
end
RUBY

Expand All @@ -255,6 +279,12 @@
expect { foo }
.to not_change { Foo.bar }
.and not_change { Foo.baz }
expect { foo }
.to change(Foo, :bar)
.and not_change(Foo, :baz)
expect { foo }
.to change { Foo.bar }
.and not_change { Foo.baz }
end
RUBY
end
Expand Down

0 comments on commit 130c564

Please sign in to comment.