lib/rubocop/cop/rspec/context_wording.rb in rubocop-rspec-3.1.0 vs lib/rubocop/cop/rspec/context_wording.rb in rubocop-rspec-3.2.0
- old
+ new
@@ -10,10 +10,13 @@
# include `if`, `unless`, `for`, `before`, `after`, or `during`.
# They may consist of multiple words if desired.
#
# @see http://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
@@ -56,21 +59,22 @@
# end
#
class ContextWording < Base
include AllowedPattern
- MSG = 'Context description should match %<patterns>s.'
+ MSG_MATCH = 'Context description should match %<patterns>s.'
+ MSG_ALWAYS = 'Current settings will always report an offense. Please ' \
+ 'add allowed words to `Prefixes` or `AllowedPatterns`.'
# @!method context_wording(node)
def_node_matcher :context_wording, <<~PATTERN
(block (send #rspec? { :context :shared_context } $({str dstr xstr} ...) ...) ...)
PATTERN
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
context_wording(node) do |context|
- if bad_pattern?(context)
- message = format(MSG, patterns: expect_patterns)
+ unless matches_allowed_pattern?(description(context))
add_offense(context, message: message)
end
end
end
@@ -82,20 +86,22 @@
def prefix_regexes
@prefix_regexes ||= prefixes.map { |pre| /^#{Regexp.escape(pre)}\b/ }
end
- def bad_pattern?(node)
- return false if allowed_patterns.empty?
-
- !matches_allowed_pattern?(description(node))
- end
-
def description(context)
if context.xstr_type?
context.value.value
else
context.value
+ end
+ end
+
+ def message
+ if allowed_patterns.empty?
+ MSG_ALWAYS
+ else
+ format(MSG_MATCH, patterns: expect_patterns)
end
end
def expect_patterns
inspected = allowed_patterns.map do |pattern|