class RuboCop::Cop::RSpec::ContextWording
end
# …
context ‘条件を満たすとき’ do
# good
end
# …
context ‘条件を満たす’ do
# bad
@example
# - とき$
# AllowedPatterns:
# RSpec/ContextWording:
# .rubocop.yml
@example ‘AllowedPatterns` configuration
with `AllowedPatterns`. By default, there are no checking by pattern.
This cop can be customized allowed context description pattern
end
# …
context ’when the display name is not present’ do
# good
end
# …
context ‘the display name not present’ do
# bad
@example
# - for
# - unless
# - if
# - without
# - with
# - when
# Prefixes:
# RSpec/ContextWording:
# .rubocop.yml
@example ‘Prefixes` configuration
report an offense. So you need to set at least one of them.
If both `Prefixes` and `AllowedPatterns` are empty, this cop will always
@see www.betterspecs.org/#contexts<br><br>They may consist of multiple words if desired.
include `if`, `unless`, `for`, `before`, `after`, or `during`.
the configuration to meet project needs. Other acceptable prefixes may
The default list of prefixes is minimal. Users are encouraged to tailor
Checks that `context` docstring starts with an allowed prefix.
def allowed_patterns
def allowed_patterns super + prefix_regexes end
def description(context)
def description(context) if context.xstr_type? context.value.value else context.value end end
def expect_patterns
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
def message
def message if allowed_patterns.empty? MSG_ALWAYS else format(MSG_MATCH, patterns: expect_patterns) end end
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
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
def prefix_regexes
def prefix_regexes @prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ } end
def prefixes
def prefixes Array(cop_config.fetch('Prefixes', [])).tap do |prefixes| non_strings = prefixes.reject { |pre| pre.is_a?(String) } unless non_strings.empty? raise "Non-string prefixes #{non_strings.inspect} detected." end end end