Created
February 23, 2010 07:27
-
-
Save masaki/311966 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -ruN router-anymoose/.shipit router-caf/.shipit | |
--- router-anymoose/.shipit 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/.shipit 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,7 +1,3 @@ | |
-steps = FindVersion, ChangeVersion, CheckChangeLog, Manifest, DistTest, Commit, Tag, MakeDist, UploadCPAN, Twitter, DistClean | |
- | |
+steps = FindVersion, ChangeVersion, CheckChangeLog, Manifest, DistTest, Commit, Tag, MakeDist, UploadCPAN, DistClean | |
git.tagpattern = %v | |
git.push_to = origin | |
- | |
-twitter.config = ~/.pit/twitter.yaml | |
- | |
diff -ruN router-anymoose/Changes router-caf/Changes | |
--- router-anymoose/Changes 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/Changes 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,12 +1,4 @@ | |
Revision history for Perl extension HTTP::Router | |
-0.03 | |
- - fixed dependencies | |
- | |
-0.02 | |
- - switched from Mouse to Any::Moose | |
- - moved DSL route definitions to HTTP::Router::Declare | |
- - added Test::HTTP::Router | |
- | |
0.01 | |
- original version | |
diff -ruN router-anymoose/Makefile.PL router-caf/Makefile.PL | |
--- router-anymoose/Makefile.PL 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/Makefile.PL 2010-02-23 16:15:20.000000000 +0900 | |
@@ -2,22 +2,13 @@ | |
name 'HTTP-Router'; | |
all_from 'lib/HTTP/Router.pm'; | |
-requires 'Any::Moose' => '0.10'; | |
-requires_any_moose prefer => 'Mouse', mouse => '0.21'; | |
-requires_any_moose 'X::AttributeHelpers', prefer => 'Mouse', mouse => '0.03'; | |
- | |
-# Core | |
-requires 'Hash::AsObject'; | |
+requires 'Class::Accessor::Fast'; | |
+requires 'Hash::Merge'; | |
requires 'List::MoreUtils'; | |
-requires 'URI::Template::Restrict' => '0.03'; | |
requires 'Scalar::Util' => '1.14'; | |
-# Declare | |
-requires 'Carp'; | |
-requires 'Storable'; | |
-requires 'Devel::Caller::Perl'; | |
-requires 'String::CamelCase'; | |
requires 'Lingua::EN::Inflect::Number'; | |
-# Debug | |
+requires 'String::CamelCase'; | |
+requires 'URI::Template::Restrict' => '0.03'; | |
requires 'Text::SimpleTable'; | |
tests 't/*.t t/*/*.t'; | |
diff -ruN router-anymoose/lib/HTTP/Router/Debug.pm router-caf/lib/HTTP/Router/Debug.pm | |
--- router-anymoose/lib/HTTP/Router/Debug.pm 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/lib/HTTP/Router/Debug.pm 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,13 +1,25 @@ | |
package HTTP::Router::Debug; | |
-use Any::Moose '::Role'; | |
+use strict; | |
+use warnings; | |
use Text::SimpleTable; | |
-use HTTP::Router; | |
-requires 'routes'; | |
+our @EXPORT = qw(routing_table draw_routing_table); | |
-sub show_table { | |
- my $table = $_[0]->routing_table->draw; | |
+sub import { | |
+ # TODO: into package | |
+ my $into = 'HTTP::Router'; | |
+ eval "require $into; 1" or die $@; | |
+ | |
+ no strict 'refs'; | |
+ no warnings 'redefine'; | |
+ for my $keyword (@EXPORT) { | |
+ *{ $into . '::' . $keyword } = \&{ __PACKAGE__ . '::' . $keyword }; | |
+ } | |
+} | |
+ | |
+sub draw_routing_table { | |
+ my $table = shift->routing_table->draw; | |
print "$table\n"; | |
} | |
@@ -36,10 +48,6 @@ | |
return $table; | |
} | |
-# apply self to HTTP::Router | |
-__PACKAGE__->meta->apply(HTTP::Router->meta); | |
- | |
-no Any::Moose '::Role'; | |
1; | |
=head1 NAME | |
@@ -55,17 +63,13 @@ | |
print $router->routing_table->draw; | |
# or | |
- $router->show_table; | |
+ $router->draw_routing_table; | |
=head1 METHODS | |
=head2 routing_table | |
-Returns a Text::SimpleTable object for routing information. | |
- | |
-=head2 show_table | |
- | |
-Constructs and Prints a table for routing information. | |
+=head2 draw_routing_table | |
=head1 AUTHOR | |
diff -ruN router-anymoose/lib/HTTP/Router/Declare.pm router-caf/lib/HTTP/Router/Declare.pm | |
--- router-anymoose/lib/HTTP/Router/Declare.pm 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/lib/HTTP/Router/Declare.pm 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,310 +0,0 @@ | |
-package HTTP::Router::Declare; | |
- | |
-use strict; | |
-use warnings; | |
-use Carp 'croak'; | |
-use Storable 'dclone'; | |
-use Devel::Caller::Perl 'called_args'; | |
-use String::CamelCase 'decamelize'; | |
-use Lingua::EN::Inflect::Number 'to_S'; | |
-use HTTP::Router; | |
-use HTTP::Router::Route; | |
- | |
-sub import { | |
- my $caller = caller; | |
- | |
- no strict 'refs'; | |
- no warnings 'redefine'; | |
- | |
- *{ $caller . '::router' } = \&routing; | |
- *{ $caller . '::routes' } = \&routing; # alias router | |
- | |
- # lexical bindings | |
- *{ $caller . '::match' } = sub { goto &match }; | |
- *{ $caller . '::with' } = sub { goto &with }; | |
- *{ $caller . '::to' } = sub ($) { goto &to }; | |
- *{ $caller . '::then' } = sub (&) { goto &then }; | |
- # resource(s) | |
- *{ $caller . '::resource' } = sub { goto &resource }; | |
- *{ $caller . '::resources' } = sub { goto &resources }; | |
-} | |
- | |
-sub _stub { | |
- my $name = shift; | |
- return sub { croak "Can't call $name() outside routing block" }; | |
-} | |
- | |
-{ | |
- my @Declarations = qw(match with to then resource resources); | |
- for my $keyword (@Declarations) { | |
- no strict 'refs'; | |
- *$keyword = _stub $keyword; | |
- } | |
-} | |
- | |
-sub routing (&) { | |
- my $block = shift; | |
- my $router = HTTP::Router->new; | |
- | |
- if ($block) { | |
- no warnings 'redefine'; | |
- | |
- local *match = create_match($router); | |
- local *with = create_with($router); | |
- local *to = sub { params => $_[0] }; | |
- local *then = sub { $_[0] }; | |
- | |
- local *resource = create_resource($router); | |
- local *resources = create_resources($router); | |
- | |
- my $root = HTTP::Router::Route->new; | |
- $block->($root); | |
- } | |
- | |
- return $router; | |
-} | |
- | |
-sub _map { | |
- my ($router, $block, %args) = @_; | |
- | |
- my $route = dclone called_args(1)->[0]; | |
- $route->append_path($args{path}) if exists $args{path}; | |
- $route->add_conditions(%{ $args{conditions} }) if exists $args{conditions}; | |
- $route->add_params(%{ $args{params} }) if exists $args{params}; | |
- | |
- return defined $block ? $block->($route) : $router->add_route($route); | |
-} | |
- | |
-sub create_match { | |
- my $router = shift; | |
- return sub { | |
- my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
- my %args = (); | |
- $args{path} = shift unless ref $_[0]; | |
- $args{conditions} = shift if ref $_[0] eq 'HASH'; | |
- _map $router, $block, %args, @_; | |
- }; | |
-} | |
- | |
-sub create_with { | |
- my $router = shift; | |
- return sub { | |
- my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
- _map $router, $block, params => @_; | |
- }; | |
-} | |
- | |
-{ | |
- my $Resource = { | |
- collection => {}, | |
- member => { | |
- create => { method => 'POST', suffix => '', action => 'create' }, | |
- show => { method => 'GET', suffix => '', action => 'show' }, | |
- update => { method => 'PUT', suffix => '', action => 'update' }, | |
- destroy => { method => 'DELETE', suffix => '', action => 'destroy' }, | |
- new => { method => 'GET', suffix => '/new', action => 'post' }, | |
- edit => { method => 'GET', suffix => '/edit', action => 'edit' }, | |
- delete => { method => 'GET', suffix => '/delete', action => 'delete' }, | |
- }, | |
- }; | |
- sub _resource_collection { $Resource->{collection} } | |
- sub _resource_member { $Resource->{member} } | |
- | |
- my $Resources = { | |
- collection => { | |
- index => { method => 'GET', suffix => '', action => 'index' }, | |
- create => { method => 'POST', suffix => '', action => 'create' }, | |
- new => { method => 'GET', suffix => '/new', action => 'post' }, | |
- }, | |
- member => { | |
- show => { method => 'GET', suffix => '', action => 'show' }, | |
- update => { method => 'PUT', suffix => '', action => 'update' }, | |
- destroy => { method => 'DELETE', suffix => '', action => 'destroy' }, | |
- edit => { method => 'GET', suffix => '/edit', action => 'edit' }, | |
- delete => { method => 'GET', suffix => '/delete', action => 'delete' }, | |
- }, | |
- }; | |
- sub _resources_collection { $Resources->{collection} } | |
- sub _resources_member { $Resources->{member} } | |
-} | |
- | |
-sub _map_resources { | |
- my ($router, $args) = @_; | |
- | |
- for my $symbol (qw'collection member') { | |
- while (my ($key, $config) = each %{ $args->{$symbol} }) { | |
- $config = { method => $config } unless ref $config; | |
- | |
- my $action = exists $config->{action} ? $config->{action} : $key; | |
- my $suffix = exists $config->{suffix} ? $config->{suffix} : "/$action"; | |
- my $prefix = $args->{"${symbol}_prefix"}; | |
- | |
- my $path = $prefix . $suffix; | |
- my $conditions = { method => $config->{method} }; | |
- my $params = { controller => $args->{controller}, action => $action }; | |
- | |
- my $formatted_route = HTTP::Router::Route->new( | |
- path => "${path}.{format}", | |
- conditions => $conditions, | |
- params => $params, | |
- ); | |
- $router->add_route($formatted_route); | |
- | |
- my $route = HTTP::Router::Route->new( | |
- path => $path, | |
- conditions => $conditions, | |
- params => $params, | |
- ); | |
- $router->add_route($route); | |
- } | |
- } | |
-} | |
- | |
-sub _create_resources { | |
- my ($router, $name, $block, $args) = @_; | |
- | |
- my %only = map { $_ => 1 } @{ $args->{only} || [] }; | |
- my %except = map { $_ => 1 } @{ $args->{except} || [] }; | |
- | |
- for my $symbol (qw'collection member') { | |
- my $extra = delete $args->{$symbol}; # save extra maps | |
- | |
- no strict 'refs'; | |
- my $default = exists $args->{singleton} ? &{"_resource_$symbol"}() : &{"_resources_$symbol"}(); | |
- | |
- if (exists $args->{only}) { | |
- $args->{$symbol} = { | |
- map { $_ => $default->{$_} } grep { $only{$_} } keys %$default | |
- }; | |
- } | |
- elsif (exists $args->{except}) { | |
- $args->{$symbol} = { | |
- map { $_ => $default->{$_} } grep { !$except{$_} } keys %$default | |
- }; | |
- } | |
- else { | |
- $args->{$symbol} = $default; | |
- } | |
- | |
- $args->{$symbol} = { %{ $args->{$symbol} }, %$extra } if defined $extra; | |
- } | |
- | |
- my $decamelized = decamelize $name; | |
- my $singular = to_S $decamelized; | |
- | |
- $args->{collection_prefix} = called_args(1)->[0]->path . | |
- (exists $args->{path_prefix} ? $args->{path_prefix} : "/$decamelized"); | |
- $args->{member_prefix} = $args->{collection_prefix} . | |
- (exists $args->{singleton} ? '' : "/{${singular}_id}"); | |
- | |
- $args->{controller} ||= $name; | |
- | |
- _map_resources($router, $args); | |
- | |
- if (defined $block) { | |
- my $route = HTTP::Router::Route->new(path => $args->{member_prefix}); | |
- $block->($route); | |
- } | |
-} | |
- | |
-sub create_resource { | |
- my $router = shift; | |
- return sub { | |
- my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
- my $name = shift; | |
- my $args = shift || {}; | |
- $args->{singleton} = 1; | |
- _create_resources $router, $name, $block, $args; | |
- }; | |
-} | |
- | |
-sub create_resources { | |
- my $router = shift; | |
- return sub { | |
- my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
- my $name = shift; | |
- my $args = shift || {}; | |
- _create_resources $router, $name, $block, $args; | |
- }; | |
-} | |
- | |
-1; | |
- | |
-=head1 NAME | |
- | |
-HTTP::Router::Declare | |
- | |
-=head1 SYNOPSIS | |
- | |
- use HTTP::Router::Declare; | |
- | |
- my $router = router { | |
- # path and params | |
- match '/' => to { controller => 'Root', action => 'index' }; | |
- | |
- # path, conditions, and params | |
- match '/home', { method => 'GET' } | |
- => to { controller => 'Home', action => 'show' }; | |
- match '/date/{year}', { year => qr/^\d{4}$/ } | |
- => to { controller => 'Date', action => 'by_year' }; | |
- | |
- # path, params, and nesting | |
- match '/account' => to { controller => 'Account' } => then { | |
- match '/login' => to { action => 'login' }; | |
- match '/logout' => to { action => 'logout' }; | |
- }; | |
- | |
- # path nesting | |
- match '/account' => then { | |
- match '/signup' => to { controller => 'Users', action => 'register' }; | |
- match '/logout' => to { controller => 'Account', action => 'logout' }; | |
- }; | |
- | |
- # conditions nesting | |
- match { method => 'GET' } => then { | |
- match '/search' => to { controller => 'Items', action => 'search' }; | |
- match '/tags' => to { controller => 'Tags', action => 'index' }; | |
- }; | |
- | |
- # params nesting | |
- with { controller => 'Account' } => then { | |
- match '/login' => to { action => 'login' }; | |
- match '/logout' => to { action => 'logout' }; | |
- match '/signup' => to { action => 'signup' }; | |
- }; | |
- | |
- # match only | |
- match '/{controller}/{action}/{id}.{format}'; | |
- match '/{controller}/{action}/{id}'; | |
- }; | |
- | |
-=head1 METHODS | |
- | |
-=head2 router $block | |
- | |
-=head2 match $path?, $conditions? | |
- | |
-=head2 to $params | |
- | |
-=head2 with $params | |
- | |
-=head2 then $block | |
- | |
-=head2 resources $name | |
- | |
-=head2 resource $name | |
- | |
-=head1 AUTHOR | |
- | |
-NAKAGAWA Masaki E<lt>[email protected]<gt> | |
- | |
-=head1 LICENSE | |
- | |
-This library is free software; you can redistribute it and/or modify | |
-it under the same terms as Perl itself. | |
- | |
-=head1 SEE ALSO | |
- | |
-L<HTTP::Router>, L<HTTP::Router::Route> | |
- | |
-=cut | |
diff -ruN router-anymoose/lib/HTTP/Router/Mapper.pm router-caf/lib/HTTP/Router/Mapper.pm | |
--- router-anymoose/lib/HTTP/Router/Mapper.pm 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/lib/HTTP/Router/Mapper.pm 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,182 @@ | |
+package HTTP::Router::Mapper; | |
+ | |
+use strict; | |
+use warnings; | |
+use base 'Class::Accessor::Fast'; | |
+use Carp 'croak'; | |
+use Hash::Merge 'merge'; | |
+use HTTP::Router::Route; | |
+ | |
+__PACKAGE__->mk_ro_accessors('router'); | |
+__PACKAGE__->mk_accessors(qw'path params conditions route'); | |
+ | |
+sub new { | |
+ my ($class, $args) = @_; | |
+ | |
+ $args->{path} ||= ''; | |
+ $args->{params} ||= {}; | |
+ $args->{conditions} ||= {}; | |
+ | |
+ return bless $args, $class; | |
+} | |
+ | |
+sub _clone_mapper { | |
+ my ($self, %params) = @_; | |
+ | |
+ for my $key (qw'path params conditions') { | |
+ $params{$key} = $self->$key unless exists $params{$key}; | |
+ } | |
+ | |
+ my $class = ref $self || $self; | |
+ return $class->new({ %params, router => $self->router }); | |
+} | |
+ | |
+sub _freeze_route { | |
+ my $self = shift; | |
+ | |
+ my $route = HTTP::Router::Route->new({ | |
+ path => $self->path, | |
+ params => $self->params, | |
+ conditions => $self->conditions, | |
+ }); | |
+ | |
+ $self->router->add_route($route); | |
+ $self->route($route); | |
+ | |
+ return $self; | |
+} | |
+ | |
+sub match { | |
+ my $self = shift; | |
+ croak 'route has already been committed' if $self->route; | |
+ | |
+ # TODO: parameterize | |
+ my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
+ my ($path, $conditions) = @_; | |
+ croak '$path or $conditions is required' unless $path or $conditions; | |
+ | |
+ my %extra = ( | |
+ $path ? (path => $self->path . $path) : (), | |
+ $conditions ? (conditions => merge($conditions, $self->conditions)) : (), | |
+ ); | |
+ my $mapper = $self->_clone_mapper(%extra); | |
+ | |
+ if ($block) { | |
+ local $_ = $mapper; | |
+ $block->($_); | |
+ } | |
+ | |
+ return $mapper; | |
+} | |
+ | |
+sub to { | |
+ my $self = shift; | |
+ croak 'route has already been committed' if $self->route; | |
+ | |
+ # TODO: parameterize | |
+ my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
+ my $params = shift; | |
+ croak '$params is required' unless $params; | |
+ | |
+ $self->params(merge($params, $self->params)); | |
+ | |
+ if ($block) { | |
+ local $_ = $self; | |
+ $block->($_); | |
+ } | |
+ else { | |
+ $self->_freeze_route; | |
+ } | |
+ | |
+ return $self; | |
+} | |
+ | |
+sub with { | |
+ my $self = shift; | |
+ croak 'route has already been committed' if $self->route; | |
+ | |
+ # TODO: parameterize | |
+ my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
+ my $params = shift; | |
+ croak '$params and $block are required' unless $params and $block; | |
+ | |
+ local $_ = $self->_clone_mapper(params => merge($params, $self->params)); | |
+ $block->($_); | |
+ | |
+ return $_; | |
+} | |
+ | |
+sub register { | |
+ my $self = shift; | |
+ croak 'route has already been committed' if $self->route; | |
+ return $self->_freeze_route; | |
+} | |
+ | |
+# TODO: not implemented yet | |
+#sub namespace {} | |
+#sub name {} | |
+ | |
+1; | |
+ | |
+=for stopwords params | |
+ | |
+=head1 NAME | |
+ | |
+HTTP::Router::Mapper | |
+ | |
+=head1 SYNOPSIS | |
+ | |
+ my $router = HTTP::Router->define(sub { | |
+ $_->match('/index.{format}') | |
+ ->to({ controller => 'Root', action => 'index' }); | |
+ | |
+ $_->match('/archives/{year}', { year => qr/\d{4}/ }) | |
+ ->to({ controller => 'Archive', action => 'by_month' }); | |
+ | |
+ $_->match('/account/login', { method => ['GET', 'POST'] }) | |
+ ->to({ controller => 'Account', action => 'login' }); | |
+ | |
+ $_->with({ controller => 'Account' }, sub { | |
+ $_->match('/account/signup')->to({ action => 'signup' }); | |
+ $_->match('/account/logout')->to({ action => 'logout' }); | |
+ }); | |
+ | |
+ $_->match('/')->register; | |
+ }); | |
+ | |
+=head1 METHODS | |
+ | |
+=head2 match($path?, $conditions?, $block?) | |
+ | |
+=head2 to($params, $block?) | |
+ | |
+=head2 with($params, $block) | |
+ | |
+=head2 register | |
+ | |
+=head1 PROPERTIES | |
+ | |
+=head2 path | |
+ | |
+=head2 conditions | |
+ | |
+=head2 params | |
+ | |
+=head2 route | |
+ | |
+=head2 router | |
+ | |
+=head1 AUTHOR | |
+ | |
+NAKAGAWA Masaki E<lt>[email protected]<gt> | |
+ | |
+=head1 LICENSE | |
+ | |
+This library is free software; you can redistribute it and/or modify | |
+it under the same terms as Perl itself. | |
+ | |
+=head1 SEE ALSO | |
+ | |
+L<HTTP::Router>, L<HTTP::Router::Route> | |
+ | |
+=cut | |
diff -ruN router-anymoose/lib/HTTP/Router/Match.pm router-caf/lib/HTTP/Router/Match.pm | |
--- router-anymoose/lib/HTTP/Router/Match.pm 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/lib/HTTP/Router/Match.pm 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,55 +1,33 @@ | |
package HTTP::Router::Match; | |
-use Any::Moose; | |
+use strict; | |
+use warnings; | |
+use base 'Class::Accessor::Fast'; | |
-has 'params' => ( | |
- is => 'ro', | |
- isa => 'HashRef', | |
- lazy => 1, | |
- default => sub { +{} }, | |
-); | |
- | |
-has 'captures' => ( | |
- is => 'ro', | |
- isa => 'HashRef', | |
- lazy => 1, | |
- default => sub { +{} }, | |
-); | |
- | |
-has 'route' => ( | |
- is => 'ro', | |
- isa => 'HTTP::Router::Route', | |
- handles => ['uri_for'], | |
- required => 1, | |
-); | |
+__PACKAGE__->mk_ro_accessors(qw'params captures route'); | |
-no Any::Moose; | |
-__PACKAGE__->meta->make_immutable; | |
+sub uri_for { shift->route->uri_for(@_) } | |
+ | |
+1; | |
+ | |
+=for stopwords params | |
=head1 NAME | |
-HTTP::Router::Match - Matched Object Representation for HTTP::Router | |
+HTTP::Router::Match | |
=head1 METHODS | |
-=head2 uri_for($args?) | |
- | |
-Returns a route path which is processed with parameters. | |
+=head2 uri_for($captures?) | |
=head1 PROPERTIES | |
=head2 params | |
-Route parameters which was matched. | |
- | |
=head2 captures | |
-Captured variable parameters which was matched. | |
- | |
=head2 route | |
-L<HTTP::Router::Route> object which was matched. | |
- | |
=head1 AUTHOR | |
NAKAGAWA Masaki E<lt>[email protected]<gt> | |
diff -ruN router-anymoose/lib/HTTP/Router/Resources.pm router-caf/lib/HTTP/Router/Resources.pm | |
--- router-anymoose/lib/HTTP/Router/Resources.pm 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/lib/HTTP/Router/Resources.pm 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,164 @@ | |
+package HTTP::Router::Resources; | |
+ | |
+use strict; | |
+use warnings; | |
+use Carp 'croak'; | |
+use Hash::Merge 'merge'; | |
+use Lingua::EN::Inflect::Number qw(to_S to_PL); | |
+use String::CamelCase qw(camelize decamelize); | |
+ | |
+our @EXPORT = qw(resources resource); | |
+ | |
+sub import { | |
+ # TODO: into package | |
+ my $into = 'HTTP::Router::Mapper'; | |
+ eval "require $into; 1" or croak $@; | |
+ | |
+ no strict 'refs'; | |
+ no warnings 'redefine'; | |
+ for my $keyword (@EXPORT) { | |
+ *{ $into . '::' . $keyword } = \&{ __PACKAGE__ . '::' . $keyword }; | |
+ } | |
+} | |
+ | |
+sub resources { | |
+ my $self = shift; | |
+ | |
+ my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
+ my ($name, $args) = @_; | |
+ | |
+ my $path = decamelize $name; | |
+ my $params = { controller => $args->{controller} || camelize(to_PL($path)) }; | |
+ my $singular = $args->{singular} || to_S($path); | |
+ my $id = "${singular}_id"; | |
+ | |
+ my $collections = merge($args->{collection} || {}, { | |
+ index => { method => 'GET', path => '' }, | |
+ create => { method => 'POST', path => '' }, | |
+ post => { method => 'GET', path => '/new' }, | |
+ }); | |
+ | |
+ my $members = merge($args->{member} || {}, { | |
+ show => { method => 'GET', path => '' }, | |
+ update => { method => 'PUT', path => '' }, | |
+ destroy => { method => 'DELETE', path => '' }, | |
+ edit => { method => 'GET', path => '/edit' }, | |
+ }); | |
+ | |
+ $self->match("/${path}")->to($params, sub { | |
+ # collections | |
+ while (my ($action, $args) = each %$collections) { | |
+ my $path = ref $args eq 'HASH' ? $args->{path} : "/${action}"; | |
+ my $method = ref $args eq 'HASH' ? $args->{method} : $args; | |
+ my $conditions = { method => $method }; | |
+ my $params = { action => $action }; | |
+ $_[0]->match("${path}.{format}", $conditions)->to($params); | |
+ $_[0]->match("${path}", $conditions)->to($params); | |
+ } | |
+ | |
+ # members | |
+ $_[0]->match("/{$id}", sub { | |
+ while (my ($action, $args) = each %$members) { | |
+ my $path = ref $args ? $args->{path} : "/${action}"; | |
+ my $method = ref $args ? $args->{method} : $args; | |
+ my $conditions = { method => $method }; | |
+ my $params = { action => $action }; | |
+ $_[0]->match("${path}.{format}", $conditions)->to($params); | |
+ $_[0]->match("${path}", $conditions)->to($params); | |
+ } | |
+ }); | |
+ }); | |
+ | |
+ if ($block) { | |
+ local $_ = $self->_clone_mapper(path => $self->path . "/${path}/{$id}"); | |
+ $block->($_); | |
+ } | |
+ | |
+ $self; | |
+} | |
+ | |
+sub resource { | |
+ my $self = shift; | |
+ | |
+ my $block = ref $_[-1] eq 'CODE' ? pop : undef; | |
+ my ($name, $args) = @_; | |
+ | |
+ my $path = decamelize $name; | |
+ my $params = { controller => $args->{controller} || camelize($path) }; | |
+ | |
+ my $members = merge($args->{member} || {}, { | |
+ create => { method => 'POST', path => '' }, | |
+ show => { method => 'GET', path => '' }, | |
+ update => { method => 'PUT', path => '' }, | |
+ destroy => { method => 'DELETE', path => '' }, | |
+ post => { method => 'GET', path => '/new' }, | |
+ edit => { method => 'GET', path => '/edit' }, | |
+ }); | |
+ | |
+ $self->match("/${path}")->to($params, sub { | |
+ # members | |
+ while (my ($action, $args) = each %$members) { | |
+ my $path = ref $args eq 'HASH' ? $args->{path} : "/${action}"; | |
+ my $method = ref $args eq 'HASH' ? $args->{method} : $args; | |
+ my $conditions = { method => $method }; | |
+ my $params = { action => $action }; | |
+ $_[0]->match("${path}.{format}", $conditions)->to($params); | |
+ $_[0]->match("${path}", $conditions)->to($params); | |
+ } | |
+ }); | |
+ | |
+ if ($block) { | |
+ local $_ = $self->_clone_mapper(path => $self->path . "/${path}"); | |
+ $block->($_); | |
+ } | |
+ | |
+ $self; | |
+} | |
+ | |
+1; | |
+ | |
+=head1 NAME | |
+ | |
+HTTP::Router::Resources | |
+ | |
+=head1 SYNOPSIS | |
+ | |
+ use HTTP::Router; | |
+ use HTTP::Router::Resources; | |
+ | |
+ my $router = HTTP::Router->define(sub { | |
+ $_->resources('users'); | |
+ | |
+ $_->resource('account'); | |
+ | |
+ $_->resources('members', sub { | |
+ $_->resources('articles'); | |
+ }); | |
+ | |
+ $_->resources('members', { | |
+ controller => 'Users', | |
+ collection => { login => 'GET' }, | |
+ member => { settings => 'GET' }, | |
+ }); | |
+ }); | |
+ | |
+=head1 METHODS | |
+ | |
+=head2 resources($name, $args?, $block?) | |
+ | |
+=head2 resource($name, $args?, $block?) | |
+ | |
+=head1 AUTHOR | |
+ | |
+NAKAGAWA Masaki E<lt>[email protected]<gt> | |
+ | |
+=head1 LICENSE | |
+ | |
+This library is free software; you can redistribute it and/or modify | |
+it under the same terms as Perl itself. | |
+ | |
+=head1 SEE ALSO | |
+ | |
+L<HTTP::Router::Mapper>, L<HTTP::Router> | |
+ | |
+=cut | |
diff -ruN router-anymoose/lib/HTTP/Router/Route.pm router-caf/lib/HTTP/Router/Route.pm | |
--- router-anymoose/lib/HTTP/Router/Route.pm 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/lib/HTTP/Router/Route.pm 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,100 +1,83 @@ | |
package HTTP::Router::Route; | |
-use Any::Moose; | |
-use Any::Moose 'X::AttributeHelpers'; | |
-use URI::Template::Restrict 0.03; | |
+use strict; | |
+use warnings; | |
+use base 'Class::Accessor::Fast'; | |
+use Hash::Merge 'merge'; | |
+use List::MoreUtils 'any'; | |
+use Scalar::Util 1.14 'blessed'; | |
+use URI::Template::Restrict; | |
use HTTP::Router::Match; | |
-has 'path' => ( | |
- is => 'rw', | |
- isa => 'Str', | |
- metaclass => 'String', | |
- lazy_build => 1, | |
- provides => { append => 'append_path' }, | |
-); | |
- | |
-has 'params' => ( | |
- is => 'rw', | |
- isa => 'HashRef', | |
- metaclass => 'Collection::Hash', | |
- lazy_build => 1, | |
- provides => { set => 'add_params' }, | |
-); | |
- | |
-has 'conditions' => ( | |
- is => 'rw', | |
- isa => 'HashRef', | |
- metaclass => 'Collection::Hash', | |
- lazy_build => 1, | |
- provides => { set => 'add_conditions' }, | |
-); | |
- | |
-has 'parts' => ( | |
- is => 'rw', | |
- isa => 'Int', | |
- lazy => 1, | |
- default => sub { $_[0]->path =~ tr!/!/! }, | |
-); | |
- | |
-has 'templates' => ( | |
- is => 'rw', | |
- isa => 'URI::Template::Restrict', | |
- lazy_build => 1, | |
- handles => [qw'variables extract'], | |
-); | |
- | |
-sub _build_path { '' } | |
-sub _build_params { {} } | |
-sub _build_conditions { {} } | |
+__PACKAGE__->mk_ro_accessors(qw'path parts templates params conditions'); | |
-sub _build_templates { URI::Template::Restrict->new($_[0]->path) } | |
+sub variables { shift->templates->variables } | |
+ | |
+sub new { | |
+ my $class = shift; | |
+ my $args = ref $_[0] ? $_[0] : { @_ }; | |
+ | |
+ my @parts = split m!/! => $args->{path}; | |
+ $args->{parts} = scalar @parts; | |
+ $args->{templates} = URI::Template::Restrict->new($args->{path}); | |
+ $args->{params} ||= {}; | |
+ $args->{conditions} ||= {}; | |
+ | |
+ return bless $args, $class; | |
+} | |
sub match { | |
my ($self, $req) = @_; | |
return unless blessed $req and $req->can('path'); | |
my $path = $req->path; | |
- defined $path or return; | |
+ return unless defined $path; | |
+ | |
+ # part size | |
+ my $size = scalar @{[ split m!/! => $path ]}; | |
+ return unless $size == $self->parts; | |
# path, captures | |
- my %captures; | |
- if ($self->variables) { | |
- my $size = $path =~ tr!/!/!; | |
- $size == $self->parts or return; # FIXME: ignore parts | |
- %captures = $self->extract($path) or return; | |
- $self->_is_valid_variables(\%captures) or return; | |
+ my %vars = $self->templates->extract($path); | |
+ if (%vars) { | |
+ return unless $self->check_variable_conditions(\%vars); | |
} | |
else { | |
- $path eq $self->path or return; | |
+ return unless $path eq $self->path; | |
} | |
# conditions | |
- $self->_is_valid_request($req) or return; | |
+ return unless $self->check_request_conditions($req); | |
+ | |
+ return HTTP::Router::Match->new({ | |
+ params => merge(\%vars, $self->params), | |
+ captures => \%vars, | |
+ route => $self, | |
+ }); | |
+} | |
+ | |
+sub uri_for { | |
+ my ($self, $captures) = @_; | |
- my %params = %captures; | |
- for my $key (keys %{ $self->params }) { | |
- next if exists $params{$key}; | |
- $params{$key} = $self->params->{$key}; | |
+ my $params = $captures || {}; | |
+ for my $name (keys %$params) { | |
+ return unless $self->validate($params->{$name}, $self->conditions->{$name}); | |
} | |
- return HTTP::Router::Match->new( | |
- params => \%params, | |
- captures => \%captures, | |
- route => $self, | |
- ); | |
+ return $self->templates->process_to_string(%$params); | |
} | |
-sub _is_valid_variables { | |
+sub check_variable_conditions { | |
my ($self, $vars) = @_; | |
for my $name (keys %$vars) { | |
- return 0 unless $self->_validate($vars->{$name}, $self->conditions->{$name}); | |
+ return 0 unless $self->validate($vars->{$name}, $self->conditions->{$name}); | |
} | |
return 1; | |
} | |
-sub _is_valid_request { | |
+sub check_request_conditions { | |
my ($self, $req) = @_; | |
my $conditions = do { | |
@@ -110,109 +93,56 @@ | |
$value = 'GET' if $value eq 'HEAD'; | |
} | |
- return 0 unless $self->_validate($value, $self->conditions->{$name}); | |
+ return 0 unless $self->validate($value, $self->conditions->{$name}); | |
} | |
return 1; | |
} | |
-sub _validate { | |
+sub validate { | |
my ($self, $input, $expected) = @_; | |
# arguments | |
return 0 unless defined $input; | |
return 1 unless defined $expected; | |
# validation | |
- return $input =~ $expected if ref $expected eq 'Regexp'; | |
- return grep { $input eq $_ } @$expected if ref $expected eq 'ARRAY'; | |
+ return $input =~ $expected if ref $expected eq 'Regexp'; | |
+ return any { $input eq $_ } @$expected if ref $expected eq 'ARRAY'; | |
return $input eq $expected; | |
} | |
-sub uri_for { | |
- my ($self, $args) = @_; | |
- | |
- for my $name (keys %{ $args || {} }) { | |
- return unless $self->_validate($args->{$name}, $self->conditions->{$name}); | |
- } | |
- | |
- return $self->templates->process_to_string(%$args); | |
-} | |
- | |
-no Any::Moose; | |
-__PACKAGE__->meta->make_immutable; | |
+1; | |
=for stopwords params | |
=head1 NAME | |
-HTTP::Router::Route - Route Representation for HTTP::Router | |
- | |
-=head1 SYNOPSIS | |
- | |
- use HTTP::Router; | |
- use HTTP::Router::Route; | |
- | |
- my $router = HTTP::Router->new; | |
- | |
- my $route = HTTP::Router::Route->new( | |
- path => '/', | |
- conditions => { method => 'GET' }, | |
- params => { controller => 'Root', action => 'index' }, | |
- ); | |
- | |
- $router->add_route($route); | |
+HTTP::Router::Route | |
=head1 METHODS | |
=head2 match($req) | |
-Returns a L<HTTP::Router::Match> object, or C<undef> | |
-if route does not match a given request. | |
- | |
-=head2 append_path($path) | |
- | |
-Appends path to route. | |
- | |
-=head2 add_params($params) | |
- | |
-Adds parameters to route. | |
- | |
-=head2 add_conditions($conditions) | |
- | |
-Adds conditions to route. | |
- | |
-=head2 extract($path) | |
- | |
-Extracts variable values from $path, and returns variable hash. | |
- | |
-=head2 uri_for($args?) | |
- | |
-Returns a path which is processed with parameters. | |
+=head2 uri_for($captures?) | |
=head1 PROPERTIES | |
=head2 path | |
-Path string for route. | |
- | |
=head2 params | |
-Route specific parameters. | |
- | |
=head2 conditions | |
-Conditions for determining route. | |
+=head2 variables | |
=head2 templates | |
-L<URI::Template::Restrict> representation with route path. | |
+=head1 INTERNALS | |
-=head2 parts | |
+=head2 check_variable_conditions | |
-Size of splitting route path with slash. | |
- | |
-=head2 variables | |
+=head2 check_request_conditions | |
-Variable names in route path. | |
+=head2 validate | |
=head1 AUTHOR | |
diff -ruN router-anymoose/lib/HTTP/Router.pm router-caf/lib/HTTP/Router.pm | |
--- router-anymoose/lib/HTTP/Router.pm 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/lib/HTTP/Router.pm 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,114 +1,82 @@ | |
package HTTP::Router; | |
use 5.008_001; | |
-use Any::Moose; | |
-use Any::Moose 'X::AttributeHelpers'; | |
-use Hash::AsObject; | |
-use List::MoreUtils 'part'; | |
-use HTTP::Router::Route; | |
- | |
-our $VERSION = '0.03'; | |
- | |
-has 'routes' => ( | |
- is => 'ro', | |
- isa => 'ArrayRef', | |
- metaclass => 'Collection::Array', | |
- lazy => 1, | |
- builder => '_build_routes', | |
- auto_deref => 1, | |
- provides => { | |
- push => 'add_route', | |
- clear => 'clear_routes', | |
- }, | |
-); | |
- | |
-has 'use_inline_match' => ( | |
- is => 'rw', | |
- isa => 'Bool', | |
- default => 1, | |
-); | |
- | |
-has 'inline_matcher' => ( | |
- is => 'rw', | |
- isa => 'CodeRef', | |
- lazy_build => 1, | |
-); | |
+use strict; | |
+use warnings; | |
+use base 'Class::Accessor::Fast'; | |
+use Carp (); | |
+use HTTP::Router::Mapper; | |
-sub _build_routes { [] } | |
+our $VERSION = '0.01'; | |
-sub _build_inline_matcher { | |
+__PACKAGE__->mk_ro_accessors(qw'routes named_routes'); | |
+ | |
+sub new { | |
+ return bless { routes => [], named_routes => {} }, shift; | |
+} | |
+ | |
+sub routes { | |
my $self = shift; | |
+ if (@_) { | |
+ $self->_routes_accessor(@_); | |
+ } | |
+ else { | |
+ @{ $self->_routes_accessor }; | |
+ } | |
+} | |
- my ($path_routes, $capture_routes) = | |
- part { scalar $_->templates->expansions > 0 } $self->routes; | |
+sub add_route { | |
+ my ($self, $route) = @_; | |
+ push @{ $self->{routes} }, $route; | |
+} | |
- return sub { | |
- my $req = shift; | |
- my $parts = $req->path =~ tr!/!/!; | |
- | |
- # path | |
- for my $route (grep { $_->parts == $parts } @$path_routes) { | |
- my $match = $route->match($req) or next; | |
- return $match; # return if found path route | |
- } | |
- | |
- # capture | |
- for my $route (grep { $_->parts <= $parts } @$capture_routes) { | |
- my $match = $route->match($req) or next; | |
- return $match; | |
- } | |
- | |
- return; | |
- }; | |
+sub add_named_route { | |
+ my ($self, $name, $route) = @_; | |
+ $self->named_routes->{$name} = $route; | |
} | |
-around 'add_route' => sub { | |
- my ($next, $self, $route, @args) = @_; | |
+sub define { | |
+ my ($self, $block) = @_; | |
- unless (blessed $route) { | |
- $route = HTTP::Router::Route->new(path => $route, @args); | |
- } | |
+ Carp::croak('usage: HTTP::Router->define($coderef)') | |
+ unless ref $block and ref $block eq 'CODE'; | |
+ $self = $self->new unless ref $self; | |
- $self->clear_inline_matcher if $self->has_inline_matcher; | |
- $next->($self, $route); | |
-}; | |
+ local $_ = HTTP::Router::Mapper->new({ router => $self }); | |
+ $block->($_); | |
+ | |
+ return $self; | |
+} | |
sub reset { | |
my $self = shift; | |
- $self->clear_routes; | |
- $self->clear_inline_matcher if $self->has_inline_matcher; | |
- $self; | |
+ $self->{routes} = []; | |
+ $self->{named_routes} = {}; | |
+ return $self; | |
} | |
sub match { | |
- my $self = shift; | |
- my $req = blessed $_[0] ? $_[0] : Hash::AsObject->new(path => $_[0], %{ $_[1] || {} }); | |
+ my ($self, $req) = @_; | |
- if ($self->use_inline_match) { | |
- return $self->inline_matcher->($req); | |
+ for my $route ($self->routes) { | |
+ next unless my $match = $route->match($req); | |
+ return $match; | |
} | |
- else { | |
- for my $route ($self->routes) { | |
- my $match = $route->match($req) or next; | |
- return $match; | |
- } | |
- return; | |
- } | |
+ return; | |
} | |
sub route_for { | |
- my $self = shift; | |
+ my ($self, $req) = @_; | |
- if (my $match = $self->match(@_)) { | |
+ if (my $match = $self->match($req)) { | |
return $match->route; | |
} | |
return; | |
} | |
-no Any::Moose; | |
-__PACKAGE__->meta->make_immutable; | |
+1; | |
=head1 NAME | |
@@ -118,80 +86,51 @@ | |
use HTTP::Router; | |
- my $router = HTTP::Router->new; | |
+ my $router = HTTP::Router->define(sub { | |
+ $_->match('/')->to({ controller => 'Root', action => 'index' }); | |
+ $_->match('/index.{format}')->to({ controller => 'Root', action => 'index' }); | |
- my $route = HTTP::Router::Route->new( | |
- path => '/', | |
- conditions => { method => 'GET' }, | |
- params => { controller => 'Root', action => 'index' }, | |
- ); | |
- $router->add_route($route); | |
- # or | |
- $router->add_route('/' => ( | |
- conditions => { method => 'GET' }, | |
- params => { controller => 'Root', action => 'index' }, | |
- )); | |
+ $_->match('/archives/{year}/{month}', { year => qr/\d{4}/, month => qr/\d{2}/ }) | |
+ ->to({ controller => 'Archive', action => 'by_month' }); | |
- # GET / | |
- my $match = $router->match($req); | |
- $match->params; # { controller => 'Root', action => 'index' } | |
- $match->uri_for; # '/' | |
+ $_->match('/account/login', { method => ['GET', 'POST'] }) | |
+ ->to({ controller => 'Account', action => 'login' }); | |
-=head1 DESCRIPTION | |
- | |
-HTTP::Router provides a way of constructing routing tables. | |
- | |
-If you are interested in a Merb-like constructing way, | |
-please check L<HTTP::Router::Declare>. | |
- | |
-=head1 METHODS | |
- | |
-=head2 new | |
- | |
-Returns a HTTP::Router object. | |
+ $_->resources('users'); | |
-=head2 add_route($route) | |
+ $_->resource('account'); | |
-=head2 add_route($path, %args) | |
+ $_->resources('members', sub { | |
+ $_->resources('articles'); | |
+ }); | |
+ }); | |
-Adds a new route. | |
-You can specify L<HTTP::Router::Route> object, | |
-or path string and options pair. | |
- | |
-example: | |
+ # GET /index.html | |
+ my $match = $router->match($req); | |
+ $match->params; # { controller => 'Root', action => 'index', format => 'html' } | |
+ $match->captures; # { format => 'html' } | |
- my $route = HTTP::Router::Route->new( | |
- path => '/', | |
- conditions => { method => 'GET' }, | |
- params => { controller => 'Root', action => 'index' }, | |
- ); | |
+ $match->uri_for({ format => 'xml' }); # '/index.xml' | |
- $router->add_route($route); | |
+=head1 DESCRIPTION | |
-equals to: | |
+HTTP::Router provides a Merb-like way of constructing routing tables. | |
- $router->add_route('/' => ( | |
- conditions => { method => 'GET' }, | |
- params => { controller => 'Root', action => 'index' }, | |
- )); | |
+=head1 METHODS | |
-=head2 routes | |
+=head2 new | |
-Returns registered routes. | |
+=head2 define($code) | |
=head2 reset | |
-Clears registered routes. | |
- | |
=head2 match($req) | |
-Returns a L<HTTP::Router::Match> object that matches a given request. | |
-If no routes match, it returns C<undef>. | |
- | |
=head2 route_for($req) | |
-Returns a L<HTTP::Router::Route> object that matches a given request. | |
-If no routes match, it returns C<undef>. | |
+=head2 add_route | |
+ | |
+=head2 add_named_route | |
=head1 AUTHOR | |
@@ -206,7 +145,8 @@ | |
=head1 SEE ALSO | |
-L<HTTP::Router::Declare>, L<HTTP::Router::Route>, L<HTTP::Router::Match>, | |
+L<HTTP::Router::Mapper>, L<HTTP::Router::Resources>, | |
+L<HTTP::Router::Route>, L<HTTP::Router::Match>, | |
L<MojoX::Routes>, L<http://merbivore.com/>, | |
L<HTTPx::Dispatcher>, L<Path::Router>, L<Path::Dispatcher> | |
diff -ruN router-anymoose/lib/Test/HTTP/Router.pm router-caf/lib/Test/HTTP/Router.pm | |
--- router-anymoose/lib/Test/HTTP/Router.pm 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/lib/Test/HTTP/Router.pm 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,106 +0,0 @@ | |
-package Test::HTTP::Router; | |
- | |
-use strict; | |
-use warnings; | |
-use Exporter 'import'; | |
-use Test::Builder; | |
-use Test::Deep; | |
-use Test::MockObject; | |
- | |
-our @EXPORT = qw( | |
- path_ok path_not_ok | |
- match_ok match_not_ok | |
- params_ok params_not_ok | |
-); | |
- | |
-our $Test = Test::Builder->new; | |
- | |
-sub to_request { | |
- my $args = ref $_[0] ? shift : { @_ }; | |
- my $req = Test::MockObject->new; | |
- $req->set_always($_ => $args->{$_}) for keys %$args; | |
- $req; | |
-} | |
- | |
-sub path_ok { | |
- my ($router, $path, $message) = @_; | |
- my $req = to_request(path => $path); | |
- $Test->ok(my $match = $router->match($req) ? 1 : 0, $message || "matched $path"); | |
-} | |
- | |
-sub path_not_ok { | |
- my ($router, $path, $message) = @_; | |
- my $req = to_request(path => $path); | |
- $Test->ok(my $match = $router->match($req) ? 0 : 1, $message || "not matched $path"); | |
-} | |
- | |
-sub match_ok { | |
- my $router = shift; | |
- my $message = (@_ == 3 || @_ == 2 and not ref $_[-1]) ? pop : undef; | |
- my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]); | |
- $Test->ok(my $match = $router->match($req) ? 1 : 0, $message || "matched @{[$req->path]} with conditions"); | |
-} | |
- | |
-sub match_not_ok { | |
- my $router = shift; | |
- my $message = (@_ == 3 || @_ == 2 and not ref $_[-1]) ? pop : undef; | |
- my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]); | |
- $Test->ok(my $match = $router->match($req) ? 0 : 1, $message || "not matched @{[$req->path]} with conditions"); | |
-} | |
- | |
-sub params_ok { | |
- my $router = shift; | |
- my $message = (@_ == 4 || @_ == 3 and not ref $_[-1]) ? pop : undef; | |
- my $params = $_[-1]; | |
- my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]); | |
- | |
- my $match = $router->match($req); | |
- $Test->ok($match and eq_deeply($match->params, $params) ? 1 : 0, $message || "valid params at @{[$req->path]}"); | |
-} | |
- | |
-sub params_not_ok { | |
- my $router = shift; | |
- my $message = (@_ == 4 || @_ == 3 and not ref $_[-1]) ? pop : undef; | |
- my $params = $_[-1]; | |
- my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]); | |
- | |
- my $match = $router->match($req); | |
- $Test->ok($match and eq_deeply($match->params, $params) ? 0 : 1, $message || "invalid params at @{[$req->path]}"); | |
-} | |
- | |
-1; | |
- | |
-=head1 NAME | |
- | |
-Test::HTTP::Router - Route Testing | |
- | |
-=head1 SYNOPSIS | |
- | |
- use Test::More; | |
- use Test::HTTP::Router; | |
- use HTTP::Router; | |
- | |
- my $router = HTTP::Router->new; | |
- $router->add_route('/' => ( | |
- conditions => { method => 'GET' }, | |
- params => { controller => 'Root', action => 'index' }, | |
- )); | |
- | |
- match_ok $router, '/', { method => 'GET' }; | |
- params_ok $router, '/', { method => 'GET' }, { controller => 'Root', action => 'index' }; | |
- | |
-=head1 METHODS | |
- | |
-=head2 path_ok($router, $path, $message?) | |
- | |
-=head2 path_not_ok($router, $path, $message?) | |
- | |
-=head2 match_ok($router, $path, $conditions, $message?) | |
- | |
-=head2 match_not_ok($router, $path, $conditions, $message?) | |
- | |
-=head2 params_ok($router, $path, $conditions, $params, $message?) | |
- | |
-=head2 params_not_ok($router, $path, $conditions, $params, $message?) | |
- | |
-=cut | |
diff -ruN router-anymoose/t/10_route/captures.t router-caf/t/10_route/captures.t | |
--- router-anymoose/t/10_route/captures.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/10_route/captures.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,35 +0,0 @@ | |
-use Test::Base; | |
-use Test::Deep; | |
-use t::Router; | |
-use HTTP::Router::Route; | |
- | |
-plan tests => 2 * blocks; | |
- | |
-filters { map { $_ => ['eval'] } qw(params match request) }; | |
- | |
-run { | |
- my $block = shift; | |
- my $name = $block->name; | |
- my $route = HTTP::Router::Route->new( | |
- path => $block->path, | |
- params => $block->params, | |
- ); | |
- | |
- my $req = create_request($block->request); | |
- my $match = $route->match($req); | |
- ok $match, "match ($name)"; | |
- cmp_deeply $match->params => $block->match, "params ($name)"; | |
-}; | |
- | |
-__END__ | |
-=== /archives/{year} | |
---- path : /archives/{year} | |
---- params : { controller => 'Archive', action => 'by_year' } | |
---- request : { path => '/archives/2008' } | |
---- match : { controller => 'Archive', action => 'by_year', year => 2008 } | |
- | |
-=== /archives/{year}/{month} | |
---- path : /archives/{year}/{month} | |
---- params : { controller => 'Archive', action => 'by_month' } | |
---- request : { path => '/archives/2008/12' } | |
---- match : { controller => 'Archive', action => 'by_month', year => 2008, month => 12 } | |
diff -ruN router-anymoose/t/10_route/conditions.t router-caf/t/10_route/conditions.t | |
--- router-anymoose/t/10_route/conditions.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/10_route/conditions.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,61 +0,0 @@ | |
-use Test::Base; | |
-use Test::Deep; | |
-use t::Router; | |
-use HTTP::Router::Route; | |
- | |
-plan tests => 2 * blocks; | |
- | |
-filters { | |
- map { $_ => ['eval'] } qw(params conditions request match) | |
-}; | |
- | |
-run { | |
- my $block = shift; | |
- my $name = $block->name; | |
- my $route = HTTP::Router::Route->new( | |
- path => $block->path, | |
- params => $block->params, | |
- conditions => $block->conditions, | |
- ); | |
- | |
- my $req = create_request($block->request); | |
- my $match = $route->match($req); | |
- ok $match, "match ($name)"; | |
- cmp_deeply $match->params => $block->match, "params ($name)"; | |
-}; | |
- | |
-__END__ | |
-=== scalar conditions | |
---- path : / | |
---- params : { controller => 'Root', action => 'index' } | |
---- conditions: { method => 'GET' } | |
---- request : { path => '/', method => 'GET' } | |
---- match : { controller => 'Root', action => 'index' } | |
- | |
-=== array conditions | |
---- path : / | |
---- params : { controller => 'Root', action => 'index' } | |
---- conditions: { method => ['GET', 'POST'] } | |
---- request : { path => '/', method => 'GET' } | |
---- match : { controller => 'Root', action => 'index' } | |
- | |
-=== regexp conditions | |
---- path : / | |
---- params : { controller => 'Root', action => 'index' } | |
---- conditions: { method => qr/^(?:GET|POST)$/ } | |
---- request : { path => '/', method => 'GET' } | |
---- match : { controller => 'Root', action => 'index' } | |
- | |
-=== captures | |
---- path : /archives/{year} | |
---- params : { controller => 'Archive', action => 'by_year' } | |
---- conditions: { year => qr/^\d{4}$/ } | |
---- request : { path => '/archives/2008' } | |
---- match : { controller => 'Archive', action => 'by_year', year => 2008 } | |
- | |
-=== captures and conditions | |
---- path : /archives/{year} | |
---- params : { controller => 'Archive', action => 'by_year' } | |
---- conditions: { method => 'GET', year => qr/^\d{4}$/ } | |
---- request : { path => '/archives/2008', method => 'GET' } | |
---- match : { controller => 'Archive', action => 'by_year', year => 2008 } | |
diff -ruN router-anymoose/t/10_route/match.t router-caf/t/10_route/match.t | |
--- router-anymoose/t/10_route/match.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/10_route/match.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,33 +0,0 @@ | |
-use Test::Base; | |
-use Test::Deep; | |
-use t::Router; | |
-use HTTP::Router::Route; | |
- | |
-plan tests => 2 * blocks; | |
- | |
-filters { map { $_ => ['eval'] } qw(params request) }; | |
- | |
-run { | |
- my $block = shift; | |
- my $name = $block->name; | |
- my $route = HTTP::Router::Route->new( | |
- path => $block->path, | |
- params => $block->params, | |
- ); | |
- | |
- my $req = create_request($block->request); | |
- my $match = $route->match($req); | |
- ok $match, "match ($name)"; | |
- cmp_deeply $match->params => $block->params, "params ($name)"; | |
-}; | |
- | |
-__END__ | |
-=== / | |
---- path : / | |
---- params: { controller => 'Root', action => 'index' } | |
---- request: { path => '/' } | |
- | |
-=== /account/login | |
---- path : /account/login | |
---- params: { controller => 'Account', action => 'login' } | |
---- request: { path => '/account/login' } | |
diff -ruN router-anymoose/t/10_route/uri_for.t router-caf/t/10_route/uri_for.t | |
--- router-anymoose/t/10_route/uri_for.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/10_route/uri_for.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,45 +0,0 @@ | |
-use Test::Base; | |
-use HTTP::Router::Route; | |
- | |
-plan tests => 1 * blocks; | |
- | |
-filters { | |
- map { $_ => ['eval'] } qw(conditions args) | |
-}; | |
- | |
-run { | |
- my $block = shift; | |
- my $name = $block->name; | |
- my $route = HTTP::Router::Route->new( | |
- path => $block->path, | |
- conditions => $block->conditions || {}, | |
- ); | |
- | |
- is $route->uri_for($block->args) => $block->uri, "uri_for ($name)"; | |
-}; | |
- | |
-__END__ | |
-=== path | |
---- path: / | |
---- uri : / | |
- | |
-=== path with args | |
---- path: / | |
---- uri : / | |
---- args: { year => 2008 } | |
- | |
-=== captures | |
---- path: /archives/{year}/{month} | |
---- uri : /archives/2008/Dec | |
---- args: { year => 2008, month => 'Dec' } | |
- | |
-=== captures and conditions | |
---- path: /archives/{year}/{month} | |
---- conditions: { year => qr/^\d{4}$/, month => qr/^\d{1,2}$/ } | |
---- uri : /archives/2008/12 | |
---- args: { year => 2008, month => 12 } | |
- | |
-=== invalid conditions - undef | |
---- path: /archives/{year}/{month} | |
---- conditions: { year => qr/^\d{4}$/, month => qr/^\d{1,2}$/ } | |
---- args: { year => '08', month => 'Dec' } | |
diff -ruN router-anymoose/t/20_router/basic.t router-caf/t/20_router/basic.t | |
--- router-anymoose/t/20_router/basic.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/20_router/basic.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,19 +0,0 @@ | |
-use Test::More tests => 5; | |
-use Test::HTTP::Router; | |
-use HTTP::Router; | |
- | |
-my $r = HTTP::Router->new; | |
-$r->add_route('/', params => { controller => 'Root', action => 'index' }); | |
-$r->add_route( | |
- '/foo', | |
- conditions => { method => 'GET' }, | |
- params => { controller => 'Foo', action => 'index' } | |
-); | |
- | |
-is @{[ $r->routes ]} => 2; | |
- | |
-path_ok $r, '/'; | |
-params_ok $r, '/', { controller => 'Root', action => 'index' }; | |
- | |
-match_ok $r, '/foo', { method => 'GET' }; | |
-params_ok $r, '/foo', { method => 'GET' }, { controller => 'Foo', action => 'index' }; | |
diff -ruN router-anymoose/t/20_router/capture.t router-caf/t/20_router/capture.t | |
--- router-anymoose/t/20_router/capture.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/20_router/capture.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,23 +0,0 @@ | |
-use Test::More tests => 7; | |
-use Test::HTTP::Router; | |
-use HTTP::Router; | |
- | |
-my $r = HTTP::Router->new; | |
-$r->add_route('/bar', params => { action => 'path' }); | |
-$r->add_route( | |
- '/{year}', | |
- conditions => { year => qr/^\d{4}$/ }, | |
- params => { action => 'by_year' } | |
-); | |
-$r->add_route('/{user_id}', params => { action => 'capture' }); | |
- | |
-is @{[ $r->routes ]} => 3; | |
- | |
-path_ok $r, '/2009'; | |
-params_ok $r, '/2009', { action => 'by_year', year => 2009 }; | |
- | |
-path_ok $r, '/foo'; | |
-params_ok $r, '/foo', { action => 'capture', user_id => 'foo' }; | |
- | |
-path_ok $r, '/bar'; | |
-params_ok $r, '/bar', { action => 'path' }; | |
diff -ruN router-anymoose/t/20_router/debug.t router-caf/t/20_router/debug.t | |
--- router-anymoose/t/20_router/debug.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/20_router/debug.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,8 +0,0 @@ | |
-use Test::More tests => 3; | |
-use HTTP::Router; | |
-use HTTP::Router::Debug; | |
- | |
-my $r = HTTP::Router->new; | |
-can_ok $r => 'routing_table'; | |
-can_ok $r => 'show_table'; | |
-isa_ok $r->routing_table => 'Text::SimpleTable'; | |
diff -ruN router-anymoose/t/20_router/reset.t router-caf/t/20_router/reset.t | |
--- router-anymoose/t/20_router/reset.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/20_router/reset.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,15 +0,0 @@ | |
-use Test::More tests => 3; | |
-use HTTP::Router; | |
-use HTTP::Router::Route; | |
- | |
-my $r = HTTP::Router->new; | |
-is @{[ $r->routes ]} => 0; | |
- | |
-for my $i (1..10) { | |
- my $route = HTTP::Router::Route->new(path => $i); | |
- $r->add_route($route); | |
-} | |
-is @{[ $r->routes ]} => 10; | |
- | |
-$r->reset; | |
-is @{[ $r->routes ]} => 0; | |
diff -ruN router-anymoose/t/30_declare/match-block.t router-caf/t/30_declare/match-block.t | |
--- router-anymoose/t/30_declare/match-block.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/match-block.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,32 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 7; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- match '/account' => to { controller => 'Account' } => then { | |
- match '/login' => to { action => 'login' }; | |
- match '/logout' => to { action => 'logout' }; | |
- }; | |
- | |
- match '/users' => then { | |
- match '/new' => to { controller => 'Account', action => 'register' }; | |
- match '/list' => to { controller => 'Users', action => 'list' }; | |
- }; | |
- | |
- match { method => 'POST' } => then { | |
- match '/search' => to { controller => 'Items', action => 'search' }; | |
- match '/tags' => to { controller => 'Tags', action => 'index' }; | |
- }; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 6; | |
- | |
-path_ok $router, '/account/login'; | |
-path_ok $router, '/account/logout'; | |
- | |
-path_ok $router, '/users/new'; | |
-path_ok $router, '/users/list'; | |
- | |
-match_ok $router, '/search', { method => 'POST' }; | |
-match_ok $router, '/tags', { method => 'POST' }; | |
diff -ruN router-anymoose/t/30_declare/match.t router-caf/t/30_declare/match.t | |
--- router-anymoose/t/30_declare/match.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/match.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,24 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 5; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- match '/' => to { controller => 'Root', action => 'index' }; | |
- | |
- match '/home', { method => 'GET' } | |
- => to { controller => 'Home', action => 'show' }; | |
- match '/date/{year}', { year => qr/^\d{4}$/ } | |
- => to { controller => 'Date', action => 'by_year' }; | |
- | |
- match '/{controller}/{action}/{id}'; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 4; | |
- | |
-path_ok $router, '/'; | |
- | |
-match_ok $router, '/home', { method => 'GET' }; | |
-path_ok $router, '/date/2009'; | |
- | |
-path_ok $router, '/foo/bar/baz'; | |
diff -ruN router-anymoose/t/30_declare/resource-args.t router-caf/t/30_declare/resource-args.t | |
--- router-anymoose/t/30_declare/resource-args.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/resource-args.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,27 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 6; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- resource 'Admin', { | |
- controller => 'User::Admin', | |
- member => { settings => 'GET' }, | |
- }; | |
- | |
- resource 'Account', { except => [qw(new edit delete)] }; | |
- | |
- resource 'User', { only => [qw(show update)] }; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 28; # admin => 16, account => 8, user => 4 | |
- | |
-params_ok $router, '/admin/settings', { method => 'GET' }, | |
- { controller => 'User::Admin', action => 'settings' }; | |
-params_ok $router, '/admin/settings.html', { method => 'GET' }, | |
- { controller => 'User::Admin', action => 'settings', format => 'html' }; | |
- | |
-match_not_ok $router, '/account/edit', { method => 'GET' }, 'not matched excepted action'; | |
- | |
-params_ok $router, '/user', { method => 'GET' }, { controller => 'User', action => 'show' }; | |
-match_not_ok $router, '/user', { method => 'DELETE' }, 'not matched !only action'; | |
diff -ruN router-anymoose/t/30_declare/resource-block.t router-caf/t/30_declare/resource-block.t | |
--- router-anymoose/t/30_declare/resource-block.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/resource-block.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,29 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 8; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- resource 'Account', { member => { settings => 'GET' } } => then { | |
- resource 'Admin'; | |
- resource 'User', { only => [qw(show update)] }; | |
- }; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 34; | |
- | |
-params_ok $router, '/account', { method => 'GET' }, | |
- { controller => 'Account', action => 'show' }; | |
-params_ok $router, '/account', { method => 'POST' }, | |
- { controller => 'Account', action => 'create' }; | |
-params_ok $router, '/account/settings', { method => 'GET' }, | |
- { controller => 'Account', action => 'settings' }; | |
- | |
-params_ok $router, '/account/admin', { method => 'GET' }, | |
- { controller => 'Admin', action => 'show' }; | |
-params_ok $router, '/account/admin', { method => 'POST' }, | |
- { controller => 'Admin', action => 'create' }; | |
- | |
-params_ok $router, '/account/user', { method => 'GET' }, | |
- { controller => 'User', action => 'show' }; | |
-match_not_ok $router, '/account/user', { method => 'POST' }; | |
diff -ruN router-anymoose/t/30_declare/resource.t router-caf/t/30_declare/resource.t | |
--- router-anymoose/t/30_declare/resource.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/resource.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,36 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 15; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- resource 'Account'; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 14; | |
- | |
-params_ok $router, '/account', { method => 'GET' }, { controller => 'Account', action => 'show' }; | |
-params_ok $router, '/account', { method => 'POST' }, { controller => 'Account', action => 'create' }; | |
-params_ok $router, '/account', { method => 'PUT' }, { controller => 'Account', action => 'update' }; | |
-params_ok $router, '/account', { method => 'DELETE' }, { controller => 'Account', action => 'destroy' }; | |
- | |
-params_ok $router, '/account/new', { method => 'GET' }, { controller => 'Account', action => 'post' }; | |
-params_ok $router, '/account/edit', { method => 'GET' }, { controller => 'Account', action => 'edit' }; | |
-params_ok $router, '/account/delete', { method => 'GET' }, { controller => 'Account', action => 'delete' }; | |
- | |
-# with format | |
-params_ok $router, '/account.html', { method => 'GET' }, | |
- { controller => 'Account', action => 'show', format => 'html' }; | |
-params_ok $router, '/account.html', { method => 'POST' }, | |
- { controller => 'Account', action => 'create', format => 'html' }; | |
-params_ok $router, '/account.html', { method => 'PUT' }, | |
- { controller => 'Account', action => 'update', format => 'html' }; | |
-params_ok $router, '/account.html', { method => 'DELETE' }, | |
- { controller => 'Account', action => 'destroy', format => 'html' }; | |
- | |
-params_ok $router, '/account/new.html', { method => 'GET' }, | |
- { controller => 'Account', action => 'post', format => 'html' }; | |
-params_ok $router, '/account/edit.html', { method => 'GET' }, | |
- { controller => 'Account', action => 'edit', format => 'html' }; | |
-params_ok $router, '/account/delete.html', { method => 'GET' }, | |
- { controller => 'Account', action => 'delete', format => 'html' }; | |
diff -ruN router-anymoose/t/30_declare/resources-args.t router-caf/t/30_declare/resources-args.t | |
--- router-anymoose/t/30_declare/resources-args.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/resources-args.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,32 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 8; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- resources 'Users', { | |
- collection => { recent => 'GET' }, | |
- member => { settings => 'GET' }, | |
- }; | |
- | |
- resources 'Articles', { except => [qw(edit delete)] }; | |
- resources 'Entries', { only => [qw(show update)] }; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 36; # users => 20, articles => 12, entries => 4 | |
- | |
-params_ok $router, '/users/recent', { method => 'GET' }, | |
- { controller => 'Users', action => 'recent' }; | |
-params_ok $router, '/users/recent.html', { method => 'GET' }, | |
- { controller => 'Users', action => 'recent', format => 'html' }; | |
- | |
-params_ok $router, '/users/1/settings', { method => 'GET' }, | |
- { controller => 'Users', action => 'settings', user_id => 1 }; | |
-params_ok $router, '/users/1/settings.html', { method => 'GET' }, | |
- { controller => 'Users', action => 'settings', user_id => 1, format => 'html' }; | |
- | |
-match_not_ok $router, '/articles/1/edit', { method => 'GET' }, 'not matched excepted action'; | |
- | |
-params_ok $router, '/entries/1', { method => 'GET' }, | |
- { controller => 'Entries', action => 'show', entry_id => 1 }; | |
-match_not_ok $router, '/entries/1', { method => 'DELETE' }, 'not matched !only action'; | |
diff -ruN router-anymoose/t/30_declare/resources-block.t router-caf/t/30_declare/resources-block.t | |
--- router-anymoose/t/30_declare/resources-block.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/resources-block.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,29 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 8; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- resources 'Users', { collection => { recent => 'GET' }, member => { settings => 'GET' } } => then { | |
- resources 'Articles'; | |
- resources 'Entries', { only => [qw(show update)] }; | |
- }; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 40; | |
- | |
-params_ok $router, '/users', { method => 'GET' }, | |
- { controller => 'Users', action => 'index' }; | |
-params_ok $router, '/users/recent', { method => 'GET' }, | |
- { controller => 'Users', action => 'recent' }; | |
-params_ok $router, '/users/1/settings', { method => 'GET' }, | |
- { controller => 'Users', action => 'settings', user_id => 1 }; | |
- | |
-params_ok $router, '/users/1/articles', { method => 'GET' }, | |
- { controller => 'Articles', action => 'index', user_id => 1 }; | |
-params_ok $router, '/users/1/articles', { method => 'POST' }, | |
- { controller => 'Articles', action => 'create', user_id => 1 }; | |
- | |
-params_ok $router, '/users/1/entries/1', { method => 'GET' }, | |
- { controller => 'Entries', action => 'show', user_id => 1, entry_id => 1 }; | |
-match_not_ok $router, '/users/1/entries/1', { method => 'DELETE' }; | |
diff -ruN router-anymoose/t/30_declare/resources.t router-caf/t/30_declare/resources.t | |
--- router-anymoose/t/30_declare/resources.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/resources.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,51 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 17; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- resources 'Users'; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 16; | |
- | |
-params_ok $router, '/users', { method => 'GET' }, | |
- { controller => 'Users', action => 'index' }; | |
-params_ok $router, '/users', { method => 'POST' }, | |
- { controller => 'Users', action => 'create' }; | |
- | |
-params_ok $router, '/users/new', { method => 'GET' }, | |
- { controller => 'Users', action => 'post' }; | |
- | |
-params_ok $router, '/users/1', { method => 'GET' }, | |
- { controller => 'Users', action => 'show', user_id => 1 }; | |
-params_ok $router, '/users/1', { method => 'PUT' }, | |
- { controller => 'Users', action => 'update', user_id => 1 }; | |
-params_ok $router, '/users/1', { method => 'DELETE' }, | |
- { controller => 'Users', action => 'destroy', user_id => 1 }; | |
- | |
-params_ok $router, '/users/1/edit', { method => 'GET' }, | |
- { controller => 'Users', action => 'edit', user_id => 1 }; | |
-params_ok $router, '/users/1/delete', { method => 'GET' }, | |
- { controller => 'Users', action => 'delete', user_id => 1 }; | |
- | |
-# with format | |
-params_ok $router, '/users.html', { method => 'GET' }, | |
- { controller => 'Users', action => 'index', format => 'html' }; | |
-params_ok $router, '/users.html', { method => 'POST' }, | |
- { controller => 'Users', action => 'create', format => 'html' }; | |
- | |
-params_ok $router, '/users/new.html', { method => 'GET' }, | |
- { controller => 'Users', action => 'post', format => 'html' }; | |
- | |
-params_ok $router, '/users/1.html', { method => 'GET' }, | |
- { controller => 'Users', action => 'show', user_id => 1, format => 'html' }; | |
-params_ok $router, '/users/1.html', { method => 'PUT' }, | |
- { controller => 'Users', action => 'update', user_id => 1, format => 'html' }; | |
-params_ok $router, '/users/1.html', { method => 'DELETE' }, | |
- { controller => 'Users', action => 'destroy', user_id => 1, format => 'html' }; | |
- | |
-params_ok $router, '/users/1/edit.html', { method => 'GET' }, | |
- { controller => 'Users', action => 'edit', user_id => 1, format => 'html' }; | |
-params_ok $router, '/users/1/delete.html', { method => 'GET' }, | |
- { controller => 'Users', action => 'delete', user_id => 1, format => 'html' }; | |
diff -ruN router-anymoose/t/30_declare/with.t router-caf/t/30_declare/with.t | |
--- router-anymoose/t/30_declare/with.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/t/30_declare/with.t 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,18 +0,0 @@ | |
-use strict; | |
-use Test::More tests => 4; | |
-use Test::HTTP::Router; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- with { controller => 'Account' } => then { | |
- match '/login' => to { action => 'login' }; | |
- match '/logout' => to { action => 'logout' }; | |
- match '/signup' => to { action => 'signup' }; | |
- }; | |
-}; | |
- | |
-is scalar @{[ $router->routes ]} => 3; | |
- | |
-path_ok $router, '/login'; | |
-path_ok $router, '/logout'; | |
-path_ok $router, '/signup'; | |
diff -ruN router-anymoose/t/resource/resource-args.t router-caf/t/resource/resource-args.t | |
--- router-anymoose/t/resource/resource-args.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/resource/resource-args.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,44 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+use HTTP::Router::Resources; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->resource('admin', { | |
+ controller => 'User::Admin', | |
+ member => { settings => 'GET' }, | |
+ }); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => 12 + 2; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== show | |
+--- request: { path => '/admin', method => 'GET' } | |
+--- results: { controller => 'User::Admin', action => 'show' } | |
+ | |
+=== formatted show | |
+--- request: { path => '/admin.html', method => 'GET' } | |
+--- results: { controller => 'User::Admin', action => 'show', format => 'html' } | |
+ | |
+=== settings | |
+--- request: { path => '/admin/settings', method => 'GET' } | |
+--- results: { controller => 'User::Admin', action => 'settings' } | |
+ | |
+=== formatted settings | |
+--- request: { path => '/admin/settings.html', method => 'GET' } | |
+--- results: { controller => 'User::Admin', action => 'settings', format => 'html' } | |
diff -ruN router-anymoose/t/resource/resource-block.t router-caf/t/resource/resource-block.t | |
--- router-anymoose/t/resource/resource-block.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/resource/resource-block.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,75 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+use HTTP::Router::Resources; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->resource('account', sub { | |
+ $_->resource('admin'); | |
+ }); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => 24; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== create | |
+--- request: { path => '/account/admin', method => 'POST' } | |
+--- results: { controller => 'Admin', action => 'create' } | |
+ | |
+=== formatted create | |
+--- request: { path => '/account/admin.html', method => 'POST' } | |
+--- results: { controller => 'Admin', action => 'create', format => 'html' } | |
+ | |
+=== show | |
+--- request: { path => '/account/admin', method => 'GET' } | |
+--- results: { controller => 'Admin', action => 'show' } | |
+ | |
+=== formatted show | |
+--- request: { path => '/account/admin.html', method => 'GET' } | |
+--- results: { controller => 'Admin', action => 'show', format => 'html' } | |
+ | |
+=== update | |
+--- request: { path => '/account/admin', method => 'PUT' } | |
+--- results: { controller => 'Admin', action => 'update' } | |
+ | |
+=== formatted update | |
+--- request: { path => '/account/admin.html', method => 'PUT' } | |
+--- results: { controller => 'Admin', action => 'update', format => 'html' } | |
+ | |
+=== destroy | |
+--- request: { path => '/account/admin', method => 'DELETE' } | |
+--- results: { controller => 'Admin', action => 'destroy' } | |
+ | |
+=== formatted destroy | |
+--- request: { path => '/account/admin.html', method => 'DELETE' } | |
+--- results: { controller => 'Admin', action => 'destroy', format => 'html' } | |
+ | |
+=== new | |
+--- request: { path => '/account/admin/new', method => 'GET' } | |
+--- results: { controller => 'Admin', action => 'post' } | |
+ | |
+=== formatted new | |
+--- request: { path => '/account/admin/new.html', method => 'GET' } | |
+--- results: { controller => 'Admin', action => 'post', format => 'html' } | |
+ | |
+=== edit | |
+--- request: { path => '/account/admin/edit', method => 'GET' } | |
+--- results: { controller => 'Admin', action => 'edit' } | |
+ | |
+=== formatted edit | |
+--- request: { path => '/account/admin/edit.html', method => 'GET' } | |
+--- results: { controller => 'Admin', action => 'edit', format => 'html' } | |
diff -ruN router-anymoose/t/resource/resource.t router-caf/t/resource/resource.t | |
--- router-anymoose/t/resource/resource.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/resource/resource.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,73 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+use HTTP::Router::Resources; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->resource('Account'); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => blocks; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== create | |
+--- request: { path => '/account', method => 'POST' } | |
+--- results: { controller => 'Account', action => 'create' } | |
+ | |
+=== formatted create | |
+--- request: { path => '/account.html', method => 'POST' } | |
+--- results: { controller => 'Account', action => 'create', format => 'html' } | |
+ | |
+=== show | |
+--- request: { path => '/account', method => 'GET' } | |
+--- results: { controller => 'Account', action => 'show' } | |
+ | |
+=== formatted show | |
+--- request: { path => '/account.html', method => 'GET' } | |
+--- results: { controller => 'Account', action => 'show', format => 'html' } | |
+ | |
+=== update | |
+--- request: { path => '/account', method => 'PUT' } | |
+--- results: { controller => 'Account', action => 'update' } | |
+ | |
+=== formatted update | |
+--- request: { path => '/account.html', method => 'PUT' } | |
+--- results: { controller => 'Account', action => 'update', format => 'html' } | |
+ | |
+=== destroy | |
+--- request: { path => '/account', method => 'DELETE' } | |
+--- results: { controller => 'Account', action => 'destroy' } | |
+ | |
+=== formatted destroy | |
+--- request: { path => '/account.html', method => 'DELETE' } | |
+--- results: { controller => 'Account', action => 'destroy', format => 'html' } | |
+ | |
+=== new | |
+--- request: { path => '/account/new', method => 'GET' } | |
+--- results: { controller => 'Account', action => 'post' } | |
+ | |
+=== formatted new | |
+--- request: { path => '/account/new.html', method => 'GET' } | |
+--- results: { controller => 'Account', action => 'post', format => 'html' } | |
+ | |
+=== edit | |
+--- request: { path => '/account/edit', method => 'GET' } | |
+--- results: { controller => 'Account', action => 'edit' } | |
+ | |
+=== formatted edit | |
+--- request: { path => '/account/edit.html', method => 'GET' } | |
+--- results: { controller => 'Account', action => 'edit', format => 'html' } | |
diff -ruN router-anymoose/t/resource/resources-args.t router-caf/t/resource/resources-args.t | |
--- router-anymoose/t/resource/resources-args.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/resource/resources-args.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,61 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+use HTTP::Router::Resources; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->resources('members', { | |
+ controller => 'Users', | |
+ collection => { login => 'POST', logout => ['GET', 'POST'] }, | |
+ member => { settings => 'GET' }, | |
+ }); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => 14 + 6; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== index | |
+--- request: { path => '/members', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'index' } | |
+ | |
+=== formatted index | |
+--- request: { path => '/members.html', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'index', format => 'html' } | |
+ | |
+=== login | |
+--- request: { path => '/members/login', method => 'POST' } | |
+--- results: { controller => 'Users', action => 'login' } | |
+ | |
+=== formatted login | |
+--- request: { path => '/members/login.html', method => 'POST' } | |
+--- results: { controller => 'Users', action => 'login', format => 'html' } | |
+ | |
+=== logout | |
+--- request: { path => '/members/logout', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'logout' } | |
+ | |
+=== formatted logout | |
+--- request: { path => '/members/logout.html', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'logout', format => 'html' } | |
+ | |
+=== settings | |
+--- request: { path => '/members/10/settings', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'settings', member_id => 10 } | |
+ | |
+=== formatted settings | |
+--- request: { path => '/members/10/settings.html', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'settings', member_id => 10, format => 'html' } | |
diff -ruN router-anymoose/t/resource/resources-block.t router-caf/t/resource/resources-block.t | |
--- router-anymoose/t/resource/resources-block.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/resource/resources-block.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,83 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+use HTTP::Router::Resources; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->resources('Users', sub { | |
+ $_->resources('Articles'); | |
+ }); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => 28; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== index | |
+--- request: { path => '/users/10/articles', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'index', user_id => 10 } | |
+ | |
+=== formatted index | |
+--- request: { path => '/users/10/articles.html', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'index', user_id => 10, format => 'html' } | |
+ | |
+=== new | |
+--- request: { path => '/users/10/articles/new', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'post', user_id => 10 } | |
+ | |
+=== formatted new | |
+--- request: { path => '/users/10/articles/new.html', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'post', user_id => 10, format => 'html' } | |
+ | |
+=== create | |
+--- request: { path => '/users/10/articles', method => 'POST' } | |
+--- results: { controller => 'Articles', action => 'create', user_id => 10 } | |
+ | |
+=== formatted create | |
+--- request: { path => '/users/10/articles.html', method => 'POST' } | |
+--- results: { controller => 'Articles', action => 'create', user_id => 10, format => 'html' } | |
+ | |
+=== show | |
+--- request: { path => '/users/10/articles/20', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'show', user_id => 10, article_id => 20 } | |
+ | |
+=== formatted show | |
+--- request: { path => '/users/10/articles/20.html', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'show', user_id => 10, article_id => 20, format => 'html' } | |
+ | |
+=== edit | |
+--- request: { path => '/users/10/articles/20/edit', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'edit', user_id => 10, article_id => 20 } | |
+ | |
+=== formatted edit | |
+--- request: { path => '/users/10/articles/20/edit.html', method => 'GET' } | |
+--- results: { controller => 'Articles', action => 'edit', user_id => 10, article_id => 20, format => 'html' } | |
+ | |
+=== update | |
+--- request: { path => '/users/10/articles/20', method => 'PUT' } | |
+--- results: { controller => 'Articles', action => 'update', user_id => 10, article_id => 20 } | |
+ | |
+=== formatted update | |
+--- request: { path => '/users/10/articles/20.html', method => 'PUT' } | |
+--- results: { controller => 'Articles', action => 'update', user_id => 10, article_id => 20, format => 'html' } | |
+ | |
+=== destroy | |
+--- request: { path => '/users/10/articles/20', method => 'DELETE' } | |
+--- results: { controller => 'Articles', action => 'destroy', user_id => 10, article_id => 20 } | |
+ | |
+=== formatted destroy | |
+--- request: { path => '/users/10/articles/20.html', method => 'DELETE' } | |
+--- results: { controller => 'Articles', action => 'destroy', user_id => 10, article_id => 20, format => 'html' } | |
diff -ruN router-anymoose/t/resource/resources.t router-caf/t/resource/resources.t | |
--- router-anymoose/t/resource/resources.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/resource/resources.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,81 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+use HTTP::Router::Resources; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->resources('Users'); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => blocks; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== index | |
+--- request: { path => '/users', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'index' } | |
+ | |
+=== formatted index | |
+--- request: { path => '/users.html', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'index', format => 'html' } | |
+ | |
+=== new | |
+--- request: { path => '/users/new', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'post' } | |
+ | |
+=== formatted new | |
+--- request: { path => '/users/new.html', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'post', format => 'html' } | |
+ | |
+=== create | |
+--- request: { path => '/users', method => 'POST' } | |
+--- results: { controller => 'Users', action => 'create' } | |
+ | |
+=== formatted create | |
+--- request: { path => '/users.html', method => 'POST' } | |
+--- results: { controller => 'Users', action => 'create', format => 'html' } | |
+ | |
+=== show | |
+--- request: { path => '/users/10', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'show', user_id => 10 } | |
+ | |
+=== formatted show | |
+--- request: { path => '/users/10.html', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'show', user_id => 10, format => 'html' } | |
+ | |
+=== edit | |
+--- request: { path => '/users/10/edit', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'edit', user_id => 10 } | |
+ | |
+=== formatted edit | |
+--- request: { path => '/users/10/edit.html', method => 'GET' } | |
+--- results: { controller => 'Users', action => 'edit', user_id => 10, format => 'html' } | |
+ | |
+=== update | |
+--- request: { path => '/users/10', method => 'PUT' } | |
+--- results: { controller => 'Users', action => 'update', user_id => 10 } | |
+ | |
+=== formatted update | |
+--- request: { path => '/users/10.html', method => 'PUT' } | |
+--- results: { controller => 'Users', action => 'update', user_id => 10, format => 'html' } | |
+ | |
+=== destroy | |
+--- request: { path => '/users/10', method => 'DELETE' } | |
+--- results: { controller => 'Users', action => 'destroy', user_id => 10 } | |
+ | |
+=== formatted destroy | |
+--- request: { path => '/users/10.html', method => 'DELETE' } | |
+--- results: { controller => 'Users', action => 'destroy', user_id => 10, format => 'html' } | |
diff -ruN router-anymoose/t/route/captures.t router-caf/t/route/captures.t | |
--- router-anymoose/t/route/captures.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/route/captures.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,38 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router::Route; | |
+ | |
+plan tests => 3 * blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(params match captures request) }; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $name = $block->name; | |
+ my $route = HTTP::Router::Route->new( | |
+ path => $block->path, | |
+ params => $block->params, | |
+ ); | |
+ | |
+ my $req = create_request($block->request); | |
+ my $match = $route->match($req); | |
+ ok $match, "match ($name)"; | |
+ cmp_deeply $match->params => $block->match, "params ($name)"; | |
+ cmp_deeply $match->captures => $block->captures, "captures ($name)"; | |
+}; | |
+ | |
+__END__ | |
+=== /archives/{year} | |
+--- path : /archives/{year} | |
+--- params : { controller => 'Archive', action => 'by_year' } | |
+--- request : { path => '/archives/2008' } | |
+--- match : { controller => 'Archive', action => 'by_year', year => 2008 } | |
+--- captures: { year => 2008 } | |
+ | |
+=== /archives/{year}/{month} | |
+--- path : /archives/{year}/{month} | |
+--- params : { controller => 'Archive', action => 'by_month' } | |
+--- request : { path => '/archives/2008/12' } | |
+--- match : { controller => 'Archive', action => 'by_month', year => 2008, month => 12 } | |
+--- captures: { year => 2008, month => 12 } | |
diff -ruN router-anymoose/t/route/conditions.t router-caf/t/route/conditions.t | |
--- router-anymoose/t/route/conditions.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/route/conditions.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,68 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router::Route; | |
+ | |
+plan tests => 3 * blocks; | |
+ | |
+filters { | |
+ map { $_ => ['eval'] } qw(params conditions request match captures) | |
+}; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $name = $block->name; | |
+ my $route = HTTP::Router::Route->new( | |
+ path => $block->path, | |
+ params => $block->params, | |
+ conditions => $block->conditions, | |
+ ); | |
+ | |
+ my $req = create_request($block->request); | |
+ my $match = $route->match($req); | |
+ ok $match, "match ($name)"; | |
+ cmp_deeply $match->params => $block->match, "params ($name)"; | |
+ cmp_deeply $match->captures => $block->captures, "captures ($name)"; | |
+}; | |
+ | |
+__END__ | |
+=== scalar conditions | |
+--- path : / | |
+--- params : { controller => 'Root', action => 'index' } | |
+--- conditions: { method => 'GET' } | |
+--- request : { path => '/', method => 'GET' } | |
+--- match : { controller => 'Root', action => 'index' } | |
+--- captures : {} | |
+ | |
+ | |
+=== array conditions | |
+--- path : / | |
+--- params : { controller => 'Root', action => 'index' } | |
+--- conditions: { method => ['GET', 'POST'] } | |
+--- request : { path => '/', method => 'GET' } | |
+--- match : { controller => 'Root', action => 'index' } | |
+--- captures : {} | |
+ | |
+=== regexp conditions | |
+--- path : / | |
+--- params : { controller => 'Root', action => 'index' } | |
+--- conditions: { method => qr/^(?:GET|POST)$/ } | |
+--- request : { path => '/', method => 'GET' } | |
+--- match : { controller => 'Root', action => 'index' } | |
+--- captures : {} | |
+ | |
+=== captures | |
+--- path : /archives/{year} | |
+--- params : { controller => 'Archive', action => 'by_year' } | |
+--- conditions: { year => qr/^\d{4}$/ } | |
+--- request : { path => '/archives/2008' } | |
+--- match : { controller => 'Archive', action => 'by_year', year => 2008 } | |
+--- captures : { year => 2008 } | |
+ | |
+=== captures and conditions | |
+--- path : /archives/{year} | |
+--- params : { controller => 'Archive', action => 'by_year' } | |
+--- conditions: { method => 'GET', year => qr/^\d{4}$/ } | |
+--- request : { path => '/archives/2008', method => 'GET' } | |
+--- match : { controller => 'Archive', action => 'by_year', year => 2008 } | |
+--- captures : { year => 2008 } | |
diff -ruN router-anymoose/t/route/match.t router-caf/t/route/match.t | |
--- router-anymoose/t/route/match.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/route/match.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,33 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router::Route; | |
+ | |
+plan tests => 2 * blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(params request) }; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $name = $block->name; | |
+ my $route = HTTP::Router::Route->new( | |
+ path => $block->path, | |
+ params => $block->params, | |
+ ); | |
+ | |
+ my $req = create_request($block->request); | |
+ my $match = $route->match($req); | |
+ ok $match, "match ($name)"; | |
+ cmp_deeply $match->params => $block->params, "params ($name)"; | |
+}; | |
+ | |
+__END__ | |
+=== / | |
+--- path : / | |
+--- params: { controller => 'Root', action => 'index' } | |
+--- request: { path => '/' } | |
+ | |
+=== /account/login | |
+--- path : /account/login | |
+--- params: { controller => 'Account', action => 'login' } | |
+--- request: { path => '/account/login' } | |
diff -ruN router-anymoose/t/route/uri_for.t router-caf/t/route/uri_for.t | |
--- router-anymoose/t/route/uri_for.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/route/uri_for.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,45 @@ | |
+use Test::Base; | |
+use HTTP::Router::Route; | |
+ | |
+plan tests => 1 * blocks; | |
+ | |
+filters { | |
+ map { $_ => ['eval'] } qw(conditions args) | |
+}; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $name = $block->name; | |
+ my $route = HTTP::Router::Route->new( | |
+ path => $block->path, | |
+ conditions => $block->conditions || {}, | |
+ ); | |
+ | |
+ is $route->uri_for($block->args) => $block->uri, "uri_for ($name)"; | |
+}; | |
+ | |
+__END__ | |
+=== path | |
+--- path: / | |
+--- uri : / | |
+ | |
+=== path with args | |
+--- path: / | |
+--- uri : / | |
+--- args: { year => 2008 } | |
+ | |
+=== captures | |
+--- path: /archives/{year}/{month} | |
+--- uri : /archives/2008/Dec | |
+--- args: { year => 2008, month => 'Dec' } | |
+ | |
+=== captures and conditions | |
+--- path: /archives/{year}/{month} | |
+--- conditions: { year => qr/^\d{4}$/, month => qr/^\d{1,2}$/ } | |
+--- uri : /archives/2008/12 | |
+--- args: { year => 2008, month => 12 } | |
+ | |
+=== invalid conditions - undef | |
+--- path: /archives/{year}/{month} | |
+--- conditions: { year => qr/^\d{4}$/, month => qr/^\d{1,2}$/ } | |
+--- args: { year => '08', month => 'Dec' } | |
diff -ruN router-anymoose/t/router/match-block.t router-caf/t/router/match-block.t | |
--- router-anymoose/t/router/match-block.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/router/match-block.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,40 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(params conditions request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->match('/account', { method => 'GET' }, sub { | |
+ while (my $block = next_block) { | |
+ $_->match($block->path, $block->conditions)->to($block->params); | |
+ } | |
+ }); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => blocks; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== | |
+--- path: /login | |
+--- params: { name => 'login' } | |
+--- request: { path => '/account/login', method => 'GET' } | |
+--- results: { name => 'login' } | |
+ | |
+=== | |
+--- path: /logout | |
+--- params: { name => 'logout' } | |
+--- request: { path => '/account/logout', method => 'GET' } | |
+--- results: { name => 'logout' } | |
diff -ruN router-anymoose/t/router/match.t router-caf/t/router/match.t | |
--- router-anymoose/t/router/match.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/router/match.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,53 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(params conditions request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ while (my $block = next_block) { | |
+ $_->match($block->path, $block->conditions)->to($block->params); | |
+ } | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => blocks; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== | |
+--- path: / | |
+--- params: { name => 'root' } | |
+--- request: { path => '/' } | |
+--- results: { name => 'root' } | |
+ | |
+=== | |
+--- path: /home | |
+--- conditions: { method => 'GET' } | |
+--- params: { name => 'home' } | |
+--- request: { path => '/home', method => 'GET' } | |
+--- results: { name => 'home' } | |
+ | |
+=== | |
+--- path: /archives/{year} | |
+--- conditions: { year => qr/^\d{4}$/ } | |
+--- params: { name => 'archives' } | |
+--- request: { path => '/archives/2008' } | |
+--- results: { name => 'archives', year => 2008 } | |
+ | |
+=== | |
+--- path: /users/{user_id} | |
+--- conditions: { user_id => qr/^\d+$/, method => 'GET' } | |
+--- params: { name => 'users' } | |
+--- request: { path => '/users/100', method => 'GET' } | |
+--- results: { name => 'users', user_id => 100 } | |
diff -ruN router-anymoose/t/router/register.t router-caf/t/router/register.t | |
--- router-anymoose/t/router/register.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/router/register.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,33 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->match('/{controller}/{action}/{id}.{format}')->register; | |
+ $_->match('/{controller}/{action}/{id}')->register; | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => blocks; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== | |
+--- request: { path => '/foo/bar/baz' } | |
+--- results: { controller => 'foo', action => 'bar', id => 'baz' } | |
+ | |
+=== | |
+--- request: { path => '/foo/bar/baz.html' } | |
+--- results: { controller => 'foo', action => 'bar', id => 'baz', format => 'html' } | |
diff -ruN router-anymoose/t/router/to-block.t router-caf/t/router/to-block.t | |
--- router-anymoose/t/router/to-block.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/router/to-block.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,40 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(params conditions request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->match('/account', { method => 'GET' })->to({ controller => 'account' }, sub { | |
+ while (my $block = next_block) { | |
+ $_->match($block->path, $block->conditions)->to($block->params); | |
+ } | |
+ }); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => blocks; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== | |
+--- path: /login | |
+--- params: { action => 'login' } | |
+--- request: { path => '/account/login', method => 'GET' } | |
+--- results: { controller => 'account', action => 'login' } | |
+ | |
+=== | |
+--- path: /logout | |
+--- params: { action => 'logout' } | |
+--- request: { path => '/account/logout', method => 'GET' } | |
+--- results: { controller => 'account', action => 'logout' } | |
diff -ruN router-anymoose/t/router/with.t router-caf/t/router/with.t | |
--- router-anymoose/t/router/with.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/t/router/with.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,46 @@ | |
+use Test::Base; | |
+use Test::Deep; | |
+use t::Router; | |
+use HTTP::Router; | |
+ | |
+plan tests => 1 + 2*blocks; | |
+ | |
+filters { map { $_ => ['eval'] } qw(params conditions request results) }; | |
+ | |
+my $router = HTTP::Router->define(sub { | |
+ $_->with({ controller => 'users' }, sub { | |
+ while (my $block = next_block) { | |
+ $_->match($block->path, $block->conditions)->to($block->params); | |
+ } | |
+ }); | |
+}); | |
+ | |
+is scalar @{[ $router->routes ]} => blocks; | |
+ | |
+run { | |
+ my $block = shift; | |
+ my $req = create_request($block->request); | |
+ | |
+ my $match = $router->match($req); | |
+ ok $match; | |
+ cmp_deeply $match->params => $block->results; | |
+}; | |
+ | |
+__END__ | |
+=== | |
+--- path: /login | |
+--- params: { action => 'login' } | |
+--- request: { path => '/login' } | |
+--- results: { controller => 'users', action => 'login' } | |
+ | |
+=== | |
+--- path: /logout | |
+--- params: { action => 'logout' } | |
+--- request: { path => '/logout' } | |
+--- results: { controller => 'users', action => 'logout' } | |
+ | |
+=== | |
+--- path: /signup | |
+--- params: { action => 'signup' } | |
+--- request: { path => '/signup' } | |
+--- results: { controller => 'users', action => 'signup' } | |
diff -ruN router-anymoose/tools/benchmark_define.pl router-caf/tools/benchmark_define.pl | |
--- router-anymoose/tools/benchmark_define.pl 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/tools/benchmark_define.pl 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,53 +0,0 @@ | |
-#!/usr/bin/env perl | |
-use strict; | |
-use warnings; | |
-use Benchmark qw(:hireswallclock timethese); | |
-use Test::MockObject; | |
-use FindBin::libs; | |
-use HTTP::Router::Declare; | |
- | |
-timethese(10000, { | |
- match_to => \&_match_to, | |
- path_then => \&_path_then, | |
- conds_then => \&_conds_then, | |
- to_then => \&_to_then, | |
- with => \&_with, | |
-}); | |
- | |
-sub _match_to { | |
- router { | |
- match '/' => to { controller => 'Root', action => 'index' }; | |
- }; | |
-} | |
- | |
-sub _path_then { | |
- router { | |
- match '/account' => then { | |
- match '/login', { method => 'POST' } => to { controller => 'Account', action => 'login' }; | |
- }; | |
- }; | |
-} | |
- | |
-sub _conds_then { | |
- router { | |
- match { method => 'POST' } => then { | |
- match '/account/login' => to { controller => 'Account', action => 'login' }; | |
- }; | |
- }; | |
-} | |
- | |
-sub _to_then { | |
- router { | |
- match '/account' => to { controller => 'account' } => then { | |
- match '/login', { method => 'POST' } => to { action => 'login' }; | |
- }; | |
- }; | |
-} | |
- | |
-sub _with { | |
- router { | |
- with { controller => '/account' } => then { | |
- match '/account/login', { method => 'POST' } => to { action => 'login' }; | |
- }; | |
- }; | |
-} | |
diff -ruN router-anymoose/tools/benchmark_match.pl router-caf/tools/benchmark_match.pl | |
--- router-anymoose/tools/benchmark_match.pl 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/tools/benchmark_match.pl 1970-01-01 09:00:00.000000000 +0900 | |
@@ -1,41 +0,0 @@ | |
-#!/usr/bin/env perl | |
- | |
-use strict; | |
-use warnings; | |
-use Benchmark qw(:hireswallclock timethese); | |
-use Test::MockObject; | |
-use FindBin::libs; | |
-use HTTP::Router::Declare; | |
- | |
-my $router = router { | |
- match '/' => to { controller => 'Root', action => 'index' }; | |
- match '/account/login', { method => 'POST' } => to { controller => 'Account', action => 'login' }; | |
- match '/archives/{year}', { year => qr/^\d{4}$/ } => to { controller => 'Archives', action => 'by_year' }; | |
- resources 'Users'; | |
- resource 'Admin'; | |
-}; | |
- | |
-sub request { | |
- my $r = Test::MockObject->new; | |
- my %p = @_; | |
- while (my ($k, $v) = each %p) { | |
- $r->set_always($k, $v); | |
- } | |
- $r; | |
-} | |
- | |
-my $path_request = request(path => '/'); | |
-my $conditions_request = request(path => '/account/login', method => 'POST'); | |
-my $validate_request = request(path => '/archives/2009'); | |
-my $resources_request = request(path => '/users/new', method => 'GET'); | |
-my $resource_request = request(path => '/admin/edit', method => 'GET'); | |
-my $var_resources = request(path => '/users/masaki', method => 'GET'); | |
- | |
-timethese(10000, { | |
- path => sub { $router->match($path_request) }, | |
- conditions => sub { $router->match($conditions_request) }, | |
- validate => sub { $router->match($validate_request) }, | |
- resources => sub { $router->match($resources_request) }, | |
- resource => sub { $router->match($resource_request) }, | |
- var_resources => sub { $router->match($var_resources) }, | |
-}); | |
diff -ruN router-anymoose/tools/benchmark_router.pl router-caf/tools/benchmark_router.pl | |
--- router-anymoose/tools/benchmark_router.pl 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/tools/benchmark_router.pl 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,69 @@ | |
+#!/usr/bin/env perl | |
+use strict; | |
+use warnings; | |
+use Benchmark qw(:hireswallclock timethese); | |
+use Test::MockObject; | |
+use FindBin::libs; | |
+use HTTP::Router; | |
+ | |
+timethese(10000, { | |
+ match => \&match, | |
+ match_block => \&match_block, | |
+ to_block => \&to_block, | |
+ with => \&with, | |
+}); | |
+ | |
+sub req { | |
+ my $params = ref $_[0] ? shift : { @_ }; | |
+ my $req = Test::MockObject->new; | |
+ while (my ($name, $value) = each %$params) { | |
+ $req->set_always($name, $value); | |
+ } | |
+ $req; | |
+} | |
+ | |
+sub match { | |
+ my $router = HTTP::Router->define(sub { | |
+ $_[0]->match('/')->to({ action => 'index' }); | |
+ $_[0]->match('/{year}', { year => qr/^\d{4}$/ })->to({ action => 'year' }); | |
+ }); | |
+ | |
+ $router->match(req(path => '/')); | |
+ $router->match(req(path => '/2009')); | |
+} | |
+ | |
+sub match_block { | |
+ my $router = HTTP::Router->define(sub { | |
+ $_[0]->match('/account', sub { | |
+ $_[0]->match('/login', { method => 'POST' })->to({ action => 'login' }); | |
+ $_[0]->match('/logout', { method => 'GET' })->to({ action => 'logout' }); | |
+ }); | |
+ }); | |
+ | |
+ $router->match(req(path => '/account/login', method => 'POST')); | |
+ $router->match(req(path => '/account/logout', method => 'GET' )); | |
+} | |
+ | |
+sub to_block { | |
+ my $router = HTTP::Router->define(sub { | |
+ $_[0]->match('/account')->to({ controller => 'account' }, sub { | |
+ $_[0]->match('/login', { method => 'POST' })->to({ action => 'login' }); | |
+ $_[0]->match('/logout', { method => 'GET' })->to({ action => 'logout' }); | |
+ }); | |
+ }); | |
+ | |
+ $router->match(req(path => '/account/login', method => 'POST')); | |
+ $router->match(req(path => '/account/logout', method => 'GET' )); | |
+} | |
+ | |
+sub with { | |
+ my $router = HTTP::Router->define(sub { | |
+ $_[0]->with({ controller => '/account' }, sub { | |
+ $_[0]->match('/login', { method => 'POST' })->to({ action => 'login' }); | |
+ $_[0]->match('/logout', { method => 'GET' })->to({ action => 'logout' }); | |
+ }); | |
+ }); | |
+ | |
+ $router->match(req(path => '/login', method => 'POST')); | |
+ $router->match(req(path => '/logout', method => 'GET' )); | |
+} | |
diff -ruN router-anymoose/tools/profile.pl router-caf/tools/profile.pl | |
--- router-anymoose/tools/profile.pl 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/tools/profile.pl 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,7 +1,7 @@ | |
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
-use HTTP::Router::Declare; | |
+use HTTP::Router; | |
use Getopt::Long; | |
use Pod::Usage; | |
use Test::MockObject; | |
@@ -39,7 +39,11 @@ | |
} | |
sub create_router { | |
- router { | |
- match '/' => to { controller => 'Root', action => 'index' }; | |
- }; | |
+ HTTP::Router->define( | |
+ sub { | |
+ $_->match('/')->to( { controller => 'Root', action => 'index' } ); | |
+ } | |
+ ); | |
} | |
+ | |
+ | |
diff -ruN router-anymoose/xt/02_podcoverage.t router-caf/xt/02_podcoverage.t | |
--- router-anymoose/xt/02_podcoverage.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/xt/02_podcoverage.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -1,10 +1,4 @@ | |
use Test::More; | |
eval "use Test::Pod::Coverage 1.04"; | |
plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; | |
-all_pod_coverage_ok({ | |
- also_private => [qw( | |
- create_match create_with create_resources create_resource | |
- routing | |
- to_request | |
- )] | |
-}); | |
+all_pod_coverage_ok(); | |
diff -ruN router-anymoose/xt/03_podspell.t router-caf/xt/03_podspell.t | |
--- router-anymoose/xt/03_podspell.t 2010-02-23 16:13:48.000000000 +0900 | |
+++ router-caf/xt/03_podspell.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -21,5 +21,3 @@ | |
Takatoshi Kitano | |
HTTP::Router | |
Merb | |
-params | |
- | |
diff -ruN router-anymoose/xt/04_dependencies.t router-caf/xt/04_dependencies.t | |
--- router-anymoose/xt/04_dependencies.t 1970-01-01 09:00:00.000000000 +0900 | |
+++ router-caf/xt/04_dependencies.t 2010-02-23 16:15:20.000000000 +0900 | |
@@ -0,0 +1,4 @@ | |
+use Test::More; | |
+eval "use Test::Dependencies exclude => ['HTTP::Router']"; | |
+plan skip_all => "Test::Dependencies required for testing dependencies" if $@; | |
+ok_dependencies(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment