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