Class: RuboCop::Cop::RSpec::SharedExamples

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rspec/shared_examples.rb

Overview

Checks for consistent style for shared example names.

Enforces either ‘string` or `symbol` for shared example names.

This cop can be configured using the ‘EnforcedStyle` option

Examples:

‘EnforcedStyle: string` (default)

# bad
it_behaves_like :foo_bar_baz
it_should_behave_like :foo_bar_baz
shared_examples :foo_bar_baz
shared_examples_for :foo_bar_baz
include_examples :foo_bar_baz

# good
it_behaves_like 'foo bar baz'
it_should_behave_like 'foo bar baz'
shared_examples 'foo bar baz'
shared_examples_for 'foo bar baz'
include_examples 'foo bar baz'

‘EnforcedStyle: symbol`

# bad
it_behaves_like 'foo bar baz'
it_should_behave_like 'foo bar baz'
shared_examples 'foo bar baz'
shared_examples_for 'foo bar baz'
include_examples 'foo bar baz'

# good
it_behaves_like :foo_bar_baz
it_should_behave_like :foo_bar_baz
shared_examples :foo_bar_baz
shared_examples_for :foo_bar_baz
include_examples :foo_bar_baz

Defined Under Namespace

Classes: StringChecker, SymbolChecker

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

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

#on_send(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/rspec/shared_examples.rb', line 54

def on_send(node)
  shared_examples(node) do |ast_node|
    next unless offense?(ast_node)

    checker = new_checker(ast_node)
    add_offense(ast_node, message: checker.message) do |corrector|
      corrector.replace(ast_node, checker.preferred_style)
    end
  end
end

#shared_examples(node) ⇒ Object



47
48
49
50
51
52
# File 'lib/rubocop/cop/rspec/shared_examples.rb', line 47

def_node_matcher :shared_examples, <<~PATTERN
  {
    (send #rspec? #SharedGroups.all $_ ...)
    (send nil? #Includes.all $_ ...)
  }
PATTERN