2009-01-13 09:53:53 -08:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2010-09-12 15:22:24 +02:00
|
|
|
File.delete(File.dirname(__FILE__) + '/views/layout.test') rescue nil
|
2008-12-13 13:06:02 -08:00
|
|
|
|
2009-06-07 09:50:22 -07:00
|
|
|
class TestTemplate < Tilt::Template
|
2010-03-04 03:12:36 -08:00
|
|
|
def prepare
|
2009-06-07 09:50:22 -07:00
|
|
|
end
|
2010-03-04 03:12:36 -08:00
|
|
|
alias compile! prepare # for tilt < 0.7
|
2009-06-07 09:50:22 -07:00
|
|
|
|
|
|
|
def evaluate(scope, locals={}, &block)
|
|
|
|
inner = block ? block.call : ''
|
|
|
|
data + inner
|
|
|
|
end
|
|
|
|
|
|
|
|
Tilt.register 'test', self
|
|
|
|
end
|
|
|
|
|
2009-03-26 16:42:13 +01:00
|
|
|
class TemplatesTest < Test::Unit::TestCase
|
2009-03-26 08:01:20 -07:00
|
|
|
def render_app(base=Sinatra::Base, &block)
|
|
|
|
mock_app(base) {
|
2008-12-13 13:06:02 -08:00
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
|
|
|
get '/', &block
|
|
|
|
template(:layout3) { "Layout 3!\n" }
|
|
|
|
}
|
|
|
|
get '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_default_layout
|
|
|
|
layout = File.dirname(__FILE__) + '/views/layout.test'
|
|
|
|
File.open(layout, 'wb') { |io| io.write "Layout!\n" }
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
File.unlink(layout) rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders String templates directly' do
|
|
|
|
render_app { render :test, 'Hello World' }
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders Proc templates using the call result' do
|
|
|
|
render_app { render :test, Proc.new {'Hello World'} }
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'looks up Symbol templates in views directory' do
|
|
|
|
render_app { render :test, :hello }
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Hello World!\n", body
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the default layout template if not explicitly overridden' do
|
|
|
|
with_default_layout do
|
|
|
|
render_app { render :test, :hello }
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout!\nHello World!\n", body
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-08 15:01:36 -02:00
|
|
|
it 'uses the default layout template if not really overriden' do
|
|
|
|
with_default_layout do
|
|
|
|
render_app { render :test, :hello, :layout => true }
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout!\nHello World!\n", body
|
2009-01-08 15:01:36 -02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-13 13:06:02 -08:00
|
|
|
it 'uses the layout template specified' do
|
|
|
|
render_app { render :test, :hello, :layout => :layout2 }
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout 2!\nHello World!\n", body
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses layout templates defined with the #template method' do
|
|
|
|
render_app { render :test, :hello, :layout => :layout3 }
|
2009-01-14 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout 3!\nHello World!\n", body
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
2010-01-29 18:18:19 +01:00
|
|
|
it 'loads templates from source file' do
|
|
|
|
mock_app { enable :inline_templates }
|
2009-06-07 09:50:22 -07:00
|
|
|
assert_equal "this is foo\n\n", @app.templates[:foo][0]
|
|
|
|
assert_equal "X\n= yield\nX\n", @app.templates[:layout][0]
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
2009-04-09 11:17:29 +02:00
|
|
|
|
2010-09-16 14:44:37 +02:00
|
|
|
it 'ignores spaces after names of inline templates' do
|
|
|
|
mock_app { enable :inline_templates }
|
|
|
|
assert_equal "There's a space after 'bar'!\n\n", @app.templates[:bar][0]
|
2010-09-19 13:26:26 +02:00
|
|
|
assert_equal "this is not foo\n\n", @app.templates[:"foo bar"][0]
|
2010-09-16 14:44:37 +02:00
|
|
|
end
|
|
|
|
|
2010-01-29 18:18:19 +01:00
|
|
|
it 'loads templates from given source file' do
|
|
|
|
mock_app { set :inline_templates, __FILE__ }
|
|
|
|
assert_equal "this is foo\n\n", @app.templates[:foo][0]
|
2009-05-23 16:15:19 +02:00
|
|
|
end
|
|
|
|
|
2010-01-29 18:18:19 +01:00
|
|
|
test 'inline_templates ignores IO errors' do
|
2009-04-09 11:17:29 +02:00
|
|
|
assert_nothing_raised {
|
|
|
|
mock_app {
|
2009-12-26 03:57:55 +01:00
|
|
|
set :inline_templates, '/foo/bar'
|
2009-04-09 11:17:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert @app.templates.empty?
|
|
|
|
end
|
2009-05-19 16:03:35 +02:00
|
|
|
|
2010-01-29 18:18:19 +01:00
|
|
|
it 'loads templates from specified views directory' do
|
|
|
|
render_app { render :test, :hello, :views => options.views + '/foo' }
|
|
|
|
|
|
|
|
assert_equal "from another views directory\n", body
|
|
|
|
end
|
|
|
|
|
2009-05-19 16:03:35 +02:00
|
|
|
it 'passes locals to the layout' do
|
|
|
|
mock_app {
|
|
|
|
template :my_layout do
|
|
|
|
'Hello <%= name %>!<%= yield %>'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/' do
|
|
|
|
erb '<p>content</p>', { :layout => :my_layout }, { :name => 'Mike'}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello Mike!<p>content</p>', body
|
|
|
|
end
|
2009-03-26 08:01:20 -07:00
|
|
|
|
|
|
|
it 'loads templates defined in subclasses' do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.template(:foo) { 'bar' }
|
|
|
|
render_app(base) { render :test, :foo }
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'bar', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses templates in superclasses before subclasses' do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.template(:foo) { 'template in superclass' }
|
2009-06-07 09:50:22 -07:00
|
|
|
assert_equal 'template in superclass', base.templates[:foo].first.call
|
|
|
|
|
|
|
|
mock_app(base) {
|
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
|
|
|
template(:foo) { 'template in subclass' }
|
|
|
|
get('/') { render :test, :foo }
|
|
|
|
}
|
|
|
|
assert_equal 'template in subclass', @app.templates[:foo].first.call
|
2009-03-26 08:01:20 -07:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'template in subclass', body
|
|
|
|
end
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
2009-04-17 14:17:15 +02:00
|
|
|
# __END__ : this is not the real end of the script.
|
|
|
|
|
2008-12-13 13:06:02 -08:00
|
|
|
__END__
|
|
|
|
|
|
|
|
@@ foo
|
|
|
|
this is foo
|
|
|
|
|
2010-09-16 14:44:37 +02:00
|
|
|
@@ bar
|
|
|
|
There's a space after 'bar'!
|
|
|
|
|
2010-09-19 13:26:26 +02:00
|
|
|
@@ foo bar
|
|
|
|
this is not foo
|
|
|
|
|
2008-12-13 13:06:02 -08:00
|
|
|
@@ layout
|
|
|
|
X
|
|
|
|
= yield
|
|
|
|
X
|