Class: RuboCop::Cop::RSpec::ChangeByZero
- Extended by:
- AutoCorrector
- 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 change.by(0)`.'
- MSG_COMPOUND =
'Prefer %<preferred>s with compound expectations ' \ 'over `change.by(0)`.'
- RESTRICT_ON_SEND =
%i[change].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::NodePattern
#block_pattern, #numblock_pattern, #send_pattern
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#change_nodes(node) ⇒ Object
85 86 87 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 85 def_node_search :change_nodes, <<-PATTERN $(send nil? :change ...) PATTERN |
#expect_change_with_arguments(node) ⇒ Object
68 69 70 71 72 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 68 def_node_matcher :expect_change_with_arguments, <<-PATTERN (send (send nil? :change ...) :by (int 0)) PATTERN |
#expect_change_with_block(node) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 75 def_node_matcher :expect_change_with_block, <<-PATTERN (send (block (send nil? :change) (args) (send (...) $_)) :by (int 0)) PATTERN |
#on_send(node) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/rubocop/cop/rspec/change_by_zero.rb', line 89 def on_send(node) expect_change_with_arguments(node.parent) do check_offense(node.parent) end expect_change_with_block(node.parent.parent) do check_offense(node.parent.parent) end end |