General spec coverage improvements (rcov: 98%)

This commit is contained in:
Ryan Tomayko 2009-01-19 17:58:26 -08:00
parent 59967cb8e6
commit bc7a939ce2
5 changed files with 93 additions and 3 deletions

View File

@ -338,8 +338,8 @@ module Sinatra
original_params = nested_params(@request.params)
routes.each do |pattern, keys, conditions, method_name|
if pattern =~ path
values = $~.captures.map{|val| val && unescape(val) }
if match = pattern.match(path)
values = match.captures.map{|val| val && unescape(val) }
params =
if keys.any?
keys.zip(values).inject({}) do |hash,(k,v)|
@ -608,7 +608,7 @@ module Sinatra
end
end
[/^#{pattern}$/, keys]
elsif path.respond_to? :=~
elsif path.respond_to? :match
[path, keys]
else
raise TypeError, path
@ -658,6 +658,7 @@ module Sinatra
begin
return Rack::Handler.get(server_name)
rescue LoadError
rescue NameError
end
end
fail "Server handler (#{servers.join(',')}) not found."

33
test/response_test.rb Normal file
View File

@ -0,0 +1,33 @@
require File.dirname(__FILE__) + '/helper'
describe 'Sinatra::Response' do
before do
@response = Sinatra::Response.new
end
it "initializes with 200, text/html, and empty body" do
assert_equal 200, @response.status
assert_equal 'text/html', @response['Content-Type']
assert_equal [], @response.body
end
it 'uses case insensitive headers' do
@response['content-type'] = 'application/foo'
assert_equal 'application/foo', @response['Content-Type']
assert_equal 'application/foo', @response['CONTENT-TYPE']
end
it 'writes to body' do
@response.body = 'Hello'
@response.write ' World'
assert_equal 'Hello World', @response.body
end
[204, 304].each do |status_code|
it "removes the Content-Type header and body when response status is #{status_code}" do
@response.status = status_code
@response.body = ['Hello World']
assert_equal [status_code, {}, []], @response.finish
end
end
end

View File

@ -76,6 +76,16 @@ describe 'Result Handling' do
assert_equal 'formula of', body
end
it "raises a TypeError when result is a non two or three tuple Array" do
mock_app {
get '/' do
[409, 'formula of', 'something else', 'even more']
end
}
assert_raise(TypeError) { get '/' }
end
it "sets status when result is a Fixnum status code" do
mock_app {
get('/') { 205 }

View File

@ -226,6 +226,11 @@ describe "Routing" do
assert_equal 'right on', body
end
it 'raises a TypeError when pattern is not a String or Regexp' do
@app = mock_app
assert_raise(TypeError) { @app.get(42){} }
end
it "returns response immediately on halt" do
mock_app {
get '/' do

41
test/server_test.rb Normal file
View File

@ -0,0 +1,41 @@
require File.dirname(__FILE__) + '/helper'
class Rack::Handler::Mock
extend Test::Unit::Assertions
def self.run(app, options={})
assert(app < Sinatra::Base)
assert_equal 9001, options[:Port]
assert_equal 'foo.local', options[:Host]
yield new
end
def stop
end
end
describe 'Sinatra::Base.run!' do
before do
mock_app {
set :server, 'mock'
set :host, 'foo.local'
set :port, 9001
}
$stdout = File.open('/dev/null', 'wb')
end
after { $stdout = STDOUT }
it "locates the appropriate Rack handler and calls ::run" do
@app.run!
end
it "sets options on the app before running" do
@app.run! :sessions => true
assert @app.sessions?
end
it "falls back on the next server handler when not found" do
@app.run! :server => %w[foo bar mock]
end
end