1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
require File.expand_path('../helper', __FILE__)
class ERBTest < Test::Unit::TestCase
def engine
Tilt::ERBTemplate
end
def setup
Tilt.prefer engine, :erb
super
end
def erb_app(&block)
mock_app do
set :views, File.dirname(__FILE__) + '/views'
get('/', &block)
end
get '/'
end
it 'uses the correct engine' do
assert_equal engine, Tilt[:erb]
end
it 'renders inline ERB strings' do
erb_app { erb '<%= 1 + 1 %>' }
assert ok?
assert_equal '2', body
end
it 'renders .erb files in views path' do
erb_app { erb :hello }
assert ok?
assert_equal "Hello World\n", body
end
it 'takes a :locals option' do
erb_app do
locals = {:foo => 'Bar'}
erb '<%= foo %>', :locals => locals
end
assert ok?
assert_equal 'Bar', body
end
it "renders with inline layouts" do
mock_app do
layout { 'THIS. IS. <%= yield.upcase %>!' }
get('/') { erb 'Sparta' }
end
get '/'
assert ok?
assert_equal 'THIS. IS. SPARTA!', body
end
it "renders with file layouts" do
erb_app { erb 'Hello World', :layout => :layout2 }
assert ok?
assert_body "ERB Layout!\nHello World"
end
it "renders erb with blocks" do
mock_app do
def container
@_out_buf << "THIS."
yield
@_out_buf << "SPARTA!"
end
def is; "IS." end
get('/') { erb '<% 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 do
template(:inner) { "<inner><%= 'hi' %></inner>" }
template(:outer) { "<outer><%= erb :inner %></outer>" }
get('/') { erb :outer }
end
get '/'
assert ok?
assert_equal '<outer><inner>hi</inner></outer>', body
end
it "can render truly nested layouts by accepting a layout and a block with the contents" do
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
end
begin
require 'erubis'
class ErubisTest < ERBTest
def engine; Tilt::ErubisTemplate end
end
rescue LoadError
warn "#{$!.to_s}: skipping erubis tests"
end
|