require 'test/spec' require 'randy/routing' context "Randy::Router" do specify "matches fixed routes" do r = Randy::Router.new r.register "/foo", {:value => "Foo!"} r.register "/bar", {:value => "Bar!"} r.route("/foo").should.equal :value => "Foo!" r.route("/bar").should.equal :value => "Bar!" r.route("/quux").should.be.nil end specify "matches fields" do r = Randy::Router.new r.register "/one/:a" r.register "/two/magick/:b", {:a => "42"} r.register "/two/:a/:b" r.route("/one/1").should.equal :a => "1" r.route("/two/1/2").should.equal :a => "1", :b => "2" r.route("/two/magick/2").should.equal :a => "42", :b => "2" r.route("/one").should.be.nil r.route("/two").should.be.nil r.route("/two/1").should.be.nil end specify "matches rests" do r = Randy::Router.new r.register "/one/:a" r.register "/one/*rest" r.route("/one/bar").should.equal :a => "bar" r.route("/one/bar/quux").should.equal :rest => ["bar", "quux"] r.route("/one/bar/quux/meh").should.equal :rest => ["bar", "quux", "meh"] end specify "can generate routes" do r = Randy::Router.new r.register "/one/:a" r.register "/two/magick/:b", {:a => "42"} r.register "/two/:a/:b" r.register "/two/:a/*rest" r.link_to(:a => 42).should.equal "/one/42" r.link_to(:b => 42).should.equal "/two/magick/42" r.link_to(:a => 23, :b => 42).should.equal "/two/23/42" r.link_to(:a => 23, :rest => "foo").should.equal "/two/23/foo" r.link_to(:a => 23, :rest => "foo", :c => "huh", :d => "bleh!"). should.equal "/two/23/foo;d=bleh%21&c=huh" r.link_to(:c => "huh").should.be.nil r.link_to(:rest => "foo").should.be.nil r.register "/third" r.register "/fourth", {:c => 5} r.link_to(:a => 42).should.equal "/one/42" r.link_to(:b => 42).should.equal "/two/magick/42" r.link_to(:a => 23, :rest => "foo").should.equal "/two/23/foo" r.link_to(:d => 5).should.equal "/third;d=5" r.link_to(:c => 5).should.equal "/fourth" r.link_to(:c => 6).should.equal "/fourth;c=6" end end