mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Convert tests to custom test/spec/mini [#87]
The "spec" task has been removed and Rake's built in test helper is used to run specs now so we should be able to test with multiple installed versions of Ruby.
This commit is contained in:
parent
c43058af67
commit
045f93be91
4 changed files with 383 additions and 376 deletions
16
Rakefile
16
Rakefile
|
@ -1,23 +1,17 @@
|
|||
require 'rubygems'
|
||||
require 'rake/clean'
|
||||
require 'rake/testtask'
|
||||
require 'fileutils'
|
||||
|
||||
task :default => :test
|
||||
task :spec => :test
|
||||
|
||||
# SPECS ===============================================================
|
||||
|
||||
desc 'Run specs with story style output'
|
||||
task :spec do
|
||||
pattern = ENV['TEST'] || '.*'
|
||||
sh "specrb --testcase '#{pattern}' --specdox -Ilib:test test/*_test.rb"
|
||||
Rake::TestTask.new(:test) do |t|
|
||||
t.test_files = FileList['test/*_test.rb']
|
||||
end
|
||||
|
||||
desc 'Run specs with unit test style output'
|
||||
task :test do |t|
|
||||
sh "specrb -Ilib:test test/*_test.rb"
|
||||
end
|
||||
|
||||
desc 'Run compatibility specs'
|
||||
desc 'Run compatibility specs (requires test/spec)'
|
||||
task :compat do |t|
|
||||
pattern = ENV['TEST'] || '.*'
|
||||
sh "specrb --testcase '#{pattern}' -Ilib:test compat/*_test.rb"
|
||||
|
|
|
@ -1,14 +1,7 @@
|
|||
begin
|
||||
require 'test/spec'
|
||||
rescue LoadError
|
||||
require 'rubygems'
|
||||
require 'test/spec'
|
||||
end
|
||||
|
||||
require 'rubygems' # required so that sinatra/base can require rack
|
||||
$:.unshift File.dirname(File.dirname(__FILE__)) + '/lib'
|
||||
require 'sinatra/base'
|
||||
require 'sinatra/test'
|
||||
require 'sinatra/test/spec'
|
||||
require 'sinatra/test/unit'
|
||||
|
||||
module Sinatra::Test
|
||||
# Sets up a Sinatra::Base subclass defined with the block
|
||||
|
@ -23,3 +16,21 @@ class Sinatra::Base
|
|||
# Allow assertions in request context
|
||||
include Test::Unit::Assertions
|
||||
end
|
||||
|
||||
##
|
||||
# test/spec/mini
|
||||
# http://pastie.caboo.se/158871
|
||||
# chris@ozmm.org
|
||||
#
|
||||
def describe(*args, &block)
|
||||
return super unless (name = args.first) && block
|
||||
klass = Class.new(Test::Unit::TestCase) do
|
||||
def self.it(name, &block)
|
||||
define_method("test_#{name.gsub(/\W/,'_')}", &block)
|
||||
end
|
||||
def self.xspecify(*args) end
|
||||
def self.before(&block) define_method(:setup, &block) end
|
||||
def self.after(&block) define_method(:teardown, &block) end
|
||||
end
|
||||
klass.class_eval &block
|
||||
end
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
describe 'Sinatra::Helpers' do
|
||||
describe '#status' do
|
||||
setup do
|
||||
describe '#status' do
|
||||
before do
|
||||
mock_app {
|
||||
get '/' do
|
||||
status 207
|
||||
|
@ -15,9 +14,9 @@ describe 'Sinatra::Helpers' do
|
|||
get '/'
|
||||
assert_equal 207, response.status
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#body' do
|
||||
describe '#body' do
|
||||
it 'takes a block for defered body generation' do
|
||||
mock_app {
|
||||
get '/' do
|
||||
|
@ -39,9 +38,9 @@ describe 'Sinatra::Helpers' do
|
|||
get '/'
|
||||
assert_equal 'Hello World', body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#redirect' do
|
||||
describe '#redirect' do
|
||||
it 'uses a 302 when only a path is given' do
|
||||
mock_app {
|
||||
get '/' do
|
||||
|
@ -69,9 +68,9 @@ describe 'Sinatra::Helpers' do
|
|||
assert_equal '', body
|
||||
assert_equal '/foo', response['Location']
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#error' do
|
||||
describe '#error' do
|
||||
it 'sets a status code and halts' do
|
||||
mock_app {
|
||||
get '/' do
|
||||
|
@ -110,9 +109,9 @@ describe 'Sinatra::Helpers' do
|
|||
assert_equal 500, status
|
||||
assert_equal 'FAIL', body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#not_found' do
|
||||
describe '#not_found' do
|
||||
it 'halts with a 404 status' do
|
||||
mock_app {
|
||||
get '/' do
|
||||
|
@ -125,9 +124,9 @@ describe 'Sinatra::Helpers' do
|
|||
assert_equal 404, status
|
||||
assert_equal '', body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#session' do
|
||||
describe '#session' do
|
||||
it 'uses the existing rack.session' do
|
||||
mock_app {
|
||||
get '/' do
|
||||
|
@ -151,28 +150,32 @@ describe 'Sinatra::Helpers' do
|
|||
get '/'
|
||||
assert_equal 'Hi', body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#media_type' do
|
||||
describe '#media_type' do
|
||||
include Sinatra::Helpers
|
||||
|
||||
it "looks up media types in Rack's MIME registry" do
|
||||
Rack::Mime::MIME_TYPES['.foo'] = 'application/foo'
|
||||
assert_equal 'application/foo', media_type('foo')
|
||||
assert_equal 'application/foo', media_type('.foo')
|
||||
assert_equal 'application/foo', media_type(:foo)
|
||||
end
|
||||
|
||||
it 'returns nil when given nil' do
|
||||
assert media_type(nil).nil?
|
||||
end
|
||||
|
||||
it 'returns nil when media type not registered' do
|
||||
assert media_type(:bizzle).nil?
|
||||
end
|
||||
|
||||
it 'returns the argument when given a media type string' do
|
||||
assert_equal 'text/plain', media_type('text/plain')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#content_type' do
|
||||
describe '#content_type' do
|
||||
it 'sets the Content-Type header' do
|
||||
mock_app {
|
||||
get '/' do
|
||||
|
@ -224,17 +227,18 @@ describe 'Sinatra::Helpers' do
|
|||
}
|
||||
assert_raise(RuntimeError) { get '/foo.xml' }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#send_file' do
|
||||
before {
|
||||
describe '#send_file' do
|
||||
before do
|
||||
@file = File.dirname(__FILE__) + '/file.txt'
|
||||
File.open(@file, 'wb') { |io| io.write('Hello World') }
|
||||
}
|
||||
after {
|
||||
end
|
||||
|
||||
after do
|
||||
File.unlink @file
|
||||
@file = nil
|
||||
}
|
||||
end
|
||||
|
||||
def send_file_app
|
||||
path = @file
|
||||
|
@ -279,9 +283,9 @@ describe 'Sinatra::Helpers' do
|
|||
get '/'
|
||||
assert not_found?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#last_modified' do
|
||||
describe '#last_modified' do
|
||||
before do
|
||||
now = Time.now
|
||||
mock_app {
|
||||
|
@ -310,9 +314,9 @@ describe 'Sinatra::Helpers' do
|
|||
assert_equal 304, status
|
||||
assert_equal '', body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#etag' do
|
||||
describe '#etag' do
|
||||
before do
|
||||
mock_app {
|
||||
get '/' do
|
||||
|
@ -356,6 +360,4 @@ describe 'Sinatra::Helpers' do
|
|||
get '/'
|
||||
assert_equal 'W/"FOO"', response['ETag']
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,7 +38,7 @@ describe "Middleware" do
|
|||
end
|
||||
end
|
||||
|
||||
specify "runs in the order defined" do
|
||||
it "runs in the order defined" do
|
||||
@app.use UpcaseMiddleware
|
||||
@app.use DowncaseMiddleware
|
||||
get '/Foo'
|
||||
|
@ -46,7 +46,7 @@ describe "Middleware" do
|
|||
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
||||
end
|
||||
|
||||
specify "resets the prebuilt pipeline when new middleware is added" do
|
||||
it "resets the prebuilt pipeline when new middleware is added" do
|
||||
@app.use UpcaseMiddleware
|
||||
get '/Foo'
|
||||
assert_equal "/FOO", body
|
||||
|
|
Loading…
Add table
Reference in a new issue