Class: RuboCop::Cop::RSpec::SharedContext
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rspec/shared_context.rb
Overview
Checks for proper shared_context and shared_examples usage.
If there are no examples defined, use shared_context. If there is no setup defined, use shared_examples.
Constant Summary collapse
- MSG_EXAMPLES =
"Use `shared_examples` when you don't define context."
- MSG_CONTEXT =
"Use `shared_context` when you don't define examples."
Instance Method Summary collapse
- #context?(node) ⇒ Object
- #examples?(node) ⇒ Object
-
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler.
- #shared_context(node) ⇒ Object
- #shared_example(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
#context?(node) ⇒ Object
65 66 67 68 69 |
# File 'lib/rubocop/cop/rspec/shared_context.rb', line 65 def_node_search :context?, <<~PATTERN (send nil? {#Subjects.all #Helpers.all #Includes.context #Hooks.all} ... ) PATTERN |
#examples?(node) ⇒ Object
60 61 62 |
# File 'lib/rubocop/cop/rspec/shared_context.rb', line 60 def_node_search :examples?, <<~PATTERN (send nil? {#Includes.examples #Examples.all} ...) PATTERN |
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubocop/cop/rspec/shared_context.rb', line 81 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler context_with_only_examples(node) do add_offense(node.send_node, message: MSG_EXAMPLES) do |corrector| corrector.replace(node.send_node.loc.selector, 'shared_examples') end end examples_with_only_context(node) do add_offense(node.send_node, message: MSG_CONTEXT) do |corrector| corrector.replace(node.send_node.loc.selector, 'shared_context') end end end |
#shared_context(node) ⇒ Object
72 73 74 |
# File 'lib/rubocop/cop/rspec/shared_context.rb', line 72 def_node_matcher :shared_context, <<~PATTERN (block (send #rspec? #SharedGroups.context ...) ...) PATTERN |
#shared_example(node) ⇒ Object
77 78 79 |
# File 'lib/rubocop/cop/rspec/shared_context.rb', line 77 def_node_matcher :shared_example, <<~PATTERN (block (send #rspec? #SharedGroups.examples ...) ...) PATTERN |