test/router_spec.rb in trellis-0.0.7 vs test/router_spec.rb in trellis-0.0.8

- old
+ new

@@ -49,6 +49,38 @@ it "with a path containing a single wildcard param it should capture the values in splat" do @router = Trellis::Router.new(:path => '/*') @router.keys.should include('splat') end + it "should be able to be sorted by 'matchability'" do + route_1 = Trellis::Router.new(:path => '/hello/:one/:two') + route_2 = Trellis::Router.new(:path => '/hello/jim/:last') + route_3 = Trellis::Router.new(:path => '/hello/*') + route_4 = Trellis::Router.new(:path => '/hello/*/:foo/*') + + routes = [ route_1, route_2, route_3, route_4 ].sort {|a,b| b.score <=> a.score } + + routes[0].should be(route_4) + routes[1].should be(route_2) + routes[2].should be(route_1) + routes[3].should be(route_3) + end + + it "a catch all route should always be last when sorted with other routes" do + route_1 = Trellis::Router.new(:path => '/admin/login') + route_2 = Trellis::Router.new(:path => '/*') + route_3 = Trellis::Router.new(:path => '/admin/pages') + route_4 = Trellis::Router.new(:path => '/admin/new/page') + route_5 = Trellis::Router.new(:path => '/admin/page/:id') + route_6 = Trellis::Router.new(:path => '/admin/page/:id/delete') + + routes = [ route_1, route_2, route_3, route_4, route_5, route_6 ].sort {|a,b| b.score <=> a.score } + + routes[0].should be(route_6) + routes[1].should be(route_4) + routes[2].should be(route_5) + routes[3].should be(route_3) + routes[4].should be(route_1) + routes[5].should be(route_2) + end + end