more tests

This commit is contained in:
Blake Mizerany 2007-11-27 21:55:05 -08:00
parent 095d95e36b
commit 18d3c8714f
4 changed files with 175 additions and 24 deletions

View File

@ -1,19 +1,32 @@
require 'rubygems'
require 'rack'
module Sinatra
Result = Struct.new(:body)
Result = Struct.new(:body, :params)
class Event
URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
attr_reader :path, :block
attr_reader :path, :block, :param_keys, :pattern
def initialize(path, &b)
@path = path
@block = b
@param_keys = []
regex = @path.to_s.gsub(PARAM) do
@param_keys << $1.intern
"(#{URI_CHAR}+)"
end
@pattern = /^#{regex}$/
end
def invoke
Result.new(block.call)
def invoke(env)
return unless pattern =~ env['PATH_INFO'].squeeze('/')
params = param_keys.zip($~.captures.map(&:from_param)).to_hash
Result.new(block.call, params)
end
end
@ -31,12 +44,81 @@ module Sinatra
event
end
def lookup(method, path)
events[method].find do |e|
result = e.invoke and break result
end
def lookup(env)
events[env['REQUEST_METHOD'].downcase.to_sym].eject(&[:invoke, env])
end
end
end
end
module Kernel
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end
end
class String
# Converts +self+ to an escaped URI parameter value
# 'Foo Bar'.to_param # => 'Foo%20Bar'
def to_param
URI.escape(self)
end
# Converts +self+ from an escaped URI parameter value
# 'Foo%20Bar'.from_param # => 'Foo Bar'
def from_param
URI.unescape(self)
end
end
class Hash
def to_params
map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
end
def symbolize_keys
self.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
end
def pass(*keys)
reject { |k,v| !keys.include?(k) }
end
end
class Symbol
def to_proc
Proc.new { |*args| args.shift.__send__(self, *args) }
end
end
class Array
def to_hash
self.inject({}) { |h, (k, v)| h[k] = v; h }
end
def to_proc
Proc.new { |*args| args.shift.__send__(self[0], *(args + self[1..-1])) }
end
end
module Enumerable
def eject(&block)
find { |e| result = block[e] and break result }
end
end

38
test/application_test.rb Normal file
View File

@ -0,0 +1,38 @@
require File.dirname(__FILE__) + '/helper'
context "Simple Events" do
setup do
@app = Sinatra::Application.new
end
specify "return what's at the end" do
@app.define_event(:get, '/') do
'Hello'
end
result = @app.lookup(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/'
)
result.should.not.be.nil
result.body.should.equal 'Hello'
end
specify "takes params in path" do
@app.define_event(:get, '/:foo') do
'World'
end
result = @app.lookup(
'REQUEST_METHOD' => 'GET',
'PATH_INFO' => '/bar'
)
result.should.not.be.nil
result.body.should.equal 'World'
result.params.should.equal :foo => 'bar'
end
end

View File

@ -5,19 +5,39 @@ require 'test/spec'
context "Simple Events" do
specify "return what's at the end" do
application = Sinatra::Application.new
route = application.define_event(:get, '/') do
'Hello'
end
result = application.lookup(:get, '/')
result.should.not.be.nil
result.body.should.equal 'Hello'
def simple_request_hash(method, path)
{
'REQUEST_METHOD' => method.to_s.upcase,
'PATH_INFO' => path
}
end
def invoke_simple(path, request_path)
event = Sinatra::Event.new(path) { 'Simple' }
event.invoke(simple_request_hash(:get, request_path))
end
specify "return last value" do
result = invoke_simple('/', '/')
result.should.not.be.nil
result.body.should.equal 'Simple'
result.params.should.equal Hash.new
end
specify "takes params in path" do
result = invoke_simple('/:foo/:bar', '/a/b')
result.should.not.be.nil
result.params.should.equal :foo => 'a', :bar => 'b'
# unscapes
result = invoke_simple('/:foo/:bar', '/a/blake%20mizerany')
result.should.not.be.nil
result.params.should.equal :foo => 'a', :bar => 'blake mizerany'
end
specify "ignores to many /'s" do
result = invoke_simple('/x/y', '/x//y')
result.should.not.be.nil
end
end

11
test/helper.rb Normal file
View File

@ -0,0 +1,11 @@
require File.dirname(__FILE__) + '/../lib/sinatra'
require 'rubygems'
require 'test/spec'
module Sinatra::TestHelper
end
Test::Unit::TestCase.send :include, Sinatra::TestHelper