From 14c12df89862fc57a105d07e112960746cee7a95 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Wed, 28 Nov 2007 00:10:12 -0800 Subject: [PATCH] returning --- lib/sinatra.rb | 3 ++- test/application_test.rb | 58 ++++++++++++++++++++++++++++++++++------ test/helper.rb | 5 ++-- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index b15f0e5b..529732ac 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -78,9 +78,10 @@ module Sinatra ) returned = context.instance_eval(&result.block) context.body ||= returned + context.body = String === context.body ? [*context.body] : context.body context.finish end - + end end diff --git a/test/application_test.rb b/test/application_test.rb index 9784a231..3216f5d6 100644 --- a/test/application_test.rb +++ b/test/application_test.rb @@ -35,15 +35,12 @@ context "Looking up a request" do end - -context "Calling an app" do +context "An app returns" do setup do @app = Sinatra::Application.new end - - # - 404 if no events found - + specify "404 if no events found" do request = Rack::MockRequest.new(@app) result = request.get('/') @@ -62,7 +59,50 @@ context "Calling an app" do result.body.should.equal 'Hello World' end - specify "evaluates events in a clean context" do + specify "an objects result from each if it has it" do + + class TesterWithEach + def each + yield 'foo' + yield 'bar' + yield 'baz' + end + end + + @app.define_event(:get, '/') do + TesterWithEach.new + end + + request = Rack::MockRequest.new(@app) + result = request.get('/') + result.should.be.ok + result.body.should.equal 'foobarbaz' + + end + + specify "the body set if set before the last" do + + @app.define_event(:get, '/') do + self.body = 'Blake' + 'Mizerany' + end + + request = Rack::MockRequest.new(@app) + result = request.get('/') + result.should.be.ok + result.body.should.equal 'Blake' + + end + +end + +context "Events in an app" do + + setup do + @app = Sinatra::Application.new + end + + specify "evaluate in a clean context" do Sinatra::EventContext.class_eval do def foo 'foo' @@ -79,7 +119,7 @@ context "Calling an app" do result.body.should.equal 'foo' end - specify "gives the event access to request, response, and params" do + specify "get access to request, response, and params" do @app.define_event(:get, '/:foo') do params[:foo] + params[:bar] end @@ -89,5 +129,7 @@ context "Calling an app" do result.should.be.ok result.body.should.equal 'foobaz' end - + end + + diff --git a/test/helper.rb b/test/helper.rb index cd7a39f1..f1080258 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,7 +1,8 @@ require File.dirname(__FILE__) + '/../lib/sinatra' -require 'rubygems' -require 'test/spec' +require "rubygems" +require "test/spec" +require "mocha" module Sinatra::TestHelper