1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/compat/haml_test.rb

234 lines
4.2 KiB
Ruby
Raw Normal View History

I knew I shoulda taken that left turn at Hoboken This is a fairly large reworking of Sinatra's innards. Although most of the internal implementation has been modified, it provides the same basic feature set and is meant to be compatible with Sinatra 0.3.2. * The Event and EventContext classes have been removed. Sinatra applications are now defined within the class context of a Sinatra::Base subclass; each request is processed within a new instance. * Sinatra::Base can be used as a base class for multiple Rack applications within a single process and can be used as Rack middleware. * The routing and result type processing implementation has been simplified and enhanced a bit. There's a new route conditions system for things like :agent/:host matching and a request level #pass method has been added to allow an event handler to exit immediately, passing control to the next matching route. * Regular expressions may now be used in route patterns. Captures are available as an array from "params[:captures]". * The #body helper method now takes a block. The block is not evaluated until an attempt is made to read the body. * Options are now dynamically generated class attributes on the Sinatra::Base subclass (instead of OpenStruct); options are inherited by subclasses and may be overridden up the inheritance hierarchy. The Base.set manages all option related stuff. * The application file (app_file) detection heuristics are bit more sane now. This fixes some bugs with reloading and public/views directory detection. All thin / passenger issues of these type should be better now. * Error mappings are now split into to distinct layers: exception mappings and custom error pages. Exception mappings are registered with 'error(Exception)' and are run only when the app raises an exception. Custom error pages are registered with error(status_code) and are run any time the response has the status code specified. It's also possible to register an error page for a range of status codes: 'error(500..599)'. * The spec and unit testing extensions have been modified to take advantage of the ability to have multiple Sinatra applications. The Sinatra::Test module must be included within the TestCase in order to take advantage of these methods (unless the 'sinatra/compat' library has been required). * Rebuilt specs from scratch for better coverage and organization. Sinatra 3.2 unit tests have been retained under ./compat to ensure a baseline level of compatibility with previous versions; use the 'rake compat' task to run these. A large number of existing Sinatra idioms have been deprecated but continue to be supported through the 'sinatra/compat' library. * The "set_option" and "set_options" methods have been deprecated due to redundancy; use "set". * The "env" option (Sinatra::Base.env) has been renamed to "environment" and deprecated because it's too easy to confuse with the request-level Rack environment Hash (Sinatra::Base#env). * The request level "stop" method has been renamed "halt" and deprecated. This is for consistency with `throw :halt`. * The request level "entity_tag" method has been renamed "etag" and deprecated. Both versions were previously supported. * The request level "headers" method has been deprecated. Use response['Header-Name'] to access and modify response headers. * Sinatra.application is deprecated. Use Sinatra::Application instead. * Setting Sinatra.application = nil to reset an application is deprecated. You shouldn't have to reset objects anymore. * The Sinatra.default_options Hash is deprecated. Modifying this object now results in "set(key, value)" invocations on the Sinatra::Base subclass. * The "body.to_result" convention has been deprecated. * The ServerError exception has been deprecated. Any Exception is now considered a ServerError.
2008-12-13 16:06:02 -05:00
require File.dirname(__FILE__) + '/helper'
context "Haml" do
setup do
Sinatra.application = nil
end
context "without layouts" do
setup do
Sinatra.application = nil
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
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
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
end
describe 'Options passed to the HAML interpreter' do
setup do
Sinatra.application = nil
end
specify 'are empty be default' do
get '/' do
haml 'foo'
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
end
end