ERubis support in to sinatra. With tests!

Signed-off-by: Simon Rozet <simon@rozet.name>
This commit is contained in:
Dylan Egan 2009-10-12 17:22:01 +11:00 committed by Simon Rozet
parent 5c6332c358
commit 1b2ab3a269
5 changed files with 92 additions and 0 deletions

View File

@ -288,6 +288,10 @@ module Sinatra
render :erb, template, options, locals
end
def erubis(template, options={}, locals={})
render :erubis, template, options, locals
end
def haml(template, options={}, locals={})
render :haml, template, options, locals
end

82
test/erubis_test.rb Normal file
View File

@ -0,0 +1,82 @@
require File.dirname(__FILE__) + '/helper'
require 'erubis'
class ERubisTest < Test::Unit::TestCase
def erubis_app(&block)
mock_app {
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
}
get '/'
end
it 'renders inline ERubis strings' do
erubis_app { erubis '<%= 1 + 1 %>' }
assert ok?
assert_equal '2', body
end
it 'renders .erubis files in views path' do
erubis_app { erubis :hello }
assert ok?
assert_equal "Hello World\n", body
end
it 'takes a :locals option' do
erubis_app {
locals = {:foo => 'Bar'}
erubis '<%= foo %>', :locals => locals
}
assert ok?
assert_equal 'Bar', body
end
it "renders with inline layouts" do
mock_app {
layout { 'THIS. IS. <%= yield.upcase %>!' }
get('/') { erubis 'Sparta' }
}
get '/'
assert ok?
assert_equal 'THIS. IS. SPARTA!', body
end
it "renders with file layouts" do
erubis_app {
erubis 'Hello World', :layout => :layout2
}
assert ok?
assert_equal "ERubis Layout!\nHello World\n", body
end
it "renders erubis with blocks" do
mock_app {
def container
@_out_buf << "THIS."
yield
@_out_buf << "SPARTA!"
end
def is; "IS." end
get '/' do
erubis '<% container do %> <%= is %> <% end %>'
end
}
get '/'
assert ok?
assert_equal 'THIS. IS. SPARTA!', body
end
it "can be used in a nested fashion for partials and whatnot" do
mock_app {
template(:inner) { "<inner><%= 'hi' %></inner>" }
template(:outer) { "<outer><%= erubis :inner %></outer>" }
get '/' do
erubis :outer
end
}
get '/'
assert ok?
assert_equal '<outer><inner>hi</inner></outer>', body
end
end

3
test/views/error.erubis Normal file
View File

@ -0,0 +1,3 @@
Hello <%= 'World' %>
<% raise 'Goodbye' unless defined?(french) && french %>
<% raise 'Au revoir' if defined?(french) && french %>

1
test/views/hello.erubis Normal file
View File

@ -0,0 +1 @@
Hello <%= 'World' %>

View File

@ -0,0 +1,2 @@
ERubis Layout!
<%= yield %>