Skip to content

Commit

Permalink
fixed small format rendering bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 15, 2012
1 parent bf9f24b commit 86d3732
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -34,10 +34,10 @@ sub parse {
}

sub render {
my ($self, $values, $format) = @_;
$values ||= {};
my ($self, $values, $render) = @_;

# Merge values with defaults
my $format = ($values ||= {})->{format};
$values = {%{$self->defaults}, %$values};

# Turn pattern into path
Expand Down Expand Up @@ -70,7 +70,7 @@ sub render {

# Format is optional
$string ||= '/';
return $format && $values->{format} ? "$string.$values->{format}" : $string;
return $render && $format ? "$string.$format" : $string;
}

sub shape_match {
Expand Down
16 changes: 9 additions & 7 deletions t/mojolicious/pattern.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 89;
use Test::More tests => 90;

# "People said I was dumb, but I proved them."
use Mojo::ByteStream 'b';
Expand Down Expand Up @@ -142,7 +142,7 @@ $result = $pattern->match('/test.json');
ok $pattern->regex, 'regex has been compiled on demand';
ok !$pattern->format, 'no format regex';
is $result->{action}, 'index', 'right value';
ok !$result->{format}, 'no value';
is $result->{format}, undef, 'no value';
$result = $pattern->match('/test.json', 1);
is $result->{action}, 'index', 'right value';
is $result->{format}, undef, 'no value';
Expand All @@ -168,7 +168,7 @@ $pattern = Mojolicious::Routes::Pattern->new('/test', format => 0);
$pattern->defaults({action => 'index'});
$result = $pattern->match('/test', 1);
is $result->{action}, 'index', 'right value';
ok !$result->{format}, 'no value';
is $result->{format}, undef, 'no value';
$result = $pattern->match('/test.xml', 1);
is $result, undef, 'no result';

Expand All @@ -178,19 +178,21 @@ is $pattern->reqs->{format}, 0, 'right value';
$pattern->defaults({action => 'index'});
$result = $pattern->match('/', 1);
is $result->{action}, 'index', 'right value';
ok !$result->{format}, 'no value';
is $result->{format}, undef, 'no value';
$result = $pattern->match('/.xml', 1);
is $result, undef, 'no result';

# Versioned pattern
$pattern = Mojolicious::Routes::Pattern->new('/:test/v1.0');
$pattern->defaults({action => 'index'});
$pattern->defaults({action => 'index', format => 'html'});
$result = $pattern->match('/foo/v1.0', 1);
is $result->{test}, 'foo', 'right value';
is $result->{action}, 'index', 'right value';
ok !$result->{format}, 'no value';
is $result->{format}, 'html', 'right value';
is $pattern->render($result), '/foo/v1.0', 'right result';
is $pattern->render($result, 1), '/foo/v1.0', 'right result';
is $pattern->render($result, 1), '/foo/v1.0.html', 'right result';
is $pattern->render({%$result, format => undef}, 1), '/foo/v1.0',
'right result';
$result = $pattern->match('/foo/v1.0.txt', 1);
is $result->{test}, 'foo', 'right value';
is $result->{action}, 'index', 'right value';
Expand Down

0 comments on commit 86d3732

Please sign in to comment.