Skip to content

Commit

Permalink
fix bug: correct parent of match on sub-subroute
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwarden authored and vti committed Jan 3, 2016
1 parent 33a0701 commit e7e6b72
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/Routes/Tiny/Match.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sub new {
$self->{name} = $params{name};
$self->{arguments} = $params{arguments};
$self->{captures} = $params{captures};
$self->{parent} = undef;
$self->{parent} = $params{parent};

return $self;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Routes/Tiny/Pattern.pm
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ sub match {
my $match = $self->_build_match(
name => $self->name,
arguments => $self->arguments,
captures => $captures
captures => $captures,
parent => $args{parent}
);

if ($self->{subroutes}) {
my $parent = $match;
my $tail = substr($path, length $&);
$match = $self->{subroutes}->match($tail, %args);
$match->{parent} = $parent if $match;
$match = $self->{subroutes}->match($tail, %args, parent => $parent);
}

return $match;
Expand Down
20 changes: 20 additions & 0 deletions t/subroutes.t
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,24 @@ subtest 'pass params to subroutes' => sub {
ok $ro->match('/subroute/foo', method => 'POST');
};

subtest 'sub-subroutes' => sub {
my $r0 = Routes::Tiny->new;
$r0->mount('/toplevel/:topic', $ro);

my $match = $r0->match('/toplevel/rainbows/r3/5/bar/7/');

ok($match);
if($match) {
my $parent = $match->parent;
ok($parent);
is($parent->captures->{parent_id}, 5);
if($parent) {
my $grandparent = $parent->parent;
ok($grandparent);
is($grandparent->captures->{topic}, 'rainbows');
}
}
};


done_testing;

0 comments on commit e7e6b72

Please sign in to comment.