Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

start working on README tests

  • Loading branch information...
commit 26703870b0d4104d87efa412b2f4c440d6bd13d4 1 parent d59748c
@rkh rkh authored
Showing with 155 additions and 1 deletion.
  1. +2 −1  sinatra.gemspec
  2. +15 −0 test/helper.rb
  3. +138 −0 test/readme_test.rb
View
3  sinatra.gemspec
@@ -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
15 test/helper.rb
@@ -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 @@ 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)
View
138 test/readme_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.