test/haml_test.rb in bmizerany-sinatra-0.3.2 vs test/haml_test.rb in bmizerany-sinatra-0.8.9
- old
+ new
@@ -1,233 +1,68 @@
require File.dirname(__FILE__) + '/helper'
-context "Haml" do
+describe "HAML Templates" do
+ def haml_app(&block)
+ mock_app {
+ set :views, File.dirname(__FILE__) + '/views'
+ get '/', &block
+ }
+ get '/'
+ end
- setup do
- Sinatra.application = nil
+ it 'renders inline HAML strings' do
+ haml_app { haml '%h1 Hiya' }
+ assert ok?
+ assert_equal "<h1>Hiya</h1>\n", body
end
- context "without layouts" do
+ it 'renders .haml files in views path' do
+ haml_app { haml :hello }
+ assert ok?
+ assert_equal "<h1>Hello From Haml</h1>\n", body
+ end
- setup do
- Sinatra.application = nil
- end
+ it "renders with inline layouts" do
+ mock_app {
+ layout { %q(%h1= 'THIS. IS. ' + yield.upcase) }
+ get('/') { haml '%em Sparta' }
+ }
+ get '/'
+ assert ok?
+ assert_equal "<h1>THIS. IS. <EM>SPARTA</EM></h1>\n", body
+ end
- specify "should render" do
-
- get '/no_layout' do
- haml '== #{1+1}'
- end
-
- get_it '/no_layout'
- should.be.ok
- body.should == "2\n"
-
- end
+ it "renders with file layouts" do
+ haml_app {
+ haml 'Hello World', :layout => :layout2
+ }
+ assert ok?
+ assert_equal "<h1>HAML Layout!</h1>\n<p>Hello World</p>\n", body
end
- context "with layouts" do
-
- setup do
- Sinatra.application = nil
- end
-
- specify "can be inline" do
-
- layout do
- '== This is #{yield}!'
- end
-
- get '/lay' do
- haml 'Blake'
- end
-
- get_it '/lay'
- should.be.ok
- body.should.equal "This is Blake\n!\n"
-
- end
-
- specify "can use named layouts" do
-
- layout :pretty do
- '%h1== #{yield}'
- end
-
- get '/pretty' do
- haml 'Foo', :layout => :pretty
- end
-
- get '/not_pretty' do
- haml 'Bar'
- end
-
- get_it '/pretty'
- body.should.equal "<h1>Foo</h1>\n"
-
- get_it '/not_pretty'
- body.should.equal "Bar\n"
-
- end
-
- specify "can be read from a file if they're not inlined" do
-
- get '/foo' do
- @title = 'Welcome to the Hello Program'
- haml 'Blake', :layout => :foo_layout,
- :views_directory => File.dirname(__FILE__) + "/views"
- end
-
- get_it '/foo'
- body.should.equal "Welcome to the Hello Program\nHi Blake\n"
-
- end
-
- specify "can be read from file and layout from text" do
- get '/foo' do
- haml 'Test', :layout => '== Foo #{yield}'
- end
-
- get_it '/foo'
-
- body.should.equal "Foo Test\n"
- end
-
+ it "raises error if template not found" do
+ mock_app {
+ get('/') { haml :no_such_template }
+ }
+ assert_raise(Errno::ENOENT) { get('/') }
end
- context "Templates (in general)" do
-
- setup do
- Sinatra.application = nil
- end
-
- specify "are read from files if Symbols" do
-
- get '/from_file' do
- @name = 'Alena'
- haml :foo, :views_directory => File.dirname(__FILE__) + "/views"
- end
-
- get_it '/from_file'
-
- body.should.equal "You rock Alena!\n"
-
- end
-
- specify "use layout.ext by default if available" do
-
- get '/' do
- haml :foo, :views_directory => File.dirname(__FILE__) + "/views/layout_test"
- end
-
- get_it '/'
- should.be.ok
- body.should.equal "x This is foo!\n x\n"
-
- end
-
- specify "renders without layout" do
-
- get '/' do
- haml :no_layout, :views_directory => File.dirname(__FILE__) + "/views/no_layout"
- end
-
- get_it '/'
- should.be.ok
- body.should.equal "<h1>No Layout!</h1>\n"
-
- end
-
- specify "can render with no layout" do
- layout do
- "X\n= yield\nX"
- end
-
- get '/' do
- haml 'blake', :layout => false
- end
-
- get_it '/'
-
- body.should.equal "blake\n"
- end
-
- specify "raises error if template not found" do
- get '/' do
- haml :not_found
- end
-
- lambda { get_it '/' }.should.raise(Errno::ENOENT)
- end
-
- specify "use layout.ext by default if available" do
-
- template :foo do
- 'asdf'
- end
-
- get '/' do
- haml :foo, :layout => false,
- :views_directory => File.dirname(__FILE__) + "/views/layout_test"
- end
-
- get_it '/'
- should.be.ok
- body.should.equal "asdf\n"
-
- end
-
+ it "passes HAML options to the Haml engine" do
+ haml_app {
+ haml "!!!\n%h1 Hello World", :options => {:format => :html5}
+ }
+ assert ok?
+ assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
end
- describe 'Options passed to the HAML interpreter' do
- setup do
- Sinatra.application = nil
- end
-
- specify 'are empty be default' do
-
+ it "passes default HAML options to the Haml engine" do
+ mock_app {
+ set :haml, {:format => :html5}
get '/' do
- haml 'foo'
+ haml "!!!\n%h1 Hello World"
end
-
- Haml::Engine.expects(:new).with('foo', {}).returns(stub(:render => 'foo'))
-
- get_it '/'
- should.be.ok
-
- end
-
- specify 'can be configured by passing :options to haml' do
-
- get '/' do
- haml 'foo', :options => {:format => :html4}
- end
-
- Haml::Engine.expects(:new).with('foo', {:format => :html4}).returns(stub(:render => 'foo'))
-
- get_it '/'
- should.be.ok
-
- end
-
- specify 'can be configured using set_option :haml' do
-
- configure do
- set_option :haml, :format => :html4,
- :escape_html => true
- end
-
- get '/' do
- haml 'foo'
- end
-
- Haml::Engine.expects(:new).with('foo', {:format => :html4,
- :escape_html => true}).returns(stub(:render => 'foo'))
-
- get_it '/'
- should.be.ok
-
- end
-
+ }
+ get '/'
+ assert ok?
+ assert_equal "<!DOCTYPE html>\n<h1>Hello World</h1>\n", body
end
-
end