Add full test coverage for all options

Test for host option
Test for port option
Test for server option
Test for app_file option
Test for the root option
Test for the views option
Test for the public option
Test for reload option
Test for lock option
Tests for dump_errors, sessions, logging, and static options
DRY-up options test setup
Clean up tests for clean_trace options
Test for run option
Run options tests with default options
Enable raise_errors options in test environment

See: http://github.com/sr/sinatra/commits/options/
This commit is contained in:
Simon Rozet 2009-02-14 20:22:52 +01:00 committed by Ryan Tomayko
parent 0c0fc962a8
commit 2fdecaee88
3 changed files with 270 additions and 18 deletions

View File

@ -916,7 +916,7 @@ module Sinatra
# Base class for classic style (top-level) applications.
class Default < Base
set :raise_errors, false
set :raise_errors, Proc.new { test? }
set :dump_errors, true
set :sessions, false
set :logging, true

View File

@ -11,13 +11,6 @@ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
require 'test/unit'
require 'sinatra/test'
Sinatra::Default.set(
:environment => :test,
:run => false,
:raise_errors => true,
:logging => false
)
class Sinatra::Base
# Allow assertions in request context
include Test::Unit::Assertions
@ -26,12 +19,33 @@ end
class Test::Unit::TestCase
include Sinatra::Test
def setup
Sinatra::Default.set(
:environment => :test,
:run => false,
:raise_errors => true,
:logging => false
)
end
# Sets up a Sinatra::Base subclass defined with the block
# given. Used in setup or individual spec methods to establish
# the application.
def mock_app(base=Sinatra::Base, &block)
@app = Sinatra.new(base, &block)
end
def restore_default_options
Sinatra::Default.set(
:raise_errors => Proc.new { test? },
:dump_errors => true,
:sessions => false,
:logging => true,
:methodoverride => true,
:static => true,
:run => false
)
end
end
##
@ -53,6 +67,17 @@ def describe(*args, &block)
def self.after(&block) define_method(:teardown, &block) end
end
klass.class_eval &block
klass
end
def describe_option(name, &block)
klass = describe("Option #{name}", &block)
klass.before do
restore_default_options
@base = Sinatra.new
@default = Class.new(Sinatra::Default)
end
klass
end
# Do not output warnings for the duration of the block.

View File

@ -1,7 +1,10 @@
require File.dirname(__FILE__) + '/helper'
describe 'Options' do
before { @app = Class.new(Sinatra::Base) }
before do
restore_default_options
@app = Sinatra.new
end
it 'sets options to literal values' do
@app.set(:foo, 'bar')
@ -96,17 +99,17 @@ describe 'Options' do
end
end
describe 'clean_trace' do
before do
@app = Class.new(Sinatra::Base)
end
describe_option 'clean_trace' do
def clean_backtrace(trace)
@app.new.send(:clean_backtrace, trace)
@base.new.send(:clean_backtrace, trace)
end
it 'is enabled by default' do
assert @app.clean_trace
it 'is enabled on Base' do
assert @base.clean_trace?
end
it 'is enabled on Default' do
assert @default.clean_trace?
end
it 'does nothing when disabled' do
@ -115,7 +118,7 @@ describe 'clean_trace' do
"./myapp:42",
("#{Gem.dir}/some/lib.rb" if defined?(Gem))
].compact
@app.set :clean_trace, false
@base.set :clean_trace, false
assert_equal backtrace, clean_backtrace(backtrace)
end
@ -138,3 +141,227 @@ describe 'clean_trace' do
end
end
end
describe_option 'run' do
it 'is disabled on Base' do
assert ! @base.run?
end
it 'is disabled on Default' do
assert ! @default.run?
end
# TODO: it 'is enabled when $0 == app_file'
end
describe_option 'raise_errors' do
it 'is enabled on Base' do
assert @base.raise_errors?
end
it 'is enabled on Default only in test' do
@default.set(:environment, :development)
assert @default.development?
assert ! @default.raise_errors?, "disabled development"
@default.set(:environment, :production)
assert ! @default.raise_errors?
@default.set(:environment, :test)
assert @default.raise_errors?
end
end
describe_option 'dump_errors' do
it 'is disabled on Base' do
assert ! @base.dump_errors?
end
it 'is enabled on Default' do
assert @default.dump_errors?
end
it 'dumps exception with backtrace to rack.errors' do
Sinatra::Default.disable(:raise_errors)
mock_app(Sinatra::Default) {
error do
error = @env['rack.errors'].instance_variable_get(:@error)
error.rewind
error.read
end
get '/' do
raise
end
}
get '/'
assert body.include?("RuntimeError") && body.include?("options_test.rb")
end
end
describe_option 'sessions' do
it 'is disabled on Base' do
assert ! @base.sessions?
end
it 'is disabled on Default' do
assert ! @default.sessions?
end
# TODO: it 'uses Rack::Session::Cookie when enabled' do
end
describe_option 'logging' do
it 'is disabled on Base' do
assert ! @base.logging?
end
it 'is enabled on Default' do
assert @default.logging?
end
# TODO: it 'uses Rack::CommonLogger when enabled' do
end
describe_option 'static' do
it 'is disabled on Base' do
assert ! @base.static?
end
it 'is enabled on Default' do
assert @default.static?
end
# TODO: it setup static routes if public is enabled
# TODO: however, that's already tested in static_test so...
end
describe_option 'host' do
it 'defaults to 0.0.0.0' do
assert_equal '0.0.0.0', @base.host
assert_equal '0.0.0.0', @default.host
end
end
describe_option 'port' do
it 'defaults to 4567' do
assert_equal 4567, @base.port
assert_equal 4567, @default.port
end
end
describe_option 'server' do
it 'is one of thin, mongrel, webrick' do
assert_equal %w[thin mongrel webrick], @base.server
assert_equal %w[thin mongrel webrick], @default.server
end
end
describe_option 'app_file' do
it 'is nil' do
assert @base.app_file.nil?
assert @default.app_file.nil?
end
end
describe_option 'root' do
it 'is nil if app_file is not set' do
assert @base.root.nil?
assert @default.root.nil?
end
it 'is equal to the expanded basename of app_file' do
@base.app_file = __FILE__
assert_equal File.expand_path(File.dirname(__FILE__)), @base.root
@default.app_file = __FILE__
assert_equal File.expand_path(File.dirname(__FILE__)), @default.root
end
end
describe_option 'views' do
it 'is nil if root is not set' do
assert @base.views.nil?
assert @default.views.nil?
end
it 'is set to root joined with views/' do
@base.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/views", @base.views
@default.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/views", @default.views
end
end
describe_option 'public' do
it 'is nil if root is not set' do
assert @base.public.nil?
assert @default.public.nil?
end
it 'is set to root joined with public/' do
@base.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/public", @base.public
@default.root = File.dirname(__FILE__)
assert_equal File.dirname(__FILE__) + "/public", @default.public
end
end
describe_option 'reload' do
it 'is enabled when
app_file is set,
is not a rackup file,
and we are in development' do
@base.app_file = __FILE__
@base.set(:environment, :development)
assert @base.reload?
@default.app_file = __FILE__
@default.set(:environment, :development)
assert @default.reload?
end
it 'is disabled if app_file is not set' do
assert ! @base.reload?
assert ! @default.reload?
end
it 'is disabled if app_file is a rackup file' do
@base.app_file = 'config.ru'
assert ! @base.reload?
@default.app_file = 'config.ru'
assert ! @base.reload?
end
it 'is disabled if we are not in development' do
@base.set(:environment, :foo)
assert ! @base.reload
@default.set(:environment, :bar)
assert ! @default.reload
end
end
describe_option 'lock' do
it 'is enabled when reload is enabled' do
@base.enable(:reload)
assert @base.lock?
@default.enable(:reload)
assert @default.lock?
end
it 'is disabled when reload is disabled' do
@base.disable(:reload)
assert ! @base.lock?
@default.disable(:reload)
assert ! @default.lock?
end
end