2014-05-09 02:39:16 -04:00
|
|
|
require 'spec_helper'
|
2011-03-28 04:11:57 -04:00
|
|
|
|
2022-02-05 07:27:48 -05:00
|
|
|
RSpec.describe Sinatra::ContentFor do
|
2011-04-26 05:05:14 -04:00
|
|
|
subject do
|
|
|
|
Sinatra.new do
|
|
|
|
helpers Sinatra::ContentFor
|
2021-10-18 12:40:01 -04:00
|
|
|
set :views, File.expand_path("content_for", __dir__)
|
2011-04-26 05:05:14 -04:00
|
|
|
end.new!
|
|
|
|
end
|
|
|
|
|
2011-04-26 16:19:03 -04:00
|
|
|
Tilt.prefer Tilt::ERBTemplate
|
2022-08-08 11:40:06 -04:00
|
|
|
require 'hamlit'
|
2019-11-28 20:26:16 -05:00
|
|
|
Tilt.register Tilt::HamlTemplate, :haml
|
2011-04-26 16:19:03 -04:00
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
extend Forwardable
|
2016-05-03 04:39:16 -04:00
|
|
|
def_delegators :subject, :content_for, :clear_content_for, :yield_content
|
2011-04-26 05:05:14 -04:00
|
|
|
def render(engine, template)
|
|
|
|
subject.send(:render, engine, template, :layout => false).gsub(/\s/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "without templates" do
|
|
|
|
it 'renders blocks declared with the same key you use when rendering' do
|
|
|
|
content_for(:foo) { "foo" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo)).to eq("foo")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders blocks more than once' do
|
|
|
|
content_for(:foo) { "foo" }
|
2016-05-10 10:08:54 -04:00
|
|
|
3.times { expect(yield_content(:foo)).to eq("foo") }
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not render a block with a different key' do
|
|
|
|
content_for(:bar) { "bar" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo)).to be_empty
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
2014-02-11 18:41:59 -05:00
|
|
|
it 'renders default content if no block matches the key and a default block is specified' do
|
2018-12-09 01:15:52 -05:00
|
|
|
expect(yield_content(:foo) {}).to be_nil
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo) { "foo" }).to eq("foo")
|
2014-02-11 18:41:59 -05:00
|
|
|
end
|
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
it 'renders multiple blocks with the same key' do
|
|
|
|
content_for(:foo) { "foo" }
|
|
|
|
content_for(:foo) { "bar" }
|
|
|
|
content_for(:bar) { "WON'T RENDER ME" }
|
|
|
|
content_for(:foo) { "baz" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo)).to eq("foobarbaz")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders multiple blocks more than once' do
|
|
|
|
content_for(:foo) { "foo" }
|
|
|
|
content_for(:foo) { "bar" }
|
|
|
|
content_for(:bar) { "WON'T RENDER ME" }
|
|
|
|
content_for(:foo) { "baz" }
|
2016-05-10 10:08:54 -04:00
|
|
|
3.times { expect(yield_content(:foo)).to eq("foobarbaz") }
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes values to the blocks' do
|
|
|
|
content_for(:foo) { |a| a.upcase }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo, 'a')).to eq("A")
|
|
|
|
expect(yield_content(:foo, 'b')).to eq("B")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
2015-03-06 17:31:18 -05:00
|
|
|
|
|
|
|
it 'clears named blocks with the specified key' do
|
|
|
|
content_for(:foo) { "foo" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo)).to eq("foo")
|
2015-03-06 17:31:18 -05:00
|
|
|
clear_content_for(:foo)
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo)).to be_empty
|
2015-03-06 17:31:18 -05:00
|
|
|
end
|
2015-06-11 22:16:57 -04:00
|
|
|
|
|
|
|
it 'takes an immediate value instead of a block' do
|
|
|
|
content_for(:foo, "foo")
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo)).to eq("foo")
|
2015-06-11 22:16:57 -04:00
|
|
|
end
|
2016-12-30 01:17:47 -05:00
|
|
|
|
|
|
|
context 'when flush option was disabled' do
|
|
|
|
it 'append content' do
|
|
|
|
content_for(:foo, "foo")
|
|
|
|
content_for(:foo, "bar")
|
|
|
|
expect(yield_content(:foo)).to eq("foobar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when flush option was enabled' do
|
|
|
|
it 'flush first content' do
|
|
|
|
content_for(:foo, "foo")
|
|
|
|
content_for(:foo, "bar", flush: true)
|
|
|
|
expect(yield_content(:foo)).to eq("bar")
|
|
|
|
end
|
|
|
|
end
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
2022-07-17 05:32:46 -04:00
|
|
|
# TODO: liquid markaby builder nokogiri
|
2022-02-26 06:26:50 -05:00
|
|
|
engines = %w[erb erubi haml hamlit slim]
|
2011-04-26 05:05:14 -04:00
|
|
|
|
2011-03-28 04:11:57 -04:00
|
|
|
engines.each do |inner|
|
2011-04-26 05:05:14 -04:00
|
|
|
describe inner.capitalize do
|
|
|
|
before :all do
|
|
|
|
begin
|
|
|
|
require inner
|
|
|
|
rescue LoadError => e
|
2016-05-10 10:08:54 -04:00
|
|
|
skip "Skipping: " << e.message
|
2011-03-28 04:11:57 -04:00
|
|
|
end
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
2011-03-28 04:11:57 -04:00
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
describe "with yield_content in Ruby" do
|
|
|
|
it 'renders blocks declared with the same key you use when rendering' do
|
|
|
|
render inner, :same_key
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo).strip).to eq("foo")
|
2011-03-28 04:11:57 -04:00
|
|
|
end
|
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
it 'renders blocks more than once' do
|
|
|
|
render inner, :same_key
|
2016-05-10 10:08:54 -04:00
|
|
|
3.times { expect(yield_content(:foo).strip).to eq("foo") }
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not render a block with a different key' do
|
|
|
|
render inner, :different_key
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo)).to be_empty
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
2014-02-11 18:41:59 -05:00
|
|
|
it 'renders default content if no block matches the key and a default block is specified' do
|
|
|
|
render inner, :different_key
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo) { "foo" }).to eq("foo")
|
2014-02-11 18:41:59 -05:00
|
|
|
end
|
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
it 'renders multiple blocks with the same key' do
|
|
|
|
render inner, :multiple_blocks
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo).gsub(/\s/, '')).to eq("foobarbaz")
|
2011-03-28 04:11:57 -04:00
|
|
|
end
|
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
it 'renders multiple blocks more than once' do
|
|
|
|
render inner, :multiple_blocks
|
2016-05-10 10:08:54 -04:00
|
|
|
3.times { expect(yield_content(:foo).gsub(/\s/, '')).to eq("foobarbaz") }
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes values to the blocks' do
|
|
|
|
render inner, :takes_values
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(yield_content(:foo, 1, 2).gsub(/\s/, '')).to eq("<i>1</i>2")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with content_for in Ruby" do
|
2011-03-28 04:11:57 -04:00
|
|
|
it 'renders blocks declared with the same key you use when rendering' do
|
2011-04-26 05:05:14 -04:00
|
|
|
content_for(:foo) { "foo" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :layout)).to eq("foo")
|
2011-03-28 04:11:57 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 11:12:43 -04:00
|
|
|
it 'renders blocks more than once' do
|
2011-04-26 05:05:14 -04:00
|
|
|
content_for(:foo) { "foo" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :multiple_yields)).to eq("foofoofoo")
|
2011-03-28 11:12:43 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 04:11:57 -04:00
|
|
|
it 'does not render a block with a different key' do
|
2011-04-26 05:05:14 -04:00
|
|
|
content_for(:bar) { "foo" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :layout)).to be_empty
|
2011-03-28 04:11:57 -04:00
|
|
|
end
|
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
it 'renders multiple blocks with the same key' do
|
|
|
|
content_for(:foo) { "foo" }
|
|
|
|
content_for(:foo) { "bar" }
|
|
|
|
content_for(:bar) { "WON'T RENDER ME" }
|
|
|
|
content_for(:foo) { "baz" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :layout)).to eq("foobarbaz")
|
2011-03-28 04:11:57 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 11:12:43 -04:00
|
|
|
it 'renders multiple blocks more than once' do
|
2011-04-26 05:05:14 -04:00
|
|
|
content_for(:foo) { "foo" }
|
|
|
|
content_for(:foo) { "bar" }
|
|
|
|
content_for(:bar) { "WON'T RENDER ME" }
|
|
|
|
content_for(:foo) { "baz" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :multiple_yields)).to eq("foobarbazfoobarbazfoobarbaz")
|
2011-03-28 11:12:43 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 04:11:57 -04:00
|
|
|
it 'passes values to the blocks' do
|
2011-04-26 05:05:14 -04:00
|
|
|
content_for(:foo) { |a,b| "<i>#{a}</i>#{b}" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :passes_values)).to eq("<i>1</i>2")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
2015-03-06 17:31:18 -05:00
|
|
|
|
|
|
|
it 'clears named blocks with the specified key' do
|
|
|
|
content_for(:foo) { "foo" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :layout)).to eq("foo")
|
2015-03-06 17:31:18 -05:00
|
|
|
clear_content_for(:foo)
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :layout)).to be_empty
|
2015-03-06 17:31:18 -05:00
|
|
|
end
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
2012-12-09 14:10:08 -05:00
|
|
|
|
2011-12-15 16:27:19 -05:00
|
|
|
describe "with content_for? in Ruby" do
|
2012-12-09 14:10:08 -05:00
|
|
|
it 'renders block if key is set' do
|
2011-12-15 16:27:19 -05:00
|
|
|
content_for(:foo) { "foot" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :footer)).to eq("foot")
|
2011-12-15 16:27:19 -05:00
|
|
|
end
|
2012-12-09 14:10:08 -05:00
|
|
|
|
2011-12-15 16:27:19 -05:00
|
|
|
it 'does not render a block if different key' do
|
|
|
|
content_for(:different_key) { "foot" }
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(render(inner, :footer)).to be_empty
|
2011-12-15 16:27:19 -05:00
|
|
|
end
|
|
|
|
end
|
2011-04-26 05:05:14 -04:00
|
|
|
|
|
|
|
engines.each do |outer|
|
|
|
|
describe "with yield_content in #{outer.capitalize}" do
|
|
|
|
def body
|
|
|
|
last_response.body.gsub(/\s/, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
before :all do
|
|
|
|
begin
|
|
|
|
require outer
|
|
|
|
rescue LoadError => e
|
2016-05-10 10:08:54 -04:00
|
|
|
skip "Skipping: " << e.message
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
mock_app do
|
|
|
|
helpers Sinatra::ContentFor
|
|
|
|
set inner, :layout_engine => outer
|
2021-10-18 12:40:01 -04:00
|
|
|
set :views, File.expand_path("content_for", __dir__)
|
2011-08-17 06:32:19 -04:00
|
|
|
get('/:view') { render(inner, params[:view].to_sym) }
|
2011-04-26 05:05:14 -04:00
|
|
|
get('/:layout/:view') do
|
2011-08-17 06:32:19 -04:00
|
|
|
render inner, params[:view].to_sym, :layout => params[:layout].to_sym
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-08 15:11:42 -05:00
|
|
|
describe 'with a default content block' do
|
|
|
|
describe 'when content_for key exists' do
|
|
|
|
it 'ignores default content and renders content' do
|
|
|
|
expect(get('/yield_block/same_key')).to be_ok
|
|
|
|
expect(body).to eq("foo")
|
|
|
|
end
|
|
|
|
end
|
2018-11-30 12:54:52 -05:00
|
|
|
|
2018-12-08 15:11:42 -05:00
|
|
|
describe 'when content_for key is missing' do
|
|
|
|
it 'renders default content block' do
|
|
|
|
expect(get('/yield_block/different_key')).to be_ok
|
|
|
|
expect(body).to eq("baz")
|
|
|
|
end
|
|
|
|
end
|
2018-12-08 14:01:48 -05:00
|
|
|
end
|
|
|
|
|
2018-12-01 23:50:51 -05:00
|
|
|
it 'renders content set as parameter' do
|
|
|
|
expect(get('/parameter_value')).to be_ok
|
|
|
|
expect(body).to eq("foo")
|
|
|
|
end
|
|
|
|
|
2011-04-26 05:05:14 -04:00
|
|
|
it 'renders blocks declared with the same key you use when rendering' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(get('/same_key')).to be_ok
|
|
|
|
expect(body).to eq("foo")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders blocks more than once' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(get('/multiple_yields/same_key')).to be_ok
|
|
|
|
expect(body).to eq("foofoofoo")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not render a block with a different key' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(get('/different_key')).to be_ok
|
|
|
|
expect(body).to be_empty
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders multiple blocks with the same key' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(get('/multiple_blocks')).to be_ok
|
|
|
|
expect(body).to eq("foobarbaz")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders multiple blocks more than once' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(get('/multiple_yields/multiple_blocks')).to be_ok
|
|
|
|
expect(body).to eq("foobarbazfoobarbazfoobarbaz")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes values to the blocks' do
|
2016-05-10 10:08:54 -04:00
|
|
|
expect(get('/passes_values/takes_values')).to be_ok
|
|
|
|
expect(body).to eq("<i>1</i>2")
|
2011-04-26 05:05:14 -04:00
|
|
|
end
|
2011-03-28 04:11:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|