diff --git a/sinatra-contrib/spec/content_for/different_key.erb b/sinatra-contrib/spec/content_for/different_key.erb new file mode 100644 index 00000000..af814412 --- /dev/null +++ b/sinatra-contrib/spec/content_for/different_key.erb @@ -0,0 +1 @@ +<% content_for :bar do %>bar<% end %> diff --git a/sinatra-contrib/spec/content_for/different_key.haml b/sinatra-contrib/spec/content_for/different_key.haml new file mode 100644 index 00000000..d909fc2f --- /dev/null +++ b/sinatra-contrib/spec/content_for/different_key.haml @@ -0,0 +1,2 @@ +- content_for :bar do + bar diff --git a/sinatra-contrib/spec/content_for/layout.erb b/sinatra-contrib/spec/content_for/layout.erb new file mode 100644 index 00000000..c7bc9102 --- /dev/null +++ b/sinatra-contrib/spec/content_for/layout.erb @@ -0,0 +1 @@ +<% yield_content :foo %> \ No newline at end of file diff --git a/sinatra-contrib/spec/content_for/layout.haml b/sinatra-contrib/spec/content_for/layout.haml new file mode 100644 index 00000000..aabdfaae --- /dev/null +++ b/sinatra-contrib/spec/content_for/layout.haml @@ -0,0 +1 @@ += yield_content :foo diff --git a/sinatra-contrib/spec/content_for/multiple_blocks.erb b/sinatra-contrib/spec/content_for/multiple_blocks.erb new file mode 100644 index 00000000..032031f9 --- /dev/null +++ b/sinatra-contrib/spec/content_for/multiple_blocks.erb @@ -0,0 +1,4 @@ +<% content_for :foo do %>foo<% end %> +<% content_for :foo do %>bar<% end %> +<% content_for :baz do %>WON'T RENDER ME<% end %> +<% content_for :foo do %>baz<% end %> diff --git a/sinatra-contrib/spec/content_for/multiple_blocks.haml b/sinatra-contrib/spec/content_for/multiple_blocks.haml new file mode 100644 index 00000000..1cea892e --- /dev/null +++ b/sinatra-contrib/spec/content_for/multiple_blocks.haml @@ -0,0 +1,8 @@ +- content_for :foo do + foo +- content_for :foo do + bar +- content_for :baz do + WON'T RENDER ME +- content_for :foo do + baz diff --git a/sinatra-contrib/spec/content_for/passes_values.erb b/sinatra-contrib/spec/content_for/passes_values.erb new file mode 100644 index 00000000..f7b00777 --- /dev/null +++ b/sinatra-contrib/spec/content_for/passes_values.erb @@ -0,0 +1 @@ +<% yield_content :foo, 1, 2 %> \ No newline at end of file diff --git a/sinatra-contrib/spec/content_for/passes_values.haml b/sinatra-contrib/spec/content_for/passes_values.haml new file mode 100644 index 00000000..528e3cfb --- /dev/null +++ b/sinatra-contrib/spec/content_for/passes_values.haml @@ -0,0 +1 @@ += yield_content :foo, 1, 2 diff --git a/sinatra-contrib/spec/content_for/same_key.erb b/sinatra-contrib/spec/content_for/same_key.erb new file mode 100644 index 00000000..c6e07b75 --- /dev/null +++ b/sinatra-contrib/spec/content_for/same_key.erb @@ -0,0 +1 @@ +<% content_for :foo do %>foo<% end %> diff --git a/sinatra-contrib/spec/content_for/same_key.haml b/sinatra-contrib/spec/content_for/same_key.haml new file mode 100644 index 00000000..6af88d84 --- /dev/null +++ b/sinatra-contrib/spec/content_for/same_key.haml @@ -0,0 +1,2 @@ +- content_for :foo do + foo diff --git a/sinatra-contrib/spec/content_for/takes_values.erb b/sinatra-contrib/spec/content_for/takes_values.erb new file mode 100644 index 00000000..98b2b438 --- /dev/null +++ b/sinatra-contrib/spec/content_for/takes_values.erb @@ -0,0 +1 @@ +<% content_for :foo do |a, b| %><%= a %> <%= b %><% end %> \ No newline at end of file diff --git a/sinatra-contrib/spec/content_for/takes_values.haml b/sinatra-contrib/spec/content_for/takes_values.haml new file mode 100644 index 00000000..44059180 --- /dev/null +++ b/sinatra-contrib/spec/content_for/takes_values.haml @@ -0,0 +1,3 @@ +- content_for :foo do |a, b| + %i= a + =b diff --git a/sinatra-contrib/spec/content_for_spec.rb b/sinatra-contrib/spec/content_for_spec.rb new file mode 100644 index 00000000..88272dc8 --- /dev/null +++ b/sinatra-contrib/spec/content_for_spec.rb @@ -0,0 +1,61 @@ +require 'backports' +require_relative 'spec_helper' + +describe Sinatra::ContentFor do + # TODO: erubis slim liquid radius markaby builder nokogiri + engines = %w[erb haml] + engines.each do |inner| + engines.each do |outer| + describe "#{inner.capitalize} templates with #{outer.capitalize} layouts" do + def body + super.gsub(/\s/, '') + end + + before do + pending "different layout engines not supported yet" unless inner == outer + end + + before :all do + begin + require inner + require outer + rescue LoadError => e + pending "Skipping: " << e.message + end + end + + before do + mock_app do + helpers Sinatra::ContentFor + set inner, :layout_engine => outer + set :views, File.expand_path("../content_for", __FILE__) + get('/:view') { send(inner, params[:view].to_sym) } + get('/:layout/:view') do + send inner, params[:view].to_sym, :layout => params[:layout].to_sym + end + end + end + + it 'renders blocks declared with the same key you use when rendering' do + get('/same_key').should be_ok + body.should == "foo" + end + + it 'does not render a block with a different key' do + get('/different_key').should be_ok + body.should == "" + end + + it 'renders multiple blocks with the same key'do + get('/multiple_blocks').should be_ok + body.should == "foobarbaz" + end + + it 'passes values to the blocks' do + get('/passes_values/takes_values').should be_ok + body.should == "12" + end + end + end + end +end diff --git a/sinatra-contrib/test/content_for_test.rb b/sinatra-contrib/test/content_for_test.rb deleted file mode 100644 index c9560f22..00000000 --- a/sinatra-contrib/test/content_for_test.rb +++ /dev/null @@ -1,156 +0,0 @@ -ENV['RACK_ENV'] = 'test' - -begin - require 'rack' -rescue LoadError - require 'rubygems' - require 'rack' -end - -require 'contest' -require 'sinatra/test' -require 'haml' - -begin - require 'redgreen' -rescue LoadError -end - -require File.dirname(__FILE__) + '/../lib/sinatra/content_for' - -Sinatra::Base.set :environment, :test - -module Sinatra - class Base - set :environment, :test - helpers ContentFor - end -end - -class Test::Unit::TestCase - include Sinatra::Test - - class << self - alias_method :it, :test - end - - def mock_app(base=Sinatra::Base, &block) - @app = Sinatra.new(base, &block) - end -end - -class ContentForTest < Test::Unit::TestCase - context 'using erb' do - def erb_app(view) - mock_app { - layout { '<% yield_content :foo %>' } - get('/') { erb view } - } - end - - it 'renders blocks declared with the same key you use when rendering' do - erb_app '<% content_for :foo do %>foo<% end %>' - - get '/' - assert ok? - assert_equal 'foo', body - end - - it 'does not render a block with a different key' do - erb_app '<% content_for :bar do %>bar<% end %>' - - get '/' - assert ok? - assert_equal '', body - end - - it 'renders multiple blocks with the same key' do - erb_app <<-erb_snippet - <% content_for :foo do %>foo<% end %> - <% content_for :foo do %>bar<% end %> - <% content_for :baz do %>WON'T RENDER ME<% end %> - <% content_for :foo do %>baz<% end %> - erb_snippet - - get '/' - assert ok? - assert_equal 'foobarbaz', body - end - - it 'passes values to the blocks' do - mock_app { - layout { '<% yield_content :foo, 1, 2 %>' } - get('/') { erb '<% content_for :foo do |a, b| %><%= a %> <%= b %><% end %>' } - } - - get '/' - assert ok? - assert_equal '1 2', body - end - end - - context 'with haml' do - def haml_app(view) - mock_app { - layout { '= yield_content :foo' } - get('/') { haml view } - } - end - - it 'renders blocks declared with the same key you use when rendering' do - haml_app <<-haml_end -- content_for :foo do - foo -haml_end - - get '/' - assert ok? - assert_equal "foo\n", body - end - - it 'does not render a block with a different key' do - haml_app <<-haml_end -- content_for :bar do - bar -haml_end - - get '/' - assert ok? - assert_equal "\n", body - end - - it 'renders multiple blocks with the same key' do - haml_app <<-haml_end -- content_for :foo do - foo -- content_for :foo do - bar -- content_for :baz do - WON'T RENDER ME -- content_for :foo do - baz -haml_end - - get '/' - assert ok? - assert_equal "foo\nbar\nbaz\n", body - end - - it 'passes values to the blocks' do - mock_app { - layout { '= yield_content :foo, 1, 2' } - get('/') { - haml <<-haml_end -- content_for :foo do |a, b| - %i= a - =b -haml_end - } - } - - get '/' - assert ok? - assert_equal "1\n2\n", body - end - end -end