class RuboCop::Cop::RSpec::ContextWording

Checks that `context` docstring starts with an allowed prefix.

The default list of prefixes is minimal. Users are encouraged to tailor the configuration to meet project needs. Other acceptable prefixes may include `if`, `unless`, `for`, `before`, `after`, or `during`. They may consist of multiple words if desired.

@see rspec.rubystyle.guide/#context-descriptions @see www.betterspecs.org/#contexts

@example `Prefixes` configuration

# .rubocop.yml
# RSpec/ContextWording:
#   Prefixes:
#     - when
#     - with
#     - without
#     - if
#     - unless
#     - for

@example

# bad
context 'the display name not present' do
  # ...
end

# good
context 'when the display name is not present' do
  # ...
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 46
def on_block(node)
  context_wording(node) do |context|
    add_offense(context,
                message: format(MSG, prefixes: joined_prefixes))
  end
end

Private Instance Methods

bad_prefix?(description) click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 55
def bad_prefix?(description)
  !prefix_regex.match?(description)
end
joined_prefixes() click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 59
def joined_prefixes
  quoted = prefixes.map { |prefix| "'#{prefix}'" }
  return quoted.first if quoted.size == 1

  quoted << "or #{quoted.pop}"
  quoted.join(', ')
end
prefix_regex() click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 71
def prefix_regex
  /^#{Regexp.union(prefixes)}\b/
end
prefixes() click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 67
def prefixes
  cop_config['Prefixes'] || []
end