spec/named_routes_spec.rb in sinatra-named-routes-0.0.1 vs spec/named_routes_spec.rb in sinatra-named-routes-0.1.0

- old
+ new

@@ -1,148 +1,106 @@ -require_relative 'spec_helper' +require File.join(File.dirname(__FILE__), 'spec_helper') describe Sinatra::NamedRoutes do def helper_route(&block) - result = nil - @helper_app.get('/helper_route') do - result = instance_eval(&block) - 'ok' - end - get '/helper_route' - last_response.should be_ok - body.should be == 'ok' - result - end + time = Time.now.usec - before do - app = nil - mock_app do register Sinatra::NamedRoutes - map(:hello, '/hello') + map(:path_named_params, '/hello/:name.:format') + map(:path_splats, '/hello/*.*') + map(:path_regexp, %r{/hello/([\w]+).([\w]+)}) + map(:path_named_captures, %r{/hello/(?<person>[^/?#]+)}) + map(:path_optional_named_captures, %r{/page(?<format>.[^/?#]+)?}) - map(:path_named, '/hello/:name') - map(:path_multi_named, '/hello/:name.:format') - map(:path_splat, '/hello/*') - map(:path_multi_splat, '/hello/*.*') - map(:path_regexp, %r{/hello/([\w]+)}) - map(:path_multi_regexp, %r{/hello/([\w]+).([\w]+)}) - - get('/') { 'hello' } - get(:hello) { 'hello world' } - get('/hello/:name') { |name| "hello #{name}" } - - app = self + get "/#{time}" do + instance_eval(&block) + end end - @helper_app = app + get "/#{time}" end - describe :helper_route do - it 'runs the block' do - ran = false - helper_route { ran = true } - ran.should be_true - end + describe :routing do + it 'does allow routing with symbols as paths' do + mock_app do + register Sinatra::NamedRoutes - it 'returns the block result' do - helper_route { 42 }.should be == 42 + map :test, '/test' + + get :test do + 'symbols work' + end + end + + get('/test').should be_ok + body.should be == 'symbols work' end - end + it 'does not break normal routing' do + mock_app do + register Sinatra::NamedRoutes - describe :routing do - it 'does still allow normal routing' do - get('/').should be_ok - body.should be == 'hello' + get '/normal' do + 'still works' + end + end - get('/hello/cristian').should be_ok - body.should be == 'hello cristian' + get('/normal').should be_ok + body.should be == 'still works' end - it 'does still allow routing with symbols as paths' do - get('/hello').should be_ok - body.should be == 'hello world' - end - end describe :path_helper do - it 'does not break normal behavior' do - helper_route do - url '/route_one', false - end.should be == '/route_one' - end + it 'does not break normal helper behavior' do + mock_app do + register Sinatra::NamedRoutes - it 'ignores params if path is not a symbol' do - helper_route do - url '/route_one', false, name: 'cristian' - end.should be == '/route_one' - end - - describe :named do - it 'returns the correct path if passed a hash with symbols as keys' do - helper_route do - url :path_multi_named, false, name: 'cristian', format: 'json' - end.should be == '/hello/cristian.json' + get '/' do + url '/route', false + end end - it 'returns the correct path if passed a hash with strings as keys' do - helper_route do - url :path_multi_named, false, 'name' => 'cristian', 'format' => 'json' - end.should be == '/hello/cristian.json' - end + get('/').should be_ok + body.should be == '/route' + end - it 'ignores keys that are left' do - helper_route do - url :path_multi_named, false, name: 'cristian', format: 'json', color: 'blue' - end.should be == '/hello/cristian.json' - end + it 'ignores params if path is not a symbol' do + mock_app do + register Sinatra::NamedRoutes - it 'throws an exception if required keys do not exist' do - expect do - helper_route do - url :path_multi_named, false, name: 'cristian', color: 'blue' - end - end.to raise_exception ArgumentError + get '/' do + url '/route', false, :name => 'cristian' + end end + get('/').should be_ok + body.should be == '/route' end - describe :splat do - it 'returns the correct url for path with splats' do - helper_route do - url :path_multi_splat, false, ['cristian', 'json'] - end.should be == '/hello/cristian.json' - end + it 'returns the correct urls' do + mock_app do + register Sinatra::NamedRoutes - it 'throws exception if params does not match number of splats' do - expect do - helper_route do - url :path_multi_splat, false, ['cristian'] - end - end.to raise_exception ArgumentError - end + map :path, '/hello/?:person?' - end + get '/test1' do + url :path, false, :person => 'cristian' + end - describe :regular_expression do - it 'returns the correct url for path with regular expressions' do - helper_route do - url :path_multi_regexp, false, ['cristian', 'json'] - end.should be == '/hello/cristian.json' + get '/test2' do + url :path, false + end end - it 'throws exception if params does not match number of captures' do - expect do - helper_route do - url :path_multi_regexp, false, ['cristian'] - end - end.to raise_exception ArgumentError - end + get('/test1').should be_ok + body.should be == '/hello/cristian' + get('/test2').should be_ok + body.should be == '/hello' end end end \ No newline at end of file