sinatra/test/templates_test.rb

156 lines
3.8 KiB
Ruby
Raw Normal View History

require File.dirname(__FILE__) + '/helper'
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 21:06:02 +00:00
class TestTemplate < Tilt::Template
def compile!
end
def evaluate(scope, locals={}, &block)
inner = block ? block.call : ''
data + inner
end
Tilt.register 'test', self
end
class TemplatesTest < Test::Unit::TestCase
2009-03-26 15:01:20 +00:00
def render_app(base=Sinatra::Base, &block)
mock_app(base) {
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 21:06:02 +00:00
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
template(:layout3) { "Layout 3!\n" }
}
get '/'
end
def with_default_layout
layout = File.dirname(__FILE__) + '/views/layout.test'
File.open(layout, 'wb') { |io| io.write "Layout!\n" }
yield
ensure
File.unlink(layout) rescue nil
end
it 'renders String templates directly' do
render_app { render :test, 'Hello World' }
assert ok?
assert_equal 'Hello World', body
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 21:06:02 +00:00
end
it 'renders Proc templates using the call result' do
render_app { render :test, Proc.new {'Hello World'} }
assert ok?
assert_equal 'Hello World', body
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 21:06:02 +00:00
end
it 'looks up Symbol templates in views directory' do
render_app { render :test, :hello }
assert ok?
assert_equal "Hello World!\n", body
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 21:06:02 +00:00
end
it 'uses the default layout template if not explicitly overridden' do
with_default_layout do
render_app { render :test, :hello }
assert ok?
assert_equal "Layout!\nHello World!\n", body
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 21:06:02 +00:00
end
end
it 'uses the default layout template if not really overriden' do
with_default_layout do
render_app { render :test, :hello, :layout => true }
assert ok?
assert_equal "Layout!\nHello World!\n", body
end
end
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 21:06:02 +00:00
it 'uses the layout template specified' do
render_app { render :test, :hello, :layout => :layout2 }
assert ok?
assert_equal "Layout 2!\nHello World!\n", body
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 21:06:02 +00:00
end
it 'uses layout templates defined with the #template method' do
render_app { render :test, :hello, :layout => :layout3 }
assert ok?
assert_equal "Layout 3!\nHello World!\n", body
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 21:06:02 +00:00
end
2009-12-26 02:57:55 +00:00
it 'loads templates from source file with inline_templates enabled' do
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 21:06:02 +00:00
mock_app {
2009-12-26 02:57:55 +00:00
enable :inline_templates
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 21:06:02 +00:00
}
assert_equal "this is foo\n\n", @app.templates[:foo][0]
assert_equal "X\n= yield\nX\n", @app.templates[:layout][0]
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 21:06:02 +00:00
end
it 'loads templates from specified views directory' do
render_app { render :test, :hello, :views => options.views + '/foo' }
assert_equal "from another views directory\n", body
end
test 'use_in_file_templates simply ignores IO errors' do
assert_nothing_raised {
mock_app {
2009-12-26 02:57:55 +00:00
set :inline_templates, '/foo/bar'
}
}
assert @app.templates.empty?
end
it 'passes locals to the layout' do
mock_app {
template :my_layout do
'Hello <%= name %>!<%= yield %>'
end
get '/' do
erb '<p>content</p>', { :layout => :my_layout }, { :name => 'Mike'}
end
}
get '/'
assert ok?
assert_equal 'Hello Mike!<p>content</p>', body
end
2009-03-26 15:01:20 +00:00
it 'loads templates defined in subclasses' do
base = Class.new(Sinatra::Base)
base.template(:foo) { 'bar' }
render_app(base) { render :test, :foo }
assert ok?
assert_equal 'bar', body
end
it 'uses templates in superclasses before subclasses' do
base = Class.new(Sinatra::Base)
base.template(:foo) { 'template in superclass' }
assert_equal 'template in superclass', base.templates[:foo].first.call
mock_app(base) {
set :views, File.dirname(__FILE__) + '/views'
template(:foo) { 'template in subclass' }
get('/') { render :test, :foo }
}
assert_equal 'template in subclass', @app.templates[:foo].first.call
2009-03-26 15:01:20 +00:00
get '/'
assert ok?
assert_equal 'template in subclass', body
end
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 21:06:02 +00:00
end
# __END__ : this is not the real end of the script.
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 21:06:02 +00:00
__END__
@@ foo
this is foo
@@ layout
X
= yield
X