require 'test/spec' require 'pyrosoma/dispatch' context "Pyrosoma::Dispatch" do setup do @db = Pyrosoma::Database.new(:dir => Ferret::Store::RAMDirectory.new) @route = {:id => "route-root", :path => "/"} @route_rest = {:id => "route_rest", :path => "/**:rest"} @route_number = {:id => "route_number", :path => "/$"} @route_foo = {:id => "route_foo", :path => "/foo"} @dispatcher = Pyrosoma::Dispatcher.new @db end specify "should know about database changes" do @dispatcher.paths.should.be.empty @db << @route @dispatcher.paths.should.not.be.empty end specify "should just dispatch" do @db << @route @dispatcher.dispatch("/").should.equal @route end specify "should select the most specifc path" do @db << @route_rest << @route_number << @route_foo @dispatcher.dispatch("/foo")[:id].should.equal "route_foo" @dispatcher.dispatch("/77")[:id].should.equal "route_number" @dispatcher.dispatch("/baz")[:id].should.equal "route_rest" end specify "should merge matches" do @db << @route_rest << @route_number << @route_foo match = @dispatcher.dispatch("/baz") match[:id].should.equal "route_rest" match[:rest].should.equal "baz" match[1].should.equal "baz" end specify "should deal with non-existant paths" do @db << @route_number << @route_foo @dispatcher.dispatch("/baz").should.be.nil end specify "should update on starting" do @db << @route_rest << @route_number << @route_foo dispatcher = Pyrosoma::Dispatcher.new @db dispatcher.dispatch("/baz")[:id].should.equal "route_rest" end end