2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2010-03-15 22:20:55 -04:00
|
|
|
|
|
|
|
begin
|
2009-04-24 20:10:46 -04:00
|
|
|
require 'haml'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class HAMLTest < Test::Unit::TestCase
|
2008-12-13 16:06:02 -05:00
|
|
|
def haml_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-24 04:32:52 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders inline HAML strings' do
|
|
|
|
haml_app { haml '%h1 Hiya' }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>Hiya</h1>\n", body
|
2008-02-24 04:32:52 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'renders .haml files in views path' do
|
|
|
|
haml_app { haml :hello }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>Hello From Haml</h1>\n", body
|
2008-02-24 04:32:52 -05:00
|
|
|
end
|
2008-08-31 03:39:26 -04: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 { %q(%h1= 'THIS. IS. ' + yield.upcase) }
|
|
|
|
get('/') { haml '%em 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 "<h1>THIS. IS. <EM>SPARTA</EM></h1>\n", body
|
2008-02-28 00:07:06 -05:00
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "renders with file layouts" do
|
2012-05-21 17:21:59 -04:00
|
|
|
haml_app { haml 'Hello World', :layout => :layout2 }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<h1>HAML Layout!</h1>\n<p>Hello World</p>\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-04-19 10:43:12 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "raises error if template not found" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { get('/') { haml :no_such_template } }
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_raise(Errno::ENOENT) { get('/') }
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-04-19 10:43:12 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "passes HAML options to the Haml engine" do
|
2009-03-14 22:10:55 -04:00
|
|
|
mock_app {
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { haml "!!!\n%h1 Hello World", :format => :html5 }
|
2008-12-13 16:06:02 -05:00
|
|
|
}
|
2009-03-14 22:10:55 -04:00
|
|
|
get '/'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-04-19 10:43:12 -04:00
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it "passes default HAML options to the Haml engine" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
set :haml, {:format => :html5}
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { haml "!!!\n%h1 Hello World" }
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
get '/'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
|
2008-04-19 10:43:12 -04:00
|
|
|
end
|
2009-03-14 22:10:55 -04:00
|
|
|
|
|
|
|
it "merges the default HAML options with the overrides and passes them to the Haml engine" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-14 22:10:55 -04:00
|
|
|
set :haml, {:format => :html5, :attr_wrapper => '"'} # default HAML attr are <tag attr='single-quoted'>
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { haml "!!!\n%h1{:class => :header} Hello World" }
|
|
|
|
get('/html4') {
|
2009-03-27 13:00:33 -04:00
|
|
|
haml "!!!\n%h1{:class => 'header'} Hello World", :format => :html4
|
2012-05-21 17:21:59 -04:00
|
|
|
}
|
|
|
|
end
|
2009-03-14 22:10:55 -04:00
|
|
|
get '/'
|
|
|
|
assert ok?
|
|
|
|
assert_equal "<!DOCTYPE html>\n<h1 class=\"header\">Hello World</h1>\n", body
|
|
|
|
get '/html4'
|
|
|
|
assert ok?
|
|
|
|
assert_match(/^<!DOCTYPE html PUBLIC (.*) HTML 4.01/, body)
|
|
|
|
end
|
2010-12-16 16:57:24 -05:00
|
|
|
|
|
|
|
it "is possible to pass locals" do
|
|
|
|
haml_app { haml "= foo", :locals => { :foo => 'bar' }}
|
|
|
|
assert_equal "bar\n", body
|
|
|
|
end
|
2013-02-17 06:53:34 -05:00
|
|
|
|
|
|
|
it "can rendere 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 "<h1>Title</h1>\n<h2>Subtitle</h2>\n<p>Contents.</p>\n"
|
|
|
|
end
|
2008-02-24 04:32:52 -05:00
|
|
|
end
|
2011-01-25 03:27:15 -05:00
|
|
|
|
|
|
|
rescue LoadError
|
2010-03-15 22:20:55 -04:00
|
|
|
warn "#{$!.to_s}: skipping haml tests"
|
|
|
|
end
|