require File.expand_path('../helper', __FILE__)
begin
require 'haml'
class HAMLTest < Minitest::Test
def haml_app(&block)
mock_app do
set :views, __dir__ + '/views'
get('/', &block)
end
get '/'
end
it 'renders inline HAML strings' do
haml_app { haml '%h1 Hiya' }
assert ok?
assert_equal "
Hiya
\n", body
end
it 'renders .haml files in views path' do
haml_app { haml :hello }
assert ok?
assert_equal "Hello From Haml
\n", body
end
it "renders with inline layouts" do
mock_app do
layout { %q(%h1= 'THIS. IS. ' + yield.upcase) }
get('/') { haml '%em Sparta' }
end
get '/'
assert ok?
assert_equal "THIS. IS. SPARTA
\n", body
end
it "renders with file layouts" do
haml_app { haml 'Hello World', :layout => :layout2 }
assert ok?
assert_equal "HAML Layout!
\nHello World
\n", body
end
it "raises error if template not found" do
mock_app { get('/') { haml :no_such_template } }
assert_raises(Errno::ENOENT) { get('/') }
end
it "passes HAML options to the Haml engine" do
mock_app {
get('/') { haml "!!!\n%h1 Hello World", :format => :html5 }
}
get '/'
assert ok?
assert_equal "\nHello World
\n", body
end
it "passes default HAML options to the Haml engine" do
mock_app do
set :haml, {:format => :html5}
get('/') { haml "!!!\n%h1 Hello World" }
end
get '/'
assert ok?
assert_equal "\nHello World
\n", body
end
it "merges the default HAML options with the overrides and passes them to the Haml engine" do
mock_app do
set :haml, {:format => :html5, :attr_wrapper => '"'} # default HAML attr are
get('/') { haml "!!!\n%h1{:class => :header} Hello World" }
get('/html4') {
haml "!!!\n%h1{:class => 'header'} Hello World", :format => :html4
}
end
get '/'
assert ok?
assert_equal "\n\n", body
get '/html4'
assert ok?
assert_match(/^ { :foo => 'bar' }}
assert_equal "bar\n", 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\n= yield" }
template(:an_inner_layout) { "%h2 Subtitle\n= yield" }
template(:a_page) { "%p Contents." }
get('/') do
haml :main_outer_layout, :layout => false do
haml :an_inner_layout do
haml :a_page
end
end
end
end
get '/'
assert ok?
assert_body "Title
\nSubtitle
\nContents.
\n"
end
end
rescue LoadError
warn "#{$!}: skipping haml tests"
end