Class: RuboCop::Cop::RSpec::ChangeByZero
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp
- Defined in:
- lib/rubocop/cop/rspec/change_by_zero.rb
Overview
Prefer negated matchers over ‘to change.by(0)`.
In the case of composite expectations, cop suggest using the negation matchers of ‘RSpec::Matchers#change`.
By default the cop does not support autocorrect of compound expectations, but if you set the negated matcher for ‘change`, e.g. `not_change` with the `NegatedMatcher` option, the cop will perform the autocorrection.
Constant Summary collapse
- MSG =
'Prefer `not_to change` over `to %<method>s.by(0)`.'
- MSG_COMPOUND =
'Prefer %<preferred>s with compound expectations ' \ 'over `%<method>s.by(0)`.'
- CHANGE_METHODS =
Set[:change, :a_block_changing, :changing].freeze
- RESTRICT_ON_SEND =
CHANGE_METHODS.freeze
Instance Method Summary collapse
- #change_nodes(node) ⇒ Object
- #expect_change_with_arguments(node) ⇒ Object
- #expect_change_with_block(node) ⇒ Object
- #on_send(node) ⇒ Object
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#change_nodes(node) ⇒ Object
88 89 90 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 88 def_node_search :change_nodes, <<~PATTERN $(send nil? CHANGE_METHODS ...) PATTERN |
#expect_change_with_arguments(node) ⇒ Object
71 72 73 74 75 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 71 def_node_matcher :expect_change_with_arguments, <<~PATTERN (send $(send nil? CHANGE_METHODS ...) :by (int 0)) PATTERN |
#expect_change_with_block(node) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 78 def_node_matcher :expect_change_with_block, <<~PATTERN (send (block $(send nil? CHANGE_METHODS) (args) (send (...) _)) :by (int 0)) PATTERN |
#on_send(node) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 92 def on_send(node) expect_change_with_arguments(node.parent) do |change| register_offense(node.parent, change) end expect_change_with_block(node.parent.parent) do |change| register_offense(node.parent.parent, change) end end |