Skip to content

Commit

Permalink
Added more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bcarlso committed Aug 7, 2009
1 parent 4ad8249 commit e6677c9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 34 deletions.
7 changes: 2 additions & 5 deletions lib/sinatra/named_path_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ def self.registered(app)
app.set :named_paths, {}
end

def path name, pattern
verify_type_of(name)
named_paths[name] = pattern
end

def paths(paths)
paths.keys.each { | k | verify_type_of(k) }
named_paths.merge!(paths)
end

alias_method :path, :paths

def route(verb, path, options={}, &block)
path = named_paths[path] if path.kind_of? Symbol
super verb, path, options, &block
Expand Down
81 changes: 53 additions & 28 deletions spec/snap_spec.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,82 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'sinatra/base'
require 'rack/test'

class SinatraApp < Sinatra::Base
register Sinatra::PathDefinitionSupport
end

describe "Snap" do
after :all do
SinatraApp.named_paths = {}
describe "Snap DSL extensions" do
before :each do
create_and_register_snap
end

it "should extend the route method to support passing in symbols"
it "should register the DSL extension when included"
it "should register the helper methods when included"
it "should extend the route method to support passing in symbols" do
@app.path :home => 'homepage'
@app.get :home do
'irrelavent'
end

verify_route_added('GET', 'homepage')
end

it "should initialize the named paths collection when registered" do
SinatraApp.named_paths.should be_empty
@app.named_paths.should be_empty
end

it "should register a new named path when the path method is called" do
SinatraApp.class_eval do
path :name, '/url'
end
SinatraApp.named_paths.should == { :name => '/url' }
@app.path :name => '/url'
@app.named_paths.should == { :name => '/url' }
end

it "should register a collection of named paths when the paths method is called" do
SinatraApp.class_eval do
paths :name => '/url',
:other_name => '/other_url'
end
SinatraApp.named_paths.should == { :name => '/url',
:other_name => '/other_url' }
@app.paths :names => '/url',
:other_name => '/other_url'
@app.named_paths.should == { :names => '/url',
:other_name => '/other_url' }
end

it "should only allow symbols to be passed in as the first parameter of a path call" do
begin
SinatraApp.class_eval do
path 'anything other than a symbol', '/irrelavent-for-this-test'
end
@app.path 'anything other than a symbol', '/irrelavent-for-this-test'
fail
rescue ArgumentError
end
end

it "should only allow symbols to be passed in as the key to the paths hash" do
begin
SinatraApp.class_eval do
paths 'anything other than a symbol' => '/irrelavent-for-this-test',
:name => '/also-irrelavent'
end
@app.paths 'anything other than a symbol' => '/irrelavent-for-this-test',
:name => '/also-irrelavent'
fail
rescue ArgumentError
end
end
end

describe "Snap helper methods for building URLs" do
before :each do
create_and_register_snap
end

it "should return a simple path using path_to" do
@app.path :name => '/url'
@app.new.path_to(:name).should == '/url'
end

it "should retrieve a path by key and perform parameter substitution" do
@app.path :users => '/users/:name/tags/:tag'
@app.new.path_to(:users).with('bcarlso', 'sinatra').should == '/users/bcarlso/tags/sinatra'
end

it "should retrieve a path by key and perform parameter substitution on numeric values" do
@app.path :users => '/users/:id'
@app.new.path_to(:users).with(15).should == '/users/15'
end

it "should retrieve a path by key and perform parameter substitution on consecutive placeholders" do
@app.path :users => '/users/:id/:age'
@app.new.path_to(:users).with(15, 23).should == '/users/15/23'
end
end

describe "Snap auto registration capabilities" do
it "should register the DSL extension when included"
it "should register the helper methods when included"
end
11 changes: 10 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
require 'sinatra/named_path_support'

Spec::Runner.configure do |config|

def create_and_register_snap
@app = Sinatra.new
@app.register Sinatra::PathDefinitionSupport
@app.helpers Sinatra::PathBuilderSupport
end
def verify_route_added(verb, path)
routes = @app.routes[verb]
routes.size.should == 1
routes[0][0].should match(path)
end
end

0 comments on commit e6677c9

Please sign in to comment.