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)
|
|
|
|
mock_app {
|
|
|
|
set :views, File.dirname(__FILE__) + '/views'
|
|
|
|
get '/', &block
|
|
|
|
}
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
layout { %q(%h1= 'THIS. IS. ' + yield.upcase) }
|
|
|
|
get('/') { haml '%em Sparta' }
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
get '/' do
|
2009-03-27 13:00:33 -04:00
|
|
|
haml "!!!\n%h1 Hello World", :format => :html5
|
2009-03-14 22:10:55 -04:00
|
|
|
end
|
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
|
|
|
|
mock_app {
|
|
|
|
set :haml, {:format => :html5}
|
2008-04-19 10:43:12 -04:00
|
|
|
get '/' do
|
2008-12-13 16:06:02 -05:00
|
|
|
haml "!!!\n%h1 Hello World"
|
2008-04-19 10:43:12 -04:00
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
set :haml, {:format => :html5, :attr_wrapper => '"'} # default HAML attr are <tag attr='single-quoted'>
|
|
|
|
get '/' do
|
|
|
|
haml "!!!\n%h1{:class => :header} Hello World"
|
|
|
|
end
|
|
|
|
get '/html4' do
|
2009-03-27 13:00:33 -04:00
|
|
|
haml "!!!\n%h1{:class => 'header'} Hello World", :format => :html4
|
2009-03-14 22:10:55 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
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
|
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
|