require 'test/spec' require 'pyrosoma/pattern' class Test::Spec::ShouldNot def match(expr) assert @object !~ expr end end class Array include Comparable end context "Pyrosoma::Pattern" do specify "should do simple matches" do "/".should.match Pyrosoma::Pattern.new("/") "/foo".should.match Pyrosoma::Pattern.new("/foo") "/foo/bar".should.match Pyrosoma::Pattern.new("/foo/bar") "/foo/bar=!quux".should.match Pyrosoma::Pattern.new("/foo/bar=!quux") "/foo".should.not.match Pyrosoma::Pattern.new("/") "/foo".should.not.match Pyrosoma::Pattern.new("/bar") end specify "should do wildcards" do "/foo/bar".should.match Pyrosoma::Pattern.new("/foo/*") "/foo/bar/quux".should.match Pyrosoma::Pattern.new("/foo/**") "/foo".should.not.match Pyrosoma::Pattern.new("/foo/*") "/foo/bar/quux".should.not.match Pyrosoma::Pattern.new("/foo/*") "/foo/bar/quux".should.match Pyrosoma::Pattern.new("/foo/*/quux") "/foo/bar/baz/quux".should.match Pyrosoma::Pattern.new("/foo/**/quux") "/foo/bar/baz/quux".should.not.match Pyrosoma::Pattern.new("/foo/*/quux") "/foo/bar/baz/quux".should.match Pyrosoma::Pattern.new("/foo/*/*/quux") end specify "should have alternation" do "/foo".should.match Pyrosoma::Pattern.new("/{foo,bar}") "/bar".should.match Pyrosoma::Pattern.new("/{foo,bar}") "/quux".should.not.match Pyrosoma::Pattern.new("/{foo,bar}") "/quux".should.not.match Pyrosoma::Pattern.new("/{foo,*}") "/*".should.match Pyrosoma::Pattern.new("/{foo,*}") match = "/foo/quux" =~ Pyrosoma::Pattern.new("/foo/{bar,quux}:a") match.should.be.kind_of Hash match[1].should.equal "quux" match[:a].should.equal "quux" end specify "should have fields" do "/foo".should.not.match Pyrosoma::Pattern.new("/foo/:a") match = "/foo/quux" =~ p=Pyrosoma::Pattern.new("/foo/:a") match.should.be.kind_of Hash match[1].should.equal "quux" match[:a].should.equal "quux" match = "/foo/quux" =~ Pyrosoma::Pattern.new("/foo/*:a") match.should.be.kind_of Hash match[1].should.equal "quux" match[:a].should.equal "quux" match = "/foo/one/two/three" =~ Pyrosoma::Pattern.new("/foo/:one/:two/:three") match.should.be.kind_of Hash match[1].should.equal "one" match[:one].should.equal "one" match[2].should.equal "two" match[:two].should.equal "two" match[3].should.equal "three" match[:three].should.equal "three" end specify "should have special wildcards" do "/digit/foo".should.not.match Pyrosoma::Pattern.new("/digit/%") "/digit/33".should.match Pyrosoma::Pattern.new("/digit/%") "/digit/-33".should.not.match Pyrosoma::Pattern.new("/digit/%") match = "/digit/73" =~ Pyrosoma::Pattern.new("/digit/%:a") match.should.be.kind_of Hash match[1].should.equal "73" match[:a].should.equal "73" "/digit/foo".should.not.match Pyrosoma::Pattern.new("/digit/$") "/digit/33".should.match Pyrosoma::Pattern.new("/digit/$") "/digit/cafebabe".should.match Pyrosoma::Pattern.new("/digit/$") "/digit/CAFEBABE".should.match Pyrosoma::Pattern.new("/digit/$") "/digit/4f47d323a".should.match Pyrosoma::Pattern.new("/digit/$") "/digit/-33".should.not.match Pyrosoma::Pattern.new("/digit/$") match = "/digit/4f47d323a" =~ Pyrosoma::Pattern.new("/digit/$:a") match.should.be.kind_of Hash match[1].should.equal "4f47d323a" match[:a].should.equal "4f47d323a" end specify "should have generality" do a = Pyrosoma::Pattern.new("/").generality b = Pyrosoma::Pattern.new("/foo").generality c = Pyrosoma::Pattern.new("/99").generality d = Pyrosoma::Pattern.new("/$").generality e = Pyrosoma::Pattern.new("/**").generality a.should.be < b a.should.be < c a.should.be < d a.should.be < e b.should.be > c b.should.be < d b.should.be < e c.should.be < d c.should.be < e d.should.be < e end end