Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed optional slash problem
  • Loading branch information
kraih committed Jan 21, 2014
1 parent 9cbbf13 commit 279aea0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/Mojolicious/Guides/Routing.pod
Expand Up @@ -356,9 +356,9 @@ there is already a stash value of the same name present.
$r->route('/:mymessage')
->to(controller => 'foo', action => 'bar', mymessage => 'hi');

# /test123 -> {controller => 'foo', action => 'bar', mymessage => 'hi'}
# /testbye123 -> {controller => 'foo', action => 'bar', mymessage => 'bye'}
$r->route('/test(:mymessage)123')
# /test/123 -> {controller => 'foo', action => 'bar', mymessage => 'hi'}
# /test/bye/123 -> {controller => 'foo', action => 'bar', mymessage => 'bye'}
$r->route('/test/:mymessage/123')
->to(controller => 'foo', action => 'bar', mymessage => 'hi');

This is also the case if multiple placeholders are right after another and not
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -112,8 +112,9 @@ sub _compile {

# Slash
if ($op eq 'slash') {
$regex = ($optional ? "(?:/$block)?" : "/$block") . $regex;
$block = '';
$regex = ($optional ? "(?:/$block)?" : "/$block") . $regex;
$optional = 1;
$block = '';
next;
}

Expand Down
12 changes: 6 additions & 6 deletions t/mojolicious/lite_app.t
Expand Up @@ -60,8 +60,8 @@ get '/alternatives/:char' => [char => [qw(☃ ♥)]] => sub {
$self->render(text => $self->url_for);
};

get '/an(:optional)placeholder' =>
{optional => 'none', inline => '<%= $optional %>-<%= url_for =%>'};
get '/optional/:middle/placeholder' =>
{middle => 'none', inline => '<%= $middle %>-<%= url_for =%>'};

get '/alterformat' => [format => ['json']] => {format => 'json'} => sub {
my $self = shift;
Expand Down Expand Up @@ -523,14 +523,14 @@ $t->get_ok('/alternatives/test')->status_is(404)
->header_is(Server => 'Mojolicious (Perl)')->content_is("Oops!\n");

# Optional placeholder in the middle
$t->get_ok('/anoptionalplaceholder')->status_is(200)
$t->get_ok('/optional/test/placeholder')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is('optional-/anoptionalplaceholder');
->content_is('test-/optional/test/placeholder');

# Optional placeholder in the middle without value
$t->get_ok('/anplaceholder')->status_is(200)
$t->get_ok('/optional/placeholder')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is('none-/annoneplaceholder');
->content_is('none-/optional/none/placeholder');

# No format
$t->get_ok('/alterformat')->status_is(200)
Expand Down
7 changes: 7 additions & 0 deletions t/mojolicious/pattern.t
Expand Up @@ -27,6 +27,13 @@ is $pattern->render({name => 'bar'}), '/testbar123', 'right result';
$pattern->defaults({name => ''});
is_deeply $pattern->match('/test123', 1), {name => ''}, 'right structure';
is $pattern->render, '/test123', 'right result';
$pattern = Mojolicious::Routes::Pattern->new('/test/:name/123');
$pattern->defaults({name => 'foo'});
is_deeply $pattern->match('/test/123', 1), {name => 'foo'}, 'right structure';
is_deeply $pattern->match('/test/bar/123', 1), {name => 'bar'},
'right structure';
is $pattern->render, '/test/foo/123', 'right result';
is $pattern->render({name => 'bar'}), '/test/bar/123', 'right result';

# Root
$pattern = Mojolicious::Routes::Pattern->new('/');
Expand Down

0 comments on commit 279aea0

Please sign in to comment.