lib/rubocop/cop/rspec/context_wording.rb in rubocop-rspec-2.2.0 vs lib/rubocop/cop/rspec/context_wording.rb in rubocop-rspec-2.3.0

- old
+ new

@@ -6,10 +6,11 @@ # 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 https://rspec.rubystyle.guide/#context-descriptions # @see http://www.betterspecs.org/#contexts # # @example `Prefixes` configuration @@ -35,10 +36,11 @@ # # ... # end class ContextWording < Base MSG = 'Start context description with %<prefixes>s.' + # @!method context_wording(node) def_node_matcher :context_wording, <<-PATTERN (block (send #rspec? { :context :shared_context } $(str #bad_prefix?) ...) ...) PATTERN def on_block(node) @@ -49,11 +51,11 @@ end private def bad_prefix?(description) - !prefixes.include?(description.split(/\b/).first) + !prefix_regex.match?(description) end def joined_prefixes quoted = prefixes.map { |prefix| "'#{prefix}'" } return quoted.first if quoted.size == 1 @@ -62,9 +64,13 @@ quoted.join(', ') end def prefixes cop_config['Prefixes'] || [] + end + + def prefix_regex + /^#{Regexp.union(prefixes)}\b/ end end end end end