Skip to content

Commit

Permalink
added more pattern tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 6, 2013
1 parent 40ee1ed commit ea681e5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

4.12 2013-06-06
4.12 2013-06-07
- Improved Mojo::Message::Request to allow a few more ASCII characters in
URLs.

Expand Down
5 changes: 1 addition & 4 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -189,9 +189,7 @@ sub _tokenize {
my $pattern = $self->pattern;
my $state = 'text';
my (@tree, $quoted);
while (length(my $char = substr $pattern, 0, 1, '')) {

# Inside a placeholder
for my $char (split '', $pattern) {
my $inside = !!grep { $_ eq $state } qw(placeholder relaxed wildcard);

# Quote start
Expand Down Expand Up @@ -235,7 +233,6 @@ sub _tokenize {
# New text element
push @tree, ['text', $char] and next unless $tree[-1][0] eq 'text';

# More text
$tree[-1][-1] .= $char;
}
}
Expand Down
31 changes: 31 additions & 0 deletions t/mojolicious/pattern.t
Expand Up @@ -166,4 +166,35 @@ is $pattern->render($result), '/foo/v1.0', 'right result';
is $pattern->render($result, 1), '/foo/v1.0.txt', 'right result';
ok !$pattern->match('/foo/v2.0', 1), 'no result';

# Special placeholder names
$pattern = Mojolicious::Routes::Pattern->new('/:');
$result = $pattern->match('/foo', 1);
is_deeply $result, {'' => 'foo'}, 'right structure';
is $pattern->render($result, 1), '/foo', 'right result';
is $pattern->render({'' => 'bar'}, 1), '/bar', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('/#');
$result = $pattern->match('/foo.bar', 1);
is_deeply $result, {'' => 'foo.bar'}, 'right structure';
is $pattern->render($result, 1), '/foo.bar', 'right result';
is $pattern->render({'' => 'bar.baz'}, 1), '/bar.baz', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('/*');
$result = $pattern->match('/foo/bar', 1);
is_deeply $result, {'' => 'foo/bar'}, 'right structure';
is $pattern->render($result, 1), '/foo/bar', 'right result';
is $pattern->render({'' => 'bar/baz'}, 1), '/bar/baz', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('/:/:0');
$result = $pattern->match('/foo/bar', 1);
is_deeply $result, {'' => 'foo', '0' => 'bar'}, 'right structure';
is $pattern->render($result, 1), '/foo/bar', 'right result';
is $pattern->render({'' => 'bar', '0' => 'baz'}, 1), '/bar/baz',
'right result';
$pattern = Mojolicious::Routes::Pattern->new('/(:)test/(0)');
$result = $pattern->match('/footest/bar', 1);
is_deeply $result, {'' => 'foo', '0' => 'bar'}, 'right structure';
is $pattern->render($result, 1), '/footest/bar', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('/()test');
$result = $pattern->match('/footest', 1);
is_deeply $result, {'' => 'foo'}, 'right structure';
is $pattern->render($result, 1), '/footest', 'right result';

done_testing();
20 changes: 20 additions & 0 deletions t/mojolicious/routes.t
Expand Up @@ -193,6 +193,10 @@ is $second->render('', {}), '/second', 'right result';
$target->add_child($first)->add_child($second);
is $second->render('', {}), '/target/second', 'right result';

# /zero/*/name
$r->route('/zero/:/name')->to('zero#placeholder');
$r->route('/zero/*/name')->to('zero#wildcard');

# Cached lookup
my $fast = $r->route('/fast');
is $r->find('fast'), $fast, 'fast route found';
Expand Down Expand Up @@ -810,4 +814,20 @@ $m = Mojolicious::Routes::Match->new(root => $r);
$m->match($c => {method => 'GET', path => '/target/third'});
is_deeply $m->stack, [], 'empty stack';

# Nameless placeholder
$m = Mojolicious::Routes::Match->new(root => $r);
$m->match($c => {method => 'GET', path => '/zero/foo/name'});
is_deeply $m->stack,
[{controller => 'zero', action => 'placeholder', '' => 'foo'}],
'right structure';
is $m->path_for, '/zero/foo/name', 'right path';
is $m->path_for('' => 'bar'), '/zero/bar/name', 'right path';
$m = Mojolicious::Routes::Match->new(root => $r);
$m->match($c => {method => 'GET', path => '/zero/foo/bar/name'});
is_deeply $m->stack,
[{controller => 'zero', action => 'wildcard', '' => 'foo/bar'}],
'right structure';
is $m->path_for, '/zero/foo/bar/name', 'right path';
is $m->path_for('' => 'bar/baz'), '/zero/bar/baz/name', 'right path';

done_testing();

0 comments on commit ea681e5

Please sign in to comment.