test/templates_test.rb in sinatra-1.3.6 vs test/templates_test.rb in sinatra-1.4.0.a
- old
+ new
@@ -15,16 +15,16 @@
end
class TemplatesTest < Test::Unit::TestCase
def render_app(base=Sinatra::Base, options = {}, &block)
base, options = Sinatra::Base, base if base.is_a? Hash
- mock_app(base) {
+ mock_app(base) do
set :views, File.dirname(__FILE__) + '/views'
set options
- get '/', &block
+ get('/', &block)
template(:layout3) { "Layout 3!\n" }
- }
+ end
get '/'
end
def with_default_layout
layout = File.dirname(__FILE__) + '/views/layout.test'
@@ -32,75 +32,129 @@
yield
ensure
File.unlink(layout) rescue nil
end
+ it 'falls back to engine layout' do
+ mock_app do
+ template(:layout3) { 'Layout 3!<%= yield %>' }
+ set :erb, :layout => :layout3
+
+ get('/') do
+ erb('Hello World!', { :layout => true })
+ end
+ end
+
+ get '/'
+ assert ok?
+ assert_equal "Layout 3!Hello World!", body
+ end
+
+ it 'falls back to default layout if engine layout is true' do
+ mock_app do
+ template(:layout) { 'Layout!!! <%= yield %>' }
+ set :erb, :layout => true
+
+ get('/') do
+ erb('Hello World!', { :layout => true })
+ end
+ end
+
+ get '/'
+ assert ok?
+ assert_equal "Layout!!! Hello World!", body
+ end
+
+ it 'renders no layout if layout if falsy' do
+ mock_app do
+ template(:layout) { 'Layout!!! <%= yield %>' }
+ set :erb, :layout => true
+
+ get('/') do
+ erb('Hello World!', { :layout => nil })
+ end
+ end
+
+ get '/'
+ assert ok?
+ assert_equal "Hello World!", body
+ end
+
it 'renders String templates directly' do
- render_app { render :test, 'Hello World' }
+ render_app { render(:test, 'Hello World') }
assert ok?
assert_equal 'Hello World', body
end
it 'renders Proc templates using the call result' do
- render_app { render :test, Proc.new {'Hello World'} }
+ render_app { render(:test, Proc.new {'Hello World'}) }
assert ok?
assert_equal 'Hello World', body
end
it 'looks up Symbol templates in views directory' do
- render_app { render :test, :hello }
+ render_app { render(:test, :hello) }
assert ok?
assert_equal "Hello World!\n", body
end
it 'uses the default layout template if not explicitly overridden' do
with_default_layout do
- render_app { render :test, :hello }
+ render_app { render(:test, :hello) }
assert ok?
assert_equal "Layout!\nHello World!\n", body
end
end
it 'uses the default layout template if not really overriden' do
with_default_layout do
- render_app { render :test, :hello, :layout => true }
+ render_app { render(:test, :hello, :layout => true) }
assert ok?
assert_equal "Layout!\nHello World!\n", body
end
end
it 'uses the layout template specified' do
- render_app { render :test, :hello, :layout => :layout2 }
+ render_app { render(:test, :hello, :layout => :layout2) }
assert ok?
assert_equal "Layout 2!\nHello World!\n", body
end
it 'uses layout templates defined with the #template method' do
- render_app { render :test, :hello, :layout => :layout3 }
+ render_app { render(:test, :hello, :layout => :layout3) }
assert ok?
assert_equal "Layout 3!\nHello World!\n", body
end
it 'avoids wrapping layouts around nested templates' do
- render_app { render :str, :nested, :layout => :layout2 }
+ render_app { render(:str, :nested, :layout => :layout2) }
assert ok?
- assert_equal "<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>", body
+ assert_equal(
+ "<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>",
+ body
+ )
end
it 'allows explicitly wrapping layouts around nested templates' do
- render_app { render :str, :explicitly_nested, :layout => :layout2 }
+ render_app { render(:str, :explicitly_nested, :layout => :layout2) }
assert ok?
- assert_equal "<h1>String Layout!</h1>\n<content><h1>String Layout!</h1>\n<h1>Hello From String</h1></content>", body
+ assert_equal(
+ "<h1>String Layout!</h1>\n<content><h1>String Layout!</h1>\n<h1>Hello From String</h1></content>",
+ body
+ )
end
it 'two independent render calls do not disable layouts' do
render_app do
render :str, :explicitly_nested, :layout => :layout2
render :str, :nested, :layout => :layout2
end
assert ok?
- assert_equal "<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>", body
+ assert_equal(
+ "<h1>String Layout!</h1>\n<content><h1>Hello From String</h1></content>",
+ body
+ )
end
it 'is possible to use partials in layouts' do
render_app do
settings.layout { "<%= erb 'foo' %><%= yield %>" }
@@ -109,79 +163,101 @@
assert ok?
assert_equal "foobar", body
end
it 'loads templates from source file' do
- mock_app { enable :inline_templates }
+ mock_app { enable(:inline_templates) }
assert_equal "this is foo\n\n", @app.templates[:foo][0]
assert_equal "X\n= yield\nX\n", @app.templates[:layout][0]
end
it 'ignores spaces after names of inline templates' do
- mock_app { enable :inline_templates }
+ mock_app { enable(:inline_templates) }
assert_equal "There's a space after 'bar'!\n\n", @app.templates[:bar][0]
assert_equal "this is not foo\n\n", @app.templates[:"foo bar"][0]
end
it 'loads templates from given source file' do
- mock_app { set :inline_templates, __FILE__ }
+ mock_app { set(:inline_templates, __FILE__) }
assert_equal "this is foo\n\n", @app.templates[:foo][0]
end
test 'inline_templates ignores IO errors' do
- assert_nothing_raised {
- mock_app {
- set :inline_templates, '/foo/bar'
- }
- }
+ assert_nothing_raised { mock_app { set(:inline_templates, '/foo/bar') } }
assert @app.templates.empty?
end
it 'allows unicode in inline templates' do
- mock_app { set :inline_templates, __FILE__ }
- assert_equal "Den som tror at hemma det är där man bor har aldrig vart hos mig.\n\n",
+ mock_app { set(:inline_templates, __FILE__) }
+ assert_equal(
+ "Den som tror at hemma det är där man bor har aldrig vart hos mig.\n\n",
@app.templates[:umlaut][0]
+ )
end
it 'loads templates from specified views directory' do
- render_app { render :test, :hello, :views => settings.views + '/foo' }
+ render_app { render(:test, :hello, :views => settings.views + '/foo') }
assert_equal "from another views directory\n", body
end
+ it 'takes views directory into consideration for caching' do
+ render_app do
+ render(:test, :hello) + render(:test, :hello, :views => settings.views + '/foo')
+ end
+ assert_equal "Hello World!\nfrom another views directory\n", body
+ end
+
it 'passes locals to the layout' do
- mock_app {
- template :my_layout do
- 'Hello <%= name %>!<%= yield %>'
+ mock_app do
+ template(:my_layout) { 'Hello <%= name %>!<%= yield %>' }
+
+ get('/') do
+ erb('<p>content</p>', { :layout => :my_layout }, { :name => 'Mike'})
end
+ end
+ get '/'
+ assert ok?
+ assert_equal 'Hello Mike!<p>content</p>', body
+ end
+
+ it 'sets layout-only options via layout_options' do
+ mock_app do
get '/' do
- erb '<p>content</p>', { :layout => :my_layout }, { :name => 'Mike'}
+ render(:str, :in_a,
+ :views => settings.views + '/a',
+ :layout_options => { :views => settings.views },
+ :layout => :layout2)
end
- }
+ end
get '/'
assert ok?
- assert_equal 'Hello Mike!<p>content</p>', body
+ assert_equal "<h1>String Layout!</h1>\nGimme an A!\n", body
end
it 'loads templates defined in subclasses' do
base = Class.new(Sinatra::Base)
base.template(:foo) { 'bar' }
- render_app(base) { render :test, :foo }
+ render_app(base) { render(:test, :foo) }
assert ok?
assert_equal 'bar', body
end
it 'allows setting default content type per template engine' do
- render_app(:str => { :content_type => :txt }) { render :str, 'foo' }
+ render_app(:str => { :content_type => :txt }) {
+ render :str, 'foo'
+ }
assert_equal 'text/plain;charset=utf-8', response['Content-Type']
end
it 'setting default content type does not affect other template engines' do
- render_app(:str => { :content_type => :txt }) { render :test, 'foo' }
+ render_app(:str => { :content_type => :txt }) {
+ render :test, 'foo'
+ }
assert_equal 'text/html;charset=utf-8', response['Content-Type']
end
it 'setting default content type per template engine does not override content_type' do
render_app :str => { :content_type => :txt } do
@@ -194,15 +270,15 @@
it 'uses templates in superclasses before subclasses' do
base = Class.new(Sinatra::Base)
base.template(:foo) { 'template in superclass' }
assert_equal 'template in superclass', base.templates[:foo].first.call
- mock_app(base) {
+ mock_app(base) do
set :views, File.dirname(__FILE__) + '/views'
template(:foo) { 'template in subclass' }
get('/') { render :test, :foo }
- }
+ end
assert_equal 'template in subclass', @app.templates[:foo].first.call
get '/'
assert ok?
assert_equal 'template in subclass', body
@@ -238,15 +314,13 @@
assert_equal '2', body
end
it "passes scope to the template" do
mock_app do
- template :scoped do
- 'Hello <%= foo %>'
- end
+ template(:scoped) { 'Hello <%= foo %>' }
- get '/' do
+ get('/') do
some_scope = Object.new
def some_scope.foo() 'World!' end
erb :scoped, :scope => some_scope
end
end
@@ -261,13 +335,11 @@
set :views, ["a", "b"].map { |d| File.dirname(__FILE__) + '/views/' + d }
def find_template(views, name, engine, &block)
Array(views).each { |v| super(v, name, engine, &block) }
end
- get('/:name') do
- render :str, params[:name].to_sym
- end
+ get('/:name') { render(:str, params[:name].to_sym) }
end
get '/in_a'
assert_body 'Gimme an A!'
@@ -281,10 +353,10 @@
__END__
@@ foo
this is foo
-@@ bar
+@@ bar
There's a space after 'bar'!
@@ foo bar
this is not foo