From: John Napiorkowski Date: Tue, 17 Mar 2015 14:52:11 +0000 (-0500) Subject: support imported types (types-tiny, MXT) X-Git-Tag: 5.90089_002~43 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=842180f78ebf88d45517df4222c68057f2bea1ef;hp=b7791bd714d8a1d41f26bd3a371761ed8ab899cd support imported types (types-tiny, MXT) --- diff --git a/lib/Catalyst/Action.pm b/lib/Catalyst/Action.pm index 58693af..274ffd9 100644 --- a/lib/Catalyst/Action.pm +++ b/lib/Catalyst/Action.pm @@ -100,13 +100,19 @@ has args_constraints => ( return \@args; } else { @args = - map { Moose::Util::TypeConstraints::find_or_parse_type_constraint($_) || die "$_ is not a constraint!" } + map { $self->resolve_type_constraint($_) || die "$_ is not a constraint!" } @arg_protos; } return \@args; } +sub resolve_type_constraint { + my ($self, $name) = @_; + my $tc = eval "package ${\$self->class}; $name" || undef; + return $tc || Moose::Util::TypeConstraints::find_or_parse_type_constraint($name); +} + use overload ( # Stringify to reverse for debug output etc. diff --git a/t/arg_constraints.t b/t/arg_constraints.t index e4a2593..2a43a87 100644 --- a/t/arg_constraints.t +++ b/t/arg_constraints.t @@ -1,7 +1,12 @@ use warnings; use strict; -use Test::More; -use HTTP::Request::Common; + +BEGIN { + use Test::More; + eval "use Types::Standard; 1;" || do { + plan skip_all => "Trouble loading Types::Standard => $@"; + }; +} { package MyApp::Controller::Root; @@ -9,6 +14,7 @@ use HTTP::Request::Common; use Moose; use MooseX::MethodAttributes; + use Types::Standard qw/Tuple Int Str/; extends 'Catalyst::Controller'; @@ -22,7 +28,14 @@ use HTTP::Request::Common; $c->res->body('many_ints'); } + sub tuple :Local Args(Tuple[Str,Int]) { + my ($self, $c, $int) = @_; + $c->res->body('tuple'); + } + + sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') } + sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') } sub default :Default { @@ -75,10 +88,21 @@ use Catalyst::Test 'MyApp'; my $res = request '/priority_test/1'; is $res->content, 'int_priority'; } + { my $res = request '/priority_test/a'; is $res->content, 'any_priority'; } +{ + my $res = request '/tuple/aaa/111'; + is $res->content, 'tuple'; +} + +{ + my $res = request '/tuple/aaa/aaa'; + is $res->content, 'default'; +} + done_testing;