2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-06-07 12:50:22 -04:00
|
|
|
class TestTemplate < Tilt::Template
|
2010-03-04 06:12:36 -05:00
|
|
|
def prepare
|
2009-06-07 12:50:22 -04:00
|
|
|
end
|
2010-03-04 06:12:36 -05:00
|
|
|
alias compile! prepare # for tilt < 0.7
|
2009-06-07 12:50:22 -04:00
|
|
|
|
|
|
|
def evaluate(scope, locals={}, &block)
|
|
|
|
inner = block ? block.call : ''
|
|
|
|
data + inner
|
|
|
|
end
|
|
|
|
|
|
|
|
Tilt.register 'test', self
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class TemplatesTest < Test::Unit::TestCase
|
2009-03-26 11:01:20 -04:00
|
|
|
def render_app(base=Sinatra::Base, &block)
|
|
|
|
mock_app(base) {
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders Proc templates using the call result' do
|
|
|
|
render_app { render :test, Proc.new {'Hello World'} }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'looks up Symbol templates in views directory' do
|
|
|
|
render_app { render :test, :hello }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Hello World!\n", body
|
2008-12-13 16:06:02 -05: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 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout!\nHello World!\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-08 12:01:36 -05: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 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout!\nHello World!\n", body
|
2009-01-08 12:01:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'uses the layout template specified' do
|
|
|
|
render_app { render :test, :hello, :layout => :layout2 }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout 2!\nHello World!\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses layout templates defined with the #template method' do
|
|
|
|
render_app { render :test, :hello, :layout => :layout3 }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Layout 3!\nHello World!\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2010-01-29 12:18:19 -05:00
|
|
|
it 'loads templates from source file' do
|
|
|
|
mock_app { enable :inline_templates }
|
2009-06-07 12:50:22 -04: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 16:06:02 -05:00
|
|
|
end
|
2009-04-09 05:17:29 -04:00
|
|
|
|
2010-01-29 12:18:19 -05: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 10:15:19 -04:00
|
|
|
end
|
|
|
|
|
2010-01-29 12:18:19 -05:00
|
|
|
test 'inline_templates ignores IO errors' do
|
2009-04-09 05:17:29 -04:00
|
|
|
assert_nothing_raised {
|
|
|
|
mock_app {
|
2009-12-25 21:57:55 -05:00
|
|
|
set :inline_templates, '/foo/bar'
|
2009-04-09 05:17:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert @app.templates.empty?
|
|
|
|
end
|
2009-05-19 10:03:35 -04:00
|
|
|
|
2010-01-29 12:18:19 -05: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 10:03:35 -04: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 11:01:20 -04: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 12:50:22 -04: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 11:01:20 -04:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'template in subclass', body
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-04-17 08:17:15 -04:00
|
|
|
# __END__ : this is not the real end of the script.
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
__END__
|
|
|
|
|
|
|
|
@@ foo
|
|
|
|
this is foo
|
|
|
|
|
|
|
|
@@ layout
|
|
|
|
X
|
|
|
|
= yield
|
|
|
|
X
|