Changeset 26957

Show
Ignore:
Timestamp:
12/17/08 22:57:05 (4 years ago)
Author:
masaki
Message:

tests for nested routes

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTP-Router/branches/merb-like/t/20_build_routes.t

    r26951 r26957  
    22use HTTP::Router; 
    33 
    4 plan tests => 2; 
    5  
    6 filters { 
    7     map { $_ => ['eval'] } qw(params conditions) 
    8 }; 
     4plan tests => 8; 
    95 
    106my $router = HTTP::Router->new; 
    11 $router->define(sub { 
    12     while (my $block = next_block()) { 
    13         $_->match($block->path, $block->conditions)->to($block->params); 
    14     } 
     7 
     8# simple 
     9$router->reset->define(sub { 
     10    $_->match('/')->to(); 
     11    $_->match('/home', { method => 'GET' })->to(); 
     12    $_->match('/archives/{year}', { year => qr/^\d{4}$/ })->to(); 
     13    $_->match('/users/{user_id}', { user_id => qr/^\d+$/, method => 'GET' })->to(); 
    1514}); 
     15is scalar @{[ $router->routes ]} => 4; 
     16ok grep { $_->path eq '/' }                $router->routes; 
     17ok grep { $_->path eq '/home' }            $router->routes; 
     18ok grep { $_->path eq '/archives/{year}' } $router->routes; 
     19ok grep { $_->path eq '/users/{user_id}' } $router->routes; 
    1620 
    17 my @routes = $router->routes; 
    18 is scalar(@routes) => blocks(), 'number'; 
    19  
    20 my %params = map { $_->path, $_->params } @routes; 
    21 is_deeply \%params 
    22     => { map { $_->path, $_->params } blocks() }, 'path, params'; 
    23  
    24 __END__ 
    25 === path 
    26 --- path  : / 
    27 --- params: { controller => 'Root', action => 'index' } 
    28  
    29 === path conditions 
    30 --- path      : /account/login 
    31 --- params    : { controller => 'Account', action => 'login' } 
    32 --- conditions: { method => ['GET', 'POST'] } 
    33  
    34 === captures 
    35 --- path  : /archives/{year} 
    36 --- params: { controller => 'Archive', action => 'by_year' } 
    37  
    38 === captures with validation 
    39 --- path      : /archives/{year}/{month} 
    40 --- params    : { controller => 'Archive', action => 'by_month' } 
    41 --- conditions: { year => qr/^\d{4}$/, month => qr/^\d{1,2}$/ } 
    42  
    43 === captures with conditions 
    44 --- path      : /users/{user_id} 
    45 --- params    : { controller => 'User', action => 'show' } 
    46 --- conditions: { user_id => qr/^\d+$/, method => 'GET' } 
     21# nest 
     22$router->reset->define(sub { 
     23    $_->match('/account', sub { 
     24        $_->match('/login')->to; 
     25        $_->match('/logout')->to; 
     26    }); 
     27}); 
     28is scalar @{[ $router->routes ]} => 2; 
     29ok grep { $_->path eq '/account/login' }  $router->routes; 
     30ok grep { $_->path eq '/account/logout' } $router->routes;