2012-03-22 19:52:13 -04:00
|
|
|
# I like coding: UTF-8
|
|
|
|
require File.expand_path('../helper', __FILE__)
|
|
|
|
|
|
|
|
class CompileTest < Test::Unit::TestCase
|
2012-06-17 08:23:26 -04:00
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
def self.converts pattern, expected_regexp
|
|
|
|
it "generates #{expected_regexp.source} from #{pattern}" do
|
|
|
|
compiled, _ = compiled pattern
|
2012-06-18 01:10:40 -04:00
|
|
|
assert_equal expected_regexp, compiled
|
2012-06-18 00:56:22 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-17 08:23:26 -04:00
|
|
|
def self.parses pattern, example, expected_params
|
|
|
|
it "parses #{example} with #{pattern} into params #{expected_params}" do
|
2012-06-18 00:56:22 -04:00
|
|
|
compiled, keys = compiled pattern
|
2012-06-17 08:23:26 -04:00
|
|
|
match = compiled.match(example)
|
|
|
|
fail %Q{"#{example}" does not parse on pattern "#{pattern}".} unless match
|
|
|
|
params = Hash[keys.zip(match.captures)]
|
|
|
|
assert_equal(expected_params, params)
|
2012-03-23 00:14:18 -04:00
|
|
|
end
|
2012-06-17 08:23:26 -04:00
|
|
|
end
|
|
|
|
def self.fails pattern, example
|
|
|
|
it "does not parse #{example} with #{pattern}" do
|
2012-06-18 00:56:22 -04:00
|
|
|
compiled, _ = compiled pattern
|
2012-06-17 08:23:26 -04:00
|
|
|
match = compiled.match(example)
|
|
|
|
fail unless match.nil? || match.captures.empty?
|
2012-03-22 19:52:13 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-18 00:56:22 -04:00
|
|
|
def compiled pattern
|
|
|
|
app ||= mock_app {}
|
|
|
|
compiled, keys = app.send(:compile, pattern)
|
|
|
|
[compiled, keys]
|
|
|
|
end
|
2012-06-17 08:23:26 -04:00
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/", %r{\A/\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/", "/", {}
|
2012-06-18 00:56:22 -04:00
|
|
|
|
|
|
|
converts "/foo", %r{\A/foo\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/foo", "/foo", {}
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:foo", %r{\A/([^/?#]+)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:foo", "/foo", "foo" => "foo"
|
|
|
|
parses "/:foo", "/foo.bar", "foo" => "foo.bar"
|
|
|
|
parses "/:foo", "/foo%2Fbar", "foo" => "foo%2Fbar"
|
|
|
|
fails "/:foo", "/foo?"
|
|
|
|
fails "/:foo", "/foo/bar"
|
|
|
|
fails "/:foo", "/"
|
|
|
|
fails "/:foo", "/foo/"
|
|
|
|
|
2012-06-18 01:10:40 -04:00
|
|
|
# converts "/f\u00F6\u00F6", %r{\A/f%C3%B6%C3%B6\z} # TODO Fails in Ruby 1.8
|
|
|
|
fails "/f\u00F6\u00F6", "/f%C3%B6%C3%B6"
|
2012-06-17 08:23:26 -04:00
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:foo/:bar", %r{\A/([^/?#]+)/([^/?#]+)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:foo/:bar", "/foo/bar", "foo" => "foo", "bar" => "bar"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/hello/:person", %r{\A/hello/([^/?#]+)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/hello/:person", "/hello/Frank", "person" => "Frank"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/?:foo?/?:bar?", %r{\A/?([^/?#]+)?/?([^/?#]+)?\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/?:foo?/?:bar?", "/hello/world", "foo" => "hello", "bar" => "world"
|
|
|
|
parses "/?:foo?/?:bar?", "/hello", "foo" => "hello", "bar" => nil
|
|
|
|
parses "/?:foo?/?:bar?", "/", "foo" => nil, "bar" => nil
|
|
|
|
parses "/?:foo?/?:bar?", "", "foo" => nil, "bar" => nil
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/*", %r{\A/(.*?)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/*", "/", "splat" => ""
|
|
|
|
parses "/*", "/foo", "splat" => "foo"
|
|
|
|
parses "/*", "/foo/bar", "splat" => "foo/bar"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:foo/*", %r{\A/([^/?#]+)/(.*?)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:foo/*", "/foo/bar/baz", "foo" => "foo", "splat" => "bar/baz"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:foo/:bar", %r{\A/([^/?#]+)/([^/?#]+)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:foo/:bar", "/user@example.com/name", "foo" => "user@example.com", "bar" => "name"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/test$/", %r{\A/test(?:\$|%24)/\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
fails "/test$/", "/test$/"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/te+st/", %r{\A/te(?:\+|%2B)st/\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/te+st/", "/te+st/", {}
|
|
|
|
fails "/te+st/", "/test/"
|
|
|
|
fails "/te+st/", "/teeest/"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/test(bar)/", %r{\A/test(?:\(|%28)bar(?:\)|%29)/\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/test(bar)/", "/test(bar)/", {}
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/path with spaces", %r{\A/path(?:%20|(?:\+|%2B))with(?:%20|(?:\+|%2B))spaces\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/path with spaces", "/path%20with%20spaces", {}
|
|
|
|
parses "/path with spaces", "/path%2Bwith%2Bspaces", {}
|
|
|
|
parses "/path with spaces", "/path+with+spaces", {}
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/foo&bar", %r{\A/foo(?:&|%26)bar\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/foo&bar", "/foo&bar", {}
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:foo/*", %r{\A/([^/?#]+)/(.*?)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:foo/*", "/hello%20world/how%20are%20you", "foo" => "hello%20world", "splat" => "how%20are%20you"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/*/foo/*/*", %r{\A/(.*?)/foo/(.*?)/(.*?)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/*/foo/*/*", "/bar/foo/bling/baz/boom", "splat" => "baz/boom" # TODO
|
|
|
|
fails "/*/foo/*/*", "/bar/foo/baz"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/test.bar", %r{\A/test(?:\.|%2E)bar\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/test.bar", "/test.bar", {}
|
|
|
|
fails "/test.bar", "/test0bar"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:file.:ext", %r{\A/([^\.%2E/?#]+)(?:\.|%2E)([^\.%2E/?#]+)\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:file.:ext", "/pony.jpg", "file" => "pony", "ext" => "jpg"
|
|
|
|
parses "/:file.:ext", "/pony%2Ejpg", "file" => "pony", "ext" => "jpg"
|
|
|
|
fails "/:file.:ext", "/.jpg"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:name.?:format?", %r{\A/([^\.%2E/?#]+)(?:\.|%2E)?([^\.%2E/?#]+)?\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:name.?:format?", "/foo", "name" => "foo", "format" => nil
|
|
|
|
parses "/:name.?:format?", "/foo.bar", "name" => "foo", "format" => "bar"
|
|
|
|
parses "/:name.?:format?", "/foo%2Ebar", "name" => "foo", "format" => "bar"
|
|
|
|
fails "/:name.?:format?", "/.bar"
|
|
|
|
|
2012-06-18 00:56:22 -04:00
|
|
|
converts "/:user@?:host?", %r{\A/([^@%40/?#]+)(?:@|%40)?([^@%40/?#]+)?\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
parses "/:user@?:host?", "/foo@bar", "user" => "foo", "host" => "bar"
|
|
|
|
parses "/:user@?:host?", "/foo.foo@bar", "user" => "foo.foo", "host" => "bar"
|
|
|
|
parses "/:user@?:host?", "/foo@bar.bar", "user" => "foo", "host" => "bar.bar"
|
|
|
|
|
|
|
|
# From https://gist.github.com/2154980#gistcomment-169469.
|
|
|
|
#
|
2012-06-18 00:56:22 -04:00
|
|
|
# converts "/:name(.:format)?", %r{\A/([^\.%2E/?#]+)(?:\(|%28)(?:\.|%2E)([^\.%2E/?#]+)(?:\)|%29)?\z}
|
2012-06-17 08:23:26 -04:00
|
|
|
# parses "/:name(.:format)?", "/foo", "name" => "foo", "format" => nil
|
|
|
|
# parses "/:name(.:format)?", "/foo.bar", "name" => "foo", "format" => "bar"
|
2012-03-22 19:52:13 -04:00
|
|
|
end
|