Skip to content

Instantly share code, notes, and snippets.

@rkh
Created March 22, 2012 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkh/0c9b9e2521e0833a67b2 to your computer and use it in GitHub Desktop.
Save rkh/0c9b9e2521e0833a67b2 to your computer and use it in GitHub Desktop.
# coding: utf-8
require 'sinatra'
disable :run
$working = []
$broken = []
$titles = ['Pattern', 'Current Regexp', 'Example', 'Should Be', 'Is Currently']
def self.should(route, match, *args)
compiled = get(route) { }.first
results = match.match(compiled).to_a[1..-1]
args = args.first if args == [nil] or Array == args.first
list = results == args ? $working : $broken
list << [route, compiled, match, args, results].map(&:inspect)
end
def self.display(list)
display_line = proc do |row|
cells = row.zip($widths).map { |cell, width| cell.ljust(width) }
puts " " + cells.join(" | ")
end
display_line[$titles]
puts '-' + $widths.map { |w| "-"*w }.join("-|-") + '-'
list.each(&display_line)
end
should '/', '/'
should '/foo', '/foo'
should '/:foo', '/foo', 'foo'
should '/:foo', '/foo?', nil
should '/:foo', '/foo/bar', nil
should '/:foo', '/foo%2Fbar', 'foo%2Fbar'
should '/:foo/:bar', '/foo/bar', 'foo', 'bar'
should '/:foo', '/', nil
should '/:foo', '/foo/', nil
should '/föö', '/f%C3%B6%C3%B6'
should '/hello/:person', '/hello/Frank', 'Frank'
should '/?:foo?/?:bar?', '/hello/world', 'hello', 'world'
should '/?:foo?/?:bar?', '/hello', 'hello', nil
should '/?:foo?/?:bar?', '/', nil, nil
should '/?:foo?/?:bar?', '', nil, nil
should '/*', '/', ''
should '/*', '/foo', 'foo'
should '/*', '/', ''
should '/*', '/foo/bar', 'foo/bar'
should '/:foo/*', '/foo/bar/baz', "foo", "bar/baz"
should '/:foo/:bar', '/user@example.com/name', 'user@example.com', 'name'
should '/:file.:ext', '/pony.jpg', 'pony', 'jpg'
should '/:file.:ext', '/pony%2Ejpg', 'pony', 'jpg'
should '/:file.:ext', '/.jpg', nil
should '/test.bar', '/test.bar'
should '/test.bar', '/test0bar', nil
should '/test$/', '/test$/'
should '/te+st/', '/te+st/'
should '/te+st/', '/test/', nil
should '/te+st/', '/teeest/', nil
should '/test(bar)/', '/test(bar)/'
should '/path with spaces', '/path%20with%20spaces'
should '/path with spaces', '/path%2Bwith%2Bspaces'
should '/path with spaces', '/path+with+spaces'
should '/foo&bar', '/foo&bar'
should '/:foo/*', '/hello%20world/how%20are%20you', "hello%20world", "how%20are%20you"
should '/*/foo/*/*', '/bar/foo/bling/baz/boom', "bar", "bling", "baz/boom"
should '/*/foo/*/*', '/bar/foo/baz', nil
should '/:name.?:format?', '/foo', 'foo', nil
should '/:name.?:format?', '/foo.bar', 'foo', 'bar'
should '/:name.?:format?', '/foo%2Ebar', 'foo', 'bar'
should '/:name.?:format?', '/.bar', '.bar', nil
should '/:user@?:host?', '/foo@bar', 'foo', 'bar'
should '/:user@?:host?', '/foo.foo@bar', 'foo.foo', 'bar'
should '/:user@?:host?', '/foo@bar.bar', 'foo', 'bar.bar'
$all = [$titles] + $working + $broken
$widths = $all.inject([0] * $all.first.size) { |s,e| s.zip(e).map { |c,n| [c,n.size].max }}
puts "", "Working", "=======", ""
display $working
puts "", "Broken", "======", ""
display $broken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment