1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/test/readme_test.rb
Zachary Scott 46e8bd67b3 First pass at block styles for tests
TODO:
* test/routing_test.rb
* test/settings_test.rb
2012-05-21 17:21:59 -04:00

120 lines
2.5 KiB
Ruby

# Tests to check if all the README examples work.
require File.expand_path('../helper', __FILE__)
class ReadmeTest < Test::Unit::TestCase
example do
mock_app { get('/') { 'Hello world!' } }
get '/'
assert_body 'Hello world!'
end
section "Routes" do
example do
mock_app do
get('/') { ".. show something .." }
post('/') { ".. create something .." }
put('/') { ".. replace something .." }
patch('/') { ".. modify something .." }
delete('/') { ".. annihilate something .." }
options('/') { ".. appease something .." }
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 { get('/hello/:name') { |n| "Hello #{n}!" } }
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]+)}) {
"Hello, #{params[:captures].first}!"
}
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]+)}) { |c|
"Hello, #{c}!"
}
end
get '/hello/foo'
assert_body 'Hello, foo!'
get '/hello/bar'
assert_body 'Hello, bar!'
end
end
end