Skip to content

Commit

Permalink
Improve path parsing in _default_render_options
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed Apr 30, 2019
1 parent 91991e2 commit 8ba6330
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
32 changes: 24 additions & 8 deletions lib/rspec/rails/example/view_example_group.rb
Expand Up @@ -129,14 +129,30 @@ def rendered.body

def _default_render_options
if ::Rails::VERSION::STRING >= '3.2'
# pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
template, *components = _default_file_to_render.split('.')
handler, format, locale = *components.reverse

render_options = { :template => template }
render_options[:handlers] = [handler] if handler
render_options[:formats] = [format.to_sym] if format
render_options[:locales] = [locale] if locale
handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
formats = ActionView::Template::Types.symbols.map { |x| Regexp.escape(x) }.join("|")
locales = "[a-z]{2}(?:-[A-Z]{2})?"
variants = "[^.]*"
path_regex = %r{
\A
(?<template>.*?)
(?:\.(?<locale>#{locales}))??
(?:\.(?<format>#{formats}))??
(?:\+(?<variant>#{variants}))??
(?:\.(?<handler>#{handlers}))?
\z
}x

# This regex should always find a match.
# Worst case, everything will be nil, and :template will just be
# the original string.
match = path_regex.match(_default_file_to_render)

render_options = { :template => match[:template] }
render_options[:handlers] = [match[:handler]] if match[:handler]
render_options[:formats] = [match[:format].to_sym] if match[:format]
render_options[:locales] = [match[:locale]] if match[:locale]
render_options[:variants] = [match[:variant]] if match[:variant]

render_options
else
Expand Down
29 changes: 22 additions & 7 deletions spec/rspec/rails/example/view_example_group_spec.rb
Expand Up @@ -137,17 +137,32 @@ def _assigns
expect(view_spec.received.first).to eq([{:template => "widgets/new"},{}, nil])
end

it "converts the filename components into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
view_spec.render

if ::Rails::VERSION::STRING >= '3.2'
if ::Rails::VERSION::STRING >= '3.2'
it "converts the filename components into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :formats=>[:html], :handlers=>['erb']}, {}, nil])
else
end

it "converts the filename with variant into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html+fancy.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :formats=>[:html], :handlers=>['erb'], variants: ['fancy']}, {}, nil])
end

it "converts the filename without format into render options" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :handlers=>['erb']}, {}, nil])
end
else
it "uses the filename as a template" do
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
view_spec.render
expect(view_spec.received.first).to eq([{:template => "widgets/new.en.html.erb"}, {}, nil])
end
end
end
end

context "given a string" do
it "sends string as the first arg to render" do
Expand Down

0 comments on commit 8ba6330

Please sign in to comment.