test/templates_test.rb in sinatra-1.0 vs test/templates_test.rb in sinatra-1.1.a

- old
+ new

@@ -1,6 +1,7 @@ require File.dirname(__FILE__) + '/helper' +File.delete(File.dirname(__FILE__) + '/views/layout.test') rescue nil class TestTemplate < Tilt::Template def prepare end alias compile! prepare # for tilt < 0.7 @@ -12,13 +13,15 @@ Tilt.register 'test', self end class TemplatesTest < Test::Unit::TestCase - def render_app(base=Sinatra::Base, &block) + def render_app(base=Sinatra::Base, options = {}, &block) + base, options = Sinatra::Base, base if base.is_a? Hash mock_app(base) { set :views, File.dirname(__FILE__) + '/views' + set options get '/', &block template(:layout3) { "Layout 3!\n" } } get '/' end @@ -75,16 +78,43 @@ 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 } + assert ok? + 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 } + assert ok? + 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 + end + it 'loads templates from source file' do 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 } + 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__ } assert_equal "this is foo\n\n", @app.templates[:foo][0] end @@ -126,10 +156,28 @@ 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' } + 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' } + 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 + content_type :html + render :str, 'foo' + end + assert_equal 'text/html;charset=utf-8', response['Content-Type'] + end + 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 @@ -150,9 +198,15 @@ __END__ @@ foo this is foo + +@@ bar +There's a space after 'bar'! + +@@ foo bar +this is not foo @@ layout X = yield X