Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
a few more placeholder tests
  • Loading branch information
kraih committed Jan 21, 2014
1 parent 7371c5c commit 76cb552
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -102,51 +102,52 @@ sub render {
sub _compile {
my $self = shift;

my $placeholders = $self->placeholders;
my $constraints = $self->constraints;
my $defaults = $self->defaults;

my $block = my $regex = '';
my $optional = 1;
my $constraints = $self->constraints;
my $defaults = $self->defaults;
my $optional = 1;
for my $token (reverse @{$self->tree}) {
my $op = $token->[0];
my $compiled = '';
my $fragment = '';

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

# Text
elsif ($op eq 'text') {
$compiled = quotemeta $token->[1];
$fragment = quotemeta $token->[1];
$optional = 0;
}

# Placeholder
elsif ($op eq 'placeholder' || $op eq 'relaxed' || $op eq 'wildcard') {
my $name = $token->[1];
unshift @{$self->placeholders}, $name;
unshift @$placeholders, my $name = $token->[1];

# Placeholder
if ($op eq 'placeholder') { $compiled = '([^\/\.]+)' }
if ($op eq 'placeholder') { $fragment = '([^\/\.]+)' }

# Relaxed
elsif ($op eq 'relaxed') { $compiled = '([^\/]+)' }
elsif ($op eq 'relaxed') { $fragment = '([^\/]+)' }

# Wildcard
elsif ($op eq 'wildcard') { $compiled = '(.+)' }
elsif ($op eq 'wildcard') { $fragment = '(.+)' }

# Custom regex
my $constraint = $constraints->{$name};
$compiled = _compile_req($constraint) if $constraint;
$fragment = _compile_req($constraint) if $constraint;

# Optional placeholder
exists $defaults->{$name} ? ($compiled .= '?') : ($optional = 0);
exists $defaults->{$name} ? ($fragment .= '?') : ($optional = 0);
}

$block = "$compiled$block";
$block = "$fragment$block";
}

# Not rooted with a slash
Expand Down Expand Up @@ -186,10 +187,9 @@ sub _tokenize {
my $relaxed = $self->relaxed_start;
my $wildcard = $self->wildcard_start;

my $pattern = $self->pattern;
my $state = 'text';
my $state = 'text';
my (@tree, $quoted);
for my $char (split '', $pattern) {
for my $char (split '', $self->pattern) {
my $inside = !!grep { $_ eq $state } qw(placeholder relaxed wildcard);

# Quote start
Expand Down
17 changes: 17 additions & 0 deletions t/mojolicious/pattern.t
Expand Up @@ -22,6 +22,7 @@ $pattern->defaults({name => 'foo'});
is_deeply $pattern->match('/test123', 1), {name => 'foo'}, 'right structure';
is_deeply $pattern->match('/testbar123', 1), {name => 'bar'},
'right structure';
ok !$pattern->match('/test/123'), 'no result';
is $pattern->render, '/testfoo123', 'right result';
is $pattern->render({name => 'bar'}), '/testbar123', 'right result';
$pattern->defaults({name => ''});
Expand All @@ -36,6 +37,22 @@ ok !$pattern->match('/test'), 'no result';
is $pattern->render, '/test/foo/123', 'right result';
is $pattern->render({name => 'bar'}), '/test/bar/123', 'right result';

# Multiple optional placeholders in the middle
$pattern = Mojolicious::Routes::Pattern->new('/test/:a/123/:b/456');
$pattern->defaults({a => 'a', b => 'b'});
is_deeply $pattern->match('/test/123/456', 1), {a => 'a', b => 'b'},
'right structure';
is_deeply $pattern->match('/test/c/123/456', 1), {a => 'c', b => 'b'},
'right structure';
is_deeply $pattern->match('/test/123/c/456', 1), {a => 'a', b => 'c'},
'right structure';
is_deeply $pattern->match('/test/c/123/d/456', 1), {a => 'c', b => 'd'},
'right structure';
is $pattern->render, '/test/a/123/b/456', 'right result';
is $pattern->render({a => 'c'}), '/test/c/123/b/456', 'right result';
is $pattern->render({b => 'c'}), '/test/a/123/c/456', 'right result';
is $pattern->render({a => 'c', b => 'd'}), '/test/c/123/d/456', 'right result';

# Root
$pattern = Mojolicious::Routes::Pattern->new('/');
$pattern->defaults({action => 'index'});
Expand Down

0 comments on commit 76cb552

Please sign in to comment.