test/helper.rb in sinatra-base-1.0 vs test/helper.rb in sinatra-base-1.4.0

- old
+ new

@@ -1,7 +1,10 @@ ENV['RACK_ENV'] = 'test' +Encoding.default_external = "UTF-8" if defined? Encoding +RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE + begin require 'rack' rescue LoadError require 'rubygems' require 'rack' @@ -20,19 +23,32 @@ class Sinatra::Base # Allow assertions in request context include Test::Unit::Assertions end +class Rack::Builder + def include?(middleware) + @ins.any? { |m| p m ; middleware === m } + end +end + Sinatra::Base.set :environment, :test class Test::Unit::TestCase include Rack::Test::Methods class << self alias_method :it, :test + alias_method :section, :context end + def self.example(desc = nil, &block) + @example_count = 0 unless instance_variable_defined? :@example_count + @example_count += 1 + it(desc || "Example #{@example_count}", &block) + end + alias_method :response, :last_response setup do Sinatra::Base.set :environment, :test end @@ -50,16 +66,47 @@ def body response.body.to_s end + def assert_body(value) + if value.respond_to? :to_str + assert_equal value.lstrip.gsub(/\s*\n\s*/, ""), body.lstrip.gsub(/\s*\n\s*/, "") + else + assert_match value, body + end + end + + def assert_status(expected) + assert_equal Integer(expected), Integer(status) + end + + def assert_like(a,b) + pattern = /id=['"][^"']*["']|\s+/ + assert_equal a.strip.gsub(pattern, ""), b.strip.gsub(pattern, "") + end + + def assert_include(str, substr) + assert str.include?(substr), "expected #{str.inspect} to include #{substr.inspect}" + end + + def options(uri, params = {}, env = {}, &block) + request(uri, env.merge(:method => "OPTIONS", :params => params), &block) + end + + def patch(uri, params = {}, env = {}, &block) + request(uri, env.merge(:method => "PATCH", :params => params), &block) + end + # Delegate other missing methods to response. def method_missing(name, *args, &block) if response && response.respond_to?(name) response.send(name, *args, &block) else super end + rescue Rack::Test::Error + super end # Also check response since we delegate there. def respond_to?(symbol, include_private=false) super || (response && response.respond_to?(symbol, include_private))