sinatra/test/options_test.rb

96 lines
2.1 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 21:06:02 +00:00
require 'test/spec'
require 'sinatra/base'
require 'sinatra/test'
describe 'Options' do
include Sinatra::Test
before do
@app = Class.new(Sinatra::Base)
end
it 'sets options to literal values' do
@app.set(:foo, 'bar')
@app.should.respond_to? :foo
@app.foo.should.equal 'bar'
end
it 'sets options to Procs' do
@app.set(:foo, Proc.new { 'baz' })
@app.should.respond_to? :foo
@app.foo.should.equal 'baz'
end
it "sets multiple options with a Hash" do
@app.set :foo => 1234,
:bar => 'Hello World',
:baz => Proc.new { 'bizzle' }
@app.foo.should.equal 1234
@app.bar.should.equal 'Hello World'
@app.baz.should.equal 'bizzle'
end
it 'inherits option methods when subclassed' do
@app.set :foo, 'bar'
@app.set :biz, Proc.new { 'baz' }
sub = Class.new(@app)
sub.should.respond_to :foo
sub.foo.should.equal 'bar'
sub.should.respond_to :biz
sub.biz.should.equal 'baz'
end
it 'overrides options in subclass' do
@app.set :foo, 'bar'
@app.set :biz, Proc.new { 'baz' }
sub = Class.new(@app)
sub.set :foo, 'bling'
sub.foo.should.equal 'bling'
@app.foo.should.equal 'bar'
end
it 'creates setter methods when first defined' do
@app.set :foo, 'bar'
@app.should.respond_to 'foo='
@app.foo = 'biz'
@app.foo.should.equal 'biz'
end
it 'creates predicate methods when first defined' do
@app.set :foo, 'hello world'
@app.should.respond_to :foo?
@app.foo?.should.be true
@app.set :foo, nil
@app.foo?.should.be false
end
it 'uses existing setter methods if detected' do
class << @app
def foo
@foo
end
def foo=(value)
@foo = 'oops'
end
end
@app.set :foo, 'bam'
@app.foo.should.equal 'oops'
end
it "sets multiple options to true with #enable" do
@app.enable :sessions, :foo, :bar
@app.sessions.should.be true
@app.foo.should.be true
@app.bar.should.be true
end
it "sets multiple options to false with #disable" do
@app.disable :sessions, :foo, :bar
@app.sessions.should.be false
@app.foo.should.be false
@app.bar.should.be false
end
end