test/templates_test.rb in adamwiggins-sinatra-0.8.9 vs test/templates_test.rb in adamwiggins-sinatra-0.10.1
- old
+ new
@@ -1,15 +1,11 @@
-require 'test/spec'
-require 'sinatra/base'
-require 'sinatra/test'
+require File.dirname(__FILE__) + '/helper'
-describe 'Templating' do
- include Sinatra::Test
-
- def render_app(&block)
- mock_app {
- def render_test(template, data, options, &block)
+class TemplatesTest < Test::Unit::TestCase
+ def render_app(base=Sinatra::Base, &block)
+ mock_app(base) {
+ def render_test(data, options, locals, &block)
inner = block ? block.call : ''
data + inner
end
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
@@ -26,61 +22,114 @@
File.unlink(layout) rescue nil
end
it 'renders String templates directly' do
render_app { render :test, 'Hello World' }
- should.be.ok
- body.should.equal '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'} }
- should.be.ok
- body.should.equal 'Hello World'
+ assert ok?
+ assert_equal 'Hello World', body
end
it 'looks up Symbol templates in views directory' do
render_app { render :test, :hello }
- should.be.ok
- body.should.equal "Hello World!\n"
+ 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 }
- should.be.ok
- body.should.equal "Layout!\nHello World!\n"
+ 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 }
- should.be.ok
- body.should.equal "Layout!\nHello World!\n"
+ 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 }
- should.be.ok
- body.should.equal "Layout 2!\nHello World!\n"
+ 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 }
- should.be.ok
- body.should.equal "Layout 3!\nHello World!\n"
+ assert ok?
+ assert_equal "Layout 3!\nHello World!\n", body
end
it 'loads templates from source file with use_in_file_templates!' do
mock_app {
use_in_file_templates!
}
- @app.templates[:foo].should.equal "this is foo\n\n"
- @app.templates[:layout].should.equal "X\n= yield\nX\n"
+ assert_equal "this is foo\n\n", @app.templates[:foo][:template]
+ assert_equal "X\n= yield\nX\n", @app.templates[:layout][:template]
end
+
+ 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
+
+ test 'use_in_file_templates simply ignores IO errors' do
+ assert_nothing_raised {
+ mock_app {
+ use_in_file_templates!('/foo/bar')
+ }
+ }
+
+ assert @app.templates.empty?
+ end
+
+ 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
+
+ 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' }
+ render_app(base) { render :test, :foo }
+ @app.template(:foo) { 'template in subclass' }
+
+ get '/'
+ assert ok?
+ assert_equal 'template in subclass', body
+ end
end
+
+# __END__ : this is not the real end of the script.
__END__
@@ foo
this is foo