Skip to content

Commit

Permalink
more route normalization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 15, 2014
1 parent b131e30 commit 573bf89
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

4.81 2014-02-14
4.81 2014-02-15
- Added direct array access for child nodes to Mojo::DOM.
- Improved Mojolicious::Routes::Pattern to normalize more route variations.
- Improved routes command to show which routes are using certain features
Expand Down
9 changes: 4 additions & 5 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -50,13 +50,12 @@ sub new { @_ > 1 ? shift->SUPER::new->parse(@_) : shift->SUPER::new }
sub parse {
my $self = shift;

# Normalize pattern
my $pattern = @_ % 2 ? (shift || '/') : '/';
$pattern =~ s!/+!/!g;
$pattern ne '/' ? $self->constraints({@_}) : return $self->constraints({@_});
my $pattern = @_ % 2 ? (shift // '/') : '/';
$pattern =~ s!^/*|/+!/!g;
return $self->constraints({@_}) if $pattern eq '/';
$pattern =~ s!/$!!;

return $self->_tokenize($pattern =~ m!^/! ? $pattern : "/$pattern");
return $self->constraints({@_})->_tokenize($pattern);
}

sub render {
Expand Down
8 changes: 8 additions & 0 deletions t/mojolicious/pattern.t
Expand Up @@ -243,6 +243,14 @@ $pattern = Mojolicious::Routes::Pattern->new('//:foo//bar//');
$result = $pattern->match('/foo/bar', 1);
is_deeply $result, {'foo' => 'foo'}, 'right structure';
is $pattern->render($result, 1), '/foo/bar', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('//');
$result = $pattern->match('/', 1);
is_deeply $result, {}, 'right structure';
is $pattern->render($result, 1), '/', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('0');
$result = $pattern->match('/0', 1);
is_deeply $result, {}, 'right structure';
is $pattern->render($result, 1), '/0', 'right result';

# Unicode
$pattern = Mojolicious::Routes::Pattern->new('/(one)♥(two)');
Expand Down

0 comments on commit 573bf89

Please sign in to comment.