2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class ERBTest < Test::Unit::TestCase
|
2011-04-15 04:27:00 -04:00
|
|
|
def engine
|
|
|
|
Tilt::ERBTemplate
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
Tilt.prefer engine, :erb
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
def erb_app(&block)
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/', &block)
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/'
|
2008-02-21 01:45:17 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2011-04-15 04:27:00 -04:00
|
|
|
it 'uses the correct engine' do
|
|
|
|
assert_equal engine, Tilt[:erb]
|
|
|
|
end
|
|
|
|
|
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
|
2012-05-21 17:21:59 -04:00
|
|
|
erb_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
locals = {:foo => 'Bar'}
|
|
|
|
erb '<%= foo %>', :locals => locals
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
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
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
layout { 'THIS. IS. <%= yield.upcase %>!' }
|
|
|
|
get('/') { erb 'Sparta' }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
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
|
2012-05-21 17:21:59 -04:00
|
|
|
erb_app { erb 'Hello World', :layout => :layout2 }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
2011-04-15 04:10:41 -04:00
|
|
|
assert_body "ERB Layout!\nHello World"
|
2008-02-28 00:07:06 -05:00
|
|
|
end
|
2009-02-18 13:26:06 -05:00
|
|
|
|
|
|
|
it "renders erb with blocks" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-02-18 13:26:06 -05:00
|
|
|
def container
|
|
|
|
@_out_buf << "THIS."
|
|
|
|
yield
|
|
|
|
@_out_buf << "SPARTA!"
|
|
|
|
end
|
|
|
|
def is; "IS." end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { erb '<% container do %> <%= is %> <% end %>' }
|
|
|
|
end
|
2009-02-18 13:26:06 -05:00
|
|
|
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
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-02-22 02:16:53 -05:00
|
|
|
template(:inner) { "<inner><%= 'hi' %></inner>" }
|
|
|
|
template(:outer) { "<outer><%= erb :inner %></outer>" }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { erb :outer }
|
|
|
|
end
|
2009-02-22 02:16:53 -05:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal '<outer><inner>hi</inner></outer>', body
|
|
|
|
end
|
2013-02-17 06:53:34 -05:00
|
|
|
|
2013-03-14 14:20:51 -04:00
|
|
|
it "can render truly nested layouts by accepting a layout and a block with the contents" do
|
2013-02-17 06:53:34 -05:00
|
|
|
mock_app do
|
|
|
|
template(:main_outer_layout) { "<h1>Title</h1>\n<%= yield %>" }
|
|
|
|
template(:an_inner_layout) { "<h2>Subtitle</h2>\n<%= yield %>" }
|
|
|
|
template(:a_page) { "<p>Contents.</p>\n" }
|
|
|
|
get('/') do
|
|
|
|
erb :main_outer_layout, :layout => false do
|
|
|
|
erb :an_inner_layout do
|
|
|
|
erb :a_page
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_body "<h1>Title</h1>\n<h2>Subtitle</h2>\n<p>Contents.</p>\n"
|
|
|
|
end
|
2008-02-21 01:45:17 -05:00
|
|
|
end
|
2011-04-15 04:27:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'erubis'
|
|
|
|
class ErubisTest < ERBTest
|
|
|
|
def engine; Tilt::ErubisTemplate end
|
|
|
|
end
|
|
|
|
rescue LoadError
|
|
|
|
warn "#{$!.to_s}: skipping erubis tests"
|
|
|
|
end
|