2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class ERBTest < Test::Unit::TestCase
|
2008-12-13 16:06:02 -05:00
|
|
|
def erb_app(&block)
|
|
|
|
mock_app {
|
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
|
|
|
get '/', &block
|
|
|
|
}
|
|
|
|
get '/'
|
2008-02-21 01:45:17 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders inline ERB strings' do
|
|
|
|
erb_app { erb '<%= 1 + 1 %>' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal '2', body
|
2008-02-21 01:45:17 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders .erb files in views path' do
|
|
|
|
erb_app { erb :hello }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "Hello World\n", body
|
2008-02-21 01:45:17 -05:00
|
|
|
end
|
2008-02-28 00:07:06 -05:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'takes a :locals option' do
|
|
|
|
erb_app {
|
|
|
|
locals = {:foo => 'Bar'}
|
|
|
|
erb '<%= foo %>', :locals => locals
|
|
|
|
}
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Bar', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-02-28 00:07:06 -05:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "renders with inline layouts" do
|
|
|
|
mock_app {
|
|
|
|
layout { 'THIS. IS. <%= yield.upcase %>!' }
|
|
|
|
get('/') { erb 'Sparta' }
|
|
|
|
}
|
|
|
|
get '/'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'THIS. IS. SPARTA!', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-02-28 00:07:06 -05:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "renders with file layouts" do
|
|
|
|
erb_app {
|
|
|
|
erb 'Hello World', :layout => :layout2
|
|
|
|
}
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "ERB Layout!\nHello World\n", body
|
2008-02-28 00:07:06 -05:00
|
|
|
end
|
2009-02-18 13:26:06 -05:00
|
|
|
|
|
|
|
it "renders erb with blocks" do
|
|
|
|
mock_app {
|
|
|
|
def container
|
|
|
|
@_out_buf << "THIS."
|
|
|
|
yield
|
|
|
|
@_out_buf << "SPARTA!"
|
|
|
|
end
|
|
|
|
def is; "IS." end
|
|
|
|
get '/' do
|
|
|
|
erb '<% container do %> <%= is %> <% end %>'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal 'THIS. IS. SPARTA!', body
|
|
|
|
end
|
2009-02-22 02:16:53 -05:00
|
|
|
|
|
|
|
it "can be used in a nested fashion for partials and whatnot" do
|
|
|
|
mock_app {
|
|
|
|
template(:inner) { "<inner><%= 'hi' %></inner>" }
|
|
|
|
template(:outer) { "<outer><%= erb :inner %></outer>" }
|
|
|
|
get '/' do
|
|
|
|
erb :outer
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal '<outer><inner>hi</inner></outer>', body
|
|
|
|
end
|
2008-02-21 01:45:17 -05:00
|
|
|
end
|