start working on README tests

This commit is contained in:
Konstantin Haase 2011-05-01 13:51:11 +02:00
parent d59748c13e
commit 26703870b0
3 changed files with 155 additions and 1 deletions

View File

@ -4,7 +4,7 @@ Gem::Specification.new do |s|
s.name = 'sinatra'
s.version = '1.3.0'
s.date = '2011-04-30'
s.date = '2011-05-01'
s.description = "Classy web-development dressed in a DSL"
s.summary = "Classy web-development dressed in a DSL"
@ -60,6 +60,7 @@ Gem::Specification.new do |s|
test/public/favicon.ico
test/radius_test.rb
test/rdoc_test.rb
test/readme_test.rb
test/request_test.rb
test/response_test.rb
test/result_test.rb

View File

@ -38,6 +38,13 @@ class Test::Unit::TestCase
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
@ -74,6 +81,14 @@ class Test::Unit::TestCase
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)

138
test/readme_test.rb Normal file
View File

@ -0,0 +1,138 @@
# Tests to check if all the README examples work.
require File.dirname(__FILE__) + '/helper'
class ReadmeTest < Test::Unit::TestCase
section "Sinatra" do
example do
mock_app { get('/') { 'Hello world!' } }
get '/'
assert_body 'Hello world!'
end
end
section "Routes" do
example do
mock_app do
get '/' do
".. show something .."
end
post '/' do
".. create something .."
end
put '/' do
".. replace something .."
end
patch '/' do
".. modify something .."
end
delete '/' do
".. annihilate something .."
end
options '/' do
".. appease something .."
end
end
get '/'
assert_body '.. show something ..'
post '/'
assert_body '.. create something ..'
put '/'
assert_body '.. replace something ..'
patch '/'
assert_body '.. modify something ..'
delete '/'
assert_body '.. annihilate something ..'
options '/'
assert_body '.. appease something ..'
end
example do
mock_app do
get '/hello/:name' do
# matches "GET /hello/foo" and "GET /hello/bar"
# params[:name] is 'foo' or 'bar'
"Hello #{params[:name]}!"
end
end
get '/hello/foo'
assert_body 'Hello foo!'
get '/hello/bar'
assert_body 'Hello bar!'
end
example do
mock_app do
get '/hello/:name' do |n|
"Hello #{n}!"
end
end
get '/hello/foo'
assert_body 'Hello foo!'
get '/hello/bar'
assert_body 'Hello bar!'
end
example do
mock_app do
get '/say/*/to/*' do
# matches /say/hello/to/world
params[:splat].inspect # => ["hello", "world"]
end
get '/download/*.*' do
# matches /download/path/to/file.xml
params[:splat].inspect # => ["path/to/file", "xml"]
end
end
get "/say/hello/to/world"
assert_body '["hello", "world"]'
get "/download/path/to/file.xml"
assert_body '["path/to/file", "xml"]'
end
example do
mock_app do
get %r{/hello/([\w]+)} do
"Hello, #{params[:captures].first}!"
end
end
get '/hello/foo'
assert_body 'Hello, foo!'
get '/hello/bar'
assert_body 'Hello, bar!'
end
example do
mock_app do
get %r{/hello/([\w]+)} do |c|
"Hello, #{c}!"
end
end
get '/hello/foo'
assert_body 'Hello, foo!'
get '/hello/bar'
assert_body 'Hello, bar!'
end
end
end