From 96477afca5ed0d06c4022252e6ac696d782474f2 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Fri, 26 Oct 2007 15:37:43 -0700 Subject: [PATCH] MUCH LOVE for testing --- lib/sinatra.rb | 2 +- lib/sinatra/environment.rb | 4 +++ lib/sinatra/event.rb | 10 +++++--- lib/sinatra/test/methods.rb | 59 +++++++++++++++++++++++++++++++++++++++++++++ lib/sinatra/test/spec.rb | 35 +++++++++++++++++++++++++++ lib/sinatra/test_methods.rb | 55 ------------------------------------------ test/helper.rb | 19 ++------------- 7 files changed, 108 insertions(+), 76 deletions(-) create mode 100644 lib/sinatra/test/methods.rb create mode 100644 lib/sinatra/test/spec.rb delete mode 100644 lib/sinatra/test_methods.rb diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 23cfef5..c89eead 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -43,7 +43,7 @@ Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb') Sinatra::Loader.load_files Dir.glob(File.dirname($0) + '/vendor/*/init.rb') at_exit do - Sinatra::Environment.prepare_loggers + Sinatra::Environment.prepare_loggers unless Sinatra::Environment.test? Sinatra::Irb.start! if Sinatra::Options.console Sinatra::Server.new.start unless Sinatra::Server.running end diff --git a/lib/sinatra/environment.rb b/lib/sinatra/environment.rb index ce9fe86..2e21eca 100644 --- a/lib/sinatra/environment.rb +++ b/lib/sinatra/environment.rb @@ -1,6 +1,10 @@ module Sinatra module Environment extend self + + def test? + Options.environment == :test + end def prepare Options.parse!(ARGV) diff --git a/lib/sinatra/event.rb b/lib/sinatra/event.rb index 5496535..61c39d6 100644 --- a/lib/sinatra/event.rb +++ b/lib/sinatra/event.rb @@ -52,6 +52,11 @@ module Sinatra self.before_filters = [] self.after_filters = [] + def self.reset! + self.before_filters.clear + self.after_filters.clear + end + def self.before_attend(method_name = nil, options ={}, &block) setup_filter(:before_filters, method_name, options, &block) end @@ -70,9 +75,7 @@ module Sinatra insert_index = options[:infront] == true ? 0 : -1 send(filter_set_name).insert(insert_index, value) end - - after_attend :log_event - + attr_reader :path, :verb def initialize(verb, path, register = true, &block) @@ -107,6 +110,7 @@ module Sinatra context.body context.body || body || '' call_filters(after_filters, context) end + context.log_event context end alias :call :attend diff --git a/lib/sinatra/test/methods.rb b/lib/sinatra/test/methods.rb new file mode 100644 index 0000000..9050282 --- /dev/null +++ b/lib/sinatra/test/methods.rb @@ -0,0 +1,59 @@ +require 'uri' + +module Sinatra + + # These methods are for integration testing without an internet connection. They are available in Test::Unit::TestCase and when in Irb. + + module Test + + module Methods + + # get_it, post_it, put_it, delete_it + # Executes the method and returns the result of the body + # + # options: + # +:params+ a hash of name parameters + # + # Example: + # get_it '/', :name => 'Blake' # => 'Hello Blake!' + # + %w(get post put delete).each do |verb| + module_eval <<-end_eval + def #{verb}_it(path, params = {}) + request = Rack::MockRequest.new(Sinatra::Dispatcher.new) + @response = request.#{verb} path, :input => generate_input(params) + body + end + end_eval + end + + def response + @response || Rack::MockResponse.new(404, {}, '') + end + + def status + response.status + end + + def text + response.body + end + alias :xml :text + alias :html :text + alias :body :text + + def headers + response.headers + end + + private + + def generate_input(params) + params.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&') + end + + end + + end + +end diff --git a/lib/sinatra/test/spec.rb b/lib/sinatra/test/spec.rb new file mode 100644 index 0000000..0a67d9f --- /dev/null +++ b/lib/sinatra/test/spec.rb @@ -0,0 +1,35 @@ +require File.dirname(__FILE__) + '/methods' + +module Sinatra + module Test + module Spec + def self.included(base) + require File.dirname(__FILE__) + '/../../sinatra' + require 'test/spec' + Server.running = true + Options.set_environment :test + Environment.prepare_loggers + end + end + end +end + +include Sinatra::Test::Spec + +class Test::Spec::TestCase + + module InstanceMethods + include Sinatra::Test::Methods + end + + alias :initialize_orig :initialize + + def initialize(name, parent=nil, superclass=Test::Unit::TestCase) + initialize_orig(name, parent, superclass) + + @testcase.setup do + Sinatra::EventManager.reset! + Sinatra::Event.reset! + end + end +end diff --git a/lib/sinatra/test_methods.rb b/lib/sinatra/test_methods.rb deleted file mode 100644 index 2273c8d..0000000 --- a/lib/sinatra/test_methods.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'uri' - -module Sinatra - - # These methods are for integration testing without an internet connection. They are available in Test::Unit::TestCase and when in Irb. - - module TestMethods - - # get_it, post_it, put_it, delete_it - # Executes the method and returns the result of the body - # - # options: - # +:params+ a hash of name parameters - # - # Example: - # get_it '/', :name => 'Blake' # => 'Hello Blake!' - # - %w(get post put delete).each do |verb| - module_eval <<-end_eval - def #{verb}_it(path, params = {}) - request = Rack::MockRequest.new(Sinatra::Dispatcher.new) - @response = request.#{verb} path, :input => generate_input(params) - body - end - end_eval - end - - def response - @response || Rack::MockResponse.new(404, {}, '') - end - - def status - response.status - end - - def text - response.body - end - alias :xml :text - alias :html :text - alias :body :text - - def headers - response.headers - end - - private - - def generate_input(params) - params.map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&') - end - - end - -end diff --git a/test/helper.rb b/test/helper.rb index b4a2712..175d1f5 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,17 +1,2 @@ -require File.dirname(__FILE__) + '/../lib/sinatra' - -%w(mocha test/spec).each do |library| - begin - require library - rescue - STDERR.puts "== Sinatra's tests need #{library} to run." - end -end - -Sinatra::Server.running = true -Sinatra::Options.set_environment :test -Sinatra::Environment.prepare_loggers - -class Test::Unit::TestCase - include Sinatra::TestMethods -end +require File.dirname(__FILE__) + '/../lib/sinatra/test/spec' +require 'mocha' -- 2.11.4.GIT