lib/rubocop/cop/rspec/file_path.rb in rubocop-rspec-1.4.1 vs lib/rubocop/cop/rspec/file_path.rb in rubocop-rspec-1.5.0

- old
+ new

@@ -5,12 +5,12 @@ module Cop module RSpec # Checks the path of the spec file and enforces that it reflects the # described class/module and its optionally called out method. # - # With the configuration option `CustomTransform` modules or clases can be - # specified that should not as usual be transformed from CamelCase to + # With the configuration option `CustomTransform` modules or classes can + # be specified that should not as usual be transformed from CamelCase to # snake_case (e.g. 'RuboCop' => 'rubocop' ). # # @example # my_class/method_spec.rb # describe MyClass, '#method' # my_class_method_spec.rb # describe MyClass, '#method' @@ -18,12 +18,15 @@ class FilePath < Cop include RuboCop::RSpec::TopLevelDescribe MESSAGE = 'Spec path should end with `%s`'.freeze METHOD_STRING_MATCHER = /^[\#\.].+/ + ROUTING_PAIR = s(:pair, s(:sym, :type), s(:sym, :routing)) def on_top_level_describe(node, args) + return if routing_spec?(args) + return unless single_top_level_describe? object = args.first.const_name return unless object path_matcher = matcher(object, args[1]) @@ -32,10 +35,17 @@ add_offense(node, :expression, format(MESSAGE, path_matcher)) end private + def routing_spec?(args) + args[1..-1].any? do |arg| + next unless arg.hash_type? + arg.children.include?(ROUTING_PAIR) + end + end + def matcher(object, method) path = File.join(parts(object)) if method && method.type == :str path += '*' + method.children.first.gsub(/\W+/, '') end @@ -54,10 +64,10 @@ end def camel_to_underscore(string) string.dup.tap do |result| result.gsub!(/([^A-Z])([A-Z]+)/, '\\1_\\2') - result.gsub!(/([A-Z])([A-Z][^A-Z]+)/, '\\1_\\2') + result.gsub!(/([A-Z])([A-Z\d][^A-Z\d]+)/, '\\1_\\2') result.downcase! end end def regexp_from_glob(glob)