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 www.betterspecs.org/#contexts
If both ‘Prefixes` and `AllowedPatterns` are empty, this cop will always report an offense. So you need to set at least one of them.
@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
This cop can be customized allowed context description pattern with ‘AllowedPatterns`. By default, there are no checking by pattern.
@example ‘AllowedPatterns` configuration
# .rubocop.yml # RSpec/ContextWording: # AllowedPatterns: # - とき$
@example
# bad context '条件を満たす' do # ... end # good context '条件を満たすとき' do # ... end
Constants
- MSG_ALWAYS
- MSG_MATCH
Public Instance Methods
on_block(node)
click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 73 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler context_wording(node) do |context| unless matches_allowed_pattern?(description(context)) add_offense(context, message: message) end end end
Private Instance Methods
allowed_patterns()
click to toggle source
Calls superclass method
# File lib/rubocop/cop/rspec/context_wording.rb, line 83 def allowed_patterns super + prefix_regexes end
description(context)
click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 91 def description(context) if context.xstr_type? context.value.value else context.value end end
expect_patterns()
click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 107 def expect_patterns inspected = allowed_patterns.map do |pattern| pattern.inspect.gsub(/\A"|"\z/, '/') end return inspected.first if inspected.size == 1 inspected << "or #{inspected.pop}" inspected.join(', ') end
message()
click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 99 def message if allowed_patterns.empty? MSG_ALWAYS else format(MSG_MATCH, patterns: expect_patterns) end end
prefix_regexes()
click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 87 def prefix_regexes @prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ } end
prefixes()
click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 117 def prefixes Array(cop_config.fetch('Prefixes', [])) end