Class: RuboCop::Cop::RSpec::ExpectChange
- Extended by:
- AutoCorrector
- Includes:
- ConfigurableEnforcedStyle
- Defined in:
- lib/rubocop/cop/rspec/expect_change.rb
Overview
Checks for consistent style of change matcher.
Enforces either passing object and attribute as arguments to the matcher or passing a block that reads the attribute value.
This cop can be configured using the ‘EnforcedStyle` option.
Constant Summary collapse
- MSG_BLOCK =
'Prefer `change(%<obj>s, :%<attr>s)`.'
- MSG_CALL =
'Prefer `change { %<obj>s.%<attr>s }`.'
- RESTRICT_ON_SEND =
%i[change].freeze
Instance Method Summary collapse
- #expect_change_with_arguments(node) ⇒ Object
- #expect_change_with_block(node) ⇒ Object
- #on_block(node) ⇒ Object
- #on_send(node) ⇒ Object
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language::NodePattern
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#expect_change_with_arguments(node) ⇒ Object
41 42 43 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 41 def_node_matcher :expect_change_with_arguments, <<-PATTERN (send nil? :change $_ (sym $_)) PATTERN |
#expect_change_with_block(node) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 46 def_node_matcher :expect_change_with_block, <<-PATTERN (block (send nil? :change) (args) (send ({const send} nil? $_) $_) ) PATTERN |
#on_block(node) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 66 def on_block(node) return unless style == :method_call expect_change_with_block(node) do |receiver, | msg = format(MSG_BLOCK, obj: receiver, attr: ) add_offense(node, message: msg) do |corrector| replacement = "change(#{receiver}, :#{})" corrector.replace(node, replacement) end end end |
#on_send(node) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubocop/cop/rspec/expect_change.rb', line 54 def on_send(node) return unless style == :block expect_change_with_arguments(node) do |receiver, | msg = format(MSG_CALL, obj: receiver.source, attr: ) add_offense(node, message: msg) do |corrector| replacement = "change { #{receiver.source}.#{} }" corrector.replace(node, replacement) end end end |