From 03b080973b4d5b47bca29f0de35bd7d44d887578 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Tue, 27 Nov 2007 22:31:31 -0800 Subject: [PATCH] invoke results with a block; not the body --- lib/sinatra.rb | 4 ++-- test/application_test.rb | 20 +++++++++----------- test/events_test.rb | 11 ++++++----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index bf47c1d6..17b8f71e 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -3,7 +3,7 @@ require 'rack' module Sinatra - Result = Struct.new(:body, :params) + Result = Struct.new(:block, :params) class Event @@ -26,7 +26,7 @@ module Sinatra 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) + Result.new(block, params) end end diff --git a/test/application_test.rb b/test/application_test.rb index 599a5a2d..9017345f 100644 --- a/test/application_test.rb +++ b/test/application_test.rb @@ -1,15 +1,14 @@ require File.dirname(__FILE__) + '/helper' -context "Simple Events" do +context "Looking up a request" do setup do @app = Sinatra::Application.new end - specify "return what's at the end" do - @app.define_event(:get, '/') do - 'Hello' - end + specify "returns what's at the end" do + block = Proc.new { 'Hello' } + @app.define_event(:get, '/', &block) result = @app.lookup( 'REQUEST_METHOD' => 'GET', @@ -17,13 +16,12 @@ context "Simple Events" do ) result.should.not.be.nil - result.body.should.equal 'Hello' + result.block.should.be block end specify "takes params in path" do - @app.define_event(:get, '/:foo') do - 'World' - end + block = Proc.new { 'Hello' } + @app.define_event(:get, '/:foo', &block) result = @app.lookup( 'REQUEST_METHOD' => 'GET', @@ -31,8 +29,8 @@ context "Simple Events" do ) result.should.not.be.nil - result.body.should.equal 'World' + result.block.should.be block result.params.should.equal :foo => 'bar' end - + end diff --git a/test/events_test.rb b/test/events_test.rb index d8f0a726..6b23b115 100644 --- a/test/events_test.rb +++ b/test/events_test.rb @@ -12,15 +12,16 @@ context "Simple Events" do } end - def invoke_simple(path, request_path) - event = Sinatra::Event.new(path) { 'Simple' } + def invoke_simple(path, request_path, &b) + event = Sinatra::Event.new(path, &b) event.invoke(simple_request_hash(:get, request_path)) end specify "return last value" do - result = invoke_simple('/', '/') + block = Proc.new { 'Simple' } + result = invoke_simple('/', '/', &block) result.should.not.be.nil - result.body.should.equal 'Simple' + result.block.should.be block result.params.should.equal Hash.new end @@ -39,5 +40,5 @@ context "Simple Events" do result = invoke_simple('/x/y', '/x//y') result.should.not.be.nil end - + end