mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
move content-for tests to rspec
This commit is contained in:
parent
19c68f8ad4
commit
7fa95253e3
14 changed files with 87 additions and 156 deletions
1
sinatra-contrib/spec/content_for/different_key.erb
Normal file
1
sinatra-contrib/spec/content_for/different_key.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% content_for :bar do %>bar<% end %>
|
2
sinatra-contrib/spec/content_for/different_key.haml
Normal file
2
sinatra-contrib/spec/content_for/different_key.haml
Normal file
|
@ -0,0 +1,2 @@
|
|||
- content_for :bar do
|
||||
bar
|
1
sinatra-contrib/spec/content_for/layout.erb
Normal file
1
sinatra-contrib/spec/content_for/layout.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% yield_content :foo %>
|
1
sinatra-contrib/spec/content_for/layout.haml
Normal file
1
sinatra-contrib/spec/content_for/layout.haml
Normal file
|
@ -0,0 +1 @@
|
|||
= yield_content :foo
|
4
sinatra-contrib/spec/content_for/multiple_blocks.erb
Normal file
4
sinatra-contrib/spec/content_for/multiple_blocks.erb
Normal file
|
@ -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 %>
|
8
sinatra-contrib/spec/content_for/multiple_blocks.haml
Normal file
8
sinatra-contrib/spec/content_for/multiple_blocks.haml
Normal file
|
@ -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
|
1
sinatra-contrib/spec/content_for/passes_values.erb
Normal file
1
sinatra-contrib/spec/content_for/passes_values.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% yield_content :foo, 1, 2 %>
|
1
sinatra-contrib/spec/content_for/passes_values.haml
Normal file
1
sinatra-contrib/spec/content_for/passes_values.haml
Normal file
|
@ -0,0 +1 @@
|
|||
= yield_content :foo, 1, 2
|
1
sinatra-contrib/spec/content_for/same_key.erb
Normal file
1
sinatra-contrib/spec/content_for/same_key.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% content_for :foo do %>foo<% end %>
|
2
sinatra-contrib/spec/content_for/same_key.haml
Normal file
2
sinatra-contrib/spec/content_for/same_key.haml
Normal file
|
@ -0,0 +1,2 @@
|
|||
- content_for :foo do
|
||||
foo
|
1
sinatra-contrib/spec/content_for/takes_values.erb
Normal file
1
sinatra-contrib/spec/content_for/takes_values.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<% content_for :foo do |a, b| %><i><%= a %></i> <%= b %><% end %>
|
3
sinatra-contrib/spec/content_for/takes_values.haml
Normal file
3
sinatra-contrib/spec/content_for/takes_values.haml
Normal file
|
@ -0,0 +1,3 @@
|
|||
- content_for :foo do |a, b|
|
||||
%i= a
|
||||
=b
|
61
sinatra-contrib/spec/content_for_spec.rb
Normal file
61
sinatra-contrib/spec/content_for_spec.rb
Normal file
|
@ -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 == "<i>1</i>2"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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| %><i><%= a %></i> <%= b %><% end %>' }
|
||||
}
|
||||
|
||||
get '/'
|
||||
assert ok?
|
||||
assert_equal '<i>1</i> 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 "<i>1</i>\n2\n", body
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue