Skip to content

Commit

Permalink
Memoizes the regular expressions for path matching
Browse files Browse the repository at this point in the history
  • Loading branch information
alindeman committed Oct 27, 2013
1 parent 817f47d commit d9c9e16
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/rspec/rails/example.rb
Expand Up @@ -13,53 +13,61 @@ def c.escaped_path(*parts)
Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
end

controller_path_regex = c.escaped_path(%w[spec controllers])
c.include RSpec::Rails::ControllerExampleGroup,
:type => :controller,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec controllers]) =~ example_group[:file_path]
metadata[:type].nil? && controller_path_regex =~ example_group[:file_path]
}

helper_path_regex = c.escaped_path(%w[spec helpers])
c.include RSpec::Rails::HelperExampleGroup,
:type => :helper,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec helpers]) =~ example_group[:file_path]
metadata[:type].nil? && helper_path_regex =~ example_group[:file_path]
}

mailer_path_regex = c.escaped_path(%w[spec mailers])
if defined?(RSpec::Rails::MailerExampleGroup)
c.include RSpec::Rails::MailerExampleGroup,
:type => :mailer,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec mailers]) =~ example_group[:file_path]
metadata[:type].nil? && mailer_path_regex =~ example_group[:file_path]
}
end

model_path_regex = c.escaped_path(%w[spec models])
c.include RSpec::Rails::ModelExampleGroup,
:type => :model,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec models]) =~ example_group[:file_path]
metadata[:type].nil? && model_path_regex =~ example_group[:file_path]
}

request_path_regex = c.escaped_path(%w[spec (requests|integration|api)])
c.include RSpec::Rails::RequestExampleGroup,
:type => :request,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec (requests|integration|api)]) =~ example_group[:file_path]
metadata[:type].nil? && request_path_regex =~ example_group[:file_path]
}

routing_path_regex = c.escaped_path(%w[spec routing])
c.include RSpec::Rails::RoutingExampleGroup,
:type => :routing,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec routing]) =~ example_group[:file_path]
metadata[:type].nil? && routing_path_regex =~ example_group[:file_path]
}

view_path_regex = c.escaped_path(%w[spec views])
c.include RSpec::Rails::ViewExampleGroup,
:type => :view,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec views]) =~ example_group[:file_path]
metadata[:type].nil? && view_path_regex =~ example_group[:file_path]
}

feature_example_regex = c.escaped_path(%w[spec features])
c.include RSpec::Rails::FeatureExampleGroup,
:type => :feature,
:example_group => lambda { |example_group, metadata = {}|
metadata[:type].nil? && c.escaped_path(%w[spec features]) =~ example_group[:file_path]
metadata[:type].nil? && feature_example_regex =~ example_group[:file_path]
}
end

0 comments on commit d9c9e16

Please sign in to comment.