Skip to content

Commit 124792f

Browse files
jhawthornbenoittgt
authored andcommitted
Improve path parsing in _default_render_options (#2115)
1 parent 6d1212f commit 124792f

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

lib/rspec/rails/example/view_example_group.rb

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,35 @@ def rendered.body
129129

130130
def _default_render_options
131131
if ::Rails::VERSION::STRING >= '3.2'
132-
# pluck the handler, format, and locale out of, eg, posts/index.de.html.haml
133-
template, *components = _default_file_to_render.split('.')
134-
handler, format, locale = *components.reverse
135-
136-
render_options = { :template => template }
137-
render_options[:handlers] = [handler] if handler
138-
render_options[:formats] = [format.to_sym] if format
139-
render_options[:locales] = [locale] if locale
132+
formats = if ActionView::Template::Types.respond_to?(:symbols)
133+
ActionView::Template::Types.symbols
134+
else
135+
[:html, :text, :js, :css, :xml, :json].map(&:to_s)
136+
end.map { |x| Regexp.escape(x) }.join("|")
137+
138+
handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
139+
locales = "[a-z]{2}(?:-[A-Z]{2})?"
140+
variants = "[^.]*"
141+
path_regex = %r{
142+
\A
143+
(?<template>.*?)
144+
(?:\.(?<locale>#{locales}))??
145+
(?:\.(?<format>#{formats}))??
146+
(?:\+(?<variant>#{variants}))??
147+
(?:\.(?<handler>#{handlers}))?
148+
\z
149+
}x
150+
151+
# This regex should always find a match.
152+
# Worst case, everything will be nil, and :template will just be
153+
# the original string.
154+
match = path_regex.match(_default_file_to_render)
155+
156+
render_options = { :template => match[:template] }
157+
render_options[:handlers] = [match[:handler]] if match[:handler]
158+
render_options[:formats] = [match[:format].to_sym] if match[:format]
159+
render_options[:locales] = [match[:locale]] if match[:locale]
160+
render_options[:variants] = [match[:variant]] if match[:variant]
140161

141162
render_options
142163
else

spec/rspec/rails/example/view_example_group_spec.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,32 @@ def _assigns
137137
expect(view_spec.received.first).to eq([{:template => "widgets/new"},{}, nil])
138138
end
139139

140-
it "converts the filename components into render options" do
141-
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
142-
view_spec.render
143-
144-
if ::Rails::VERSION::STRING >= '3.2'
140+
if ::Rails::VERSION::STRING >= '3.2'
141+
it "converts the filename components into render options" do
142+
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
143+
view_spec.render
145144
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :formats=>[:html], :handlers=>['erb']}, {}, nil])
146-
else
145+
end
146+
147+
it "converts the filename with variant into render options" do
148+
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html+fancy.erb" }
149+
view_spec.render
150+
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :formats=>[:html], :handlers=>['erb'], variants: ['fancy']}, {}, nil])
151+
end
152+
153+
it "converts the filename without format into render options" do
154+
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.erb" }
155+
view_spec.render
156+
expect(view_spec.received.first).to eq([{:template => "widgets/new", :locales=>['en'], :handlers=>['erb']}, {}, nil])
157+
end
158+
else
159+
it "uses the filename as a template" do
160+
allow(view_spec).to receive(:_default_file_to_render) { "widgets/new.en.html.erb" }
161+
view_spec.render
147162
expect(view_spec.received.first).to eq([{:template => "widgets/new.en.html.erb"}, {}, nil])
148163
end
149164
end
150-
end
165+
end
151166

152167
context "given a string" do
153168
it "sends string as the first arg to render" do

0 commit comments

Comments
 (0)