class RuboCop::Cop::RSpec::SharedExamples

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

@example ‘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'

@example ‘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

Public Instance Methods

on_send(node) click to toggle source
# 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

Private Instance Methods

new_checker(ast_node) click to toggle source
# File lib/rubocop/cop/rspec/shared_examples.rb, line 75
def new_checker(ast_node)
  if style == :symbol
    SymbolChecker.new(ast_node)
  else # string
    StringChecker.new(ast_node)
  end
end
offense?(ast_node) click to toggle source
# File lib/rubocop/cop/rspec/shared_examples.rb, line 67
def offense?(ast_node)
  if style == :symbol
    ast_node.str_type?
  else # string
    ast_node.sym_type?
  end
end