From: André Walker Date: Tue, 12 Jul 2011 19:58:48 +0000 (-0300) Subject: making unit_core_component.t work X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=f5dfb6ffcafcb88c7072329e7ec45fef6e04d301 making unit_core_component.t work --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index e6a9f4d..352f762 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -27,6 +27,7 @@ use URI::https; use Tree::Simple qw/use_weak_refs/; use Tree::Simple::Visitor::FindByUID; use Class::C3::Adopt::NEXT; +use List::Util qw/first/; use List::MoreUtils qw/uniq/; use attributes; use utf8; @@ -695,27 +696,42 @@ sub component { my ( $c, $component, @args ) = @_; if ( $component ) { - my ($type, $name) = _get_component_type_name($component); - - if ($type && $c->container->has_sub_container($type)) { - my $container = $c->container->get_sub_container($type); - - if( !ref $component && $container->has_service($name) ) { - return $container->get_component( $name, $c, @args ); - } - - return $container->get_component_regexp( $name, $c, @args ); - } - if (ref $component) { + my @result; for my $subcontainer_name (qw/model view controller/) { my $subcontainer = $c->container->get_sub_container($subcontainer_name); my @components = $subcontainer->get_service_list; - my @result = grep { m{$component} } @components; + @result = grep { m{$component} } @components; return map { $subcontainer->get_component( $_, $c, @args ) } @result if @result; } + + # it expects an empty list on failed searches + return @result; + } + else { + my ($type, $name) = _get_component_type_name($component); + + if ($type && $c->container->has_sub_container($type)) { + my $container = $c->container->get_sub_container($type); + + if ( !ref $component && $container->has_service($name) ) { + return $container->get_component( $name, $c, @args ); + } + + return $container->get_component_regexp( $name, $c, @args ); + } + else { + for my $subcontainer_name (qw/model view controller/) { + my $subcontainer = $c->container->get_sub_container($subcontainer_name); + my @components = $subcontainer->get_service_list; + my $result = first { $_ eq $component } @components; + + return $subcontainer->get_component( $result, $c, @args ) + if $result; + } + } } # FIXME: I probably shouldn't be doing this @@ -2388,10 +2404,17 @@ sub setup_components { $containers->{view}->make_single_default; } +# FIXME: should this sub exist? +# should it be moved to Catalyst::Utils, +# or replaced by something already existing there? sub _get_component_type_name { my $component = shift; my @parts = split /::/, $component; + if (scalar @parts == 1) { + return (undef, $component); + } + while (my $type = shift @parts) { return ('controller', join '::', @parts) if $type =~ /^(c|controller)$/i; diff --git a/t/aggregate/unit_core_component.t b/t/aggregate/unit_core_component.t index 2875f0c..005015a 100644 --- a/t/aggregate/unit_core_component.t +++ b/t/aggregate/unit_core_component.t @@ -45,6 +45,11 @@ is_deeply([ MyApp->comp('Foo') ], \@complist, 'Fallthrough return ok'); # multiple returns { +# FIXME: this cannot be found by looking only in the container. +# either the test must be changed, or the regexp must be run against +# $c->components() in Catalyst.pm + diag('this test will not work by searching the container'); + diag('check the source of this file for more info'); my @expected = sort qw( MyApp::C::Controller MyApp::M::Model ); my @got = sort MyApp->comp( qr{::[MC]::} ); is_deeply( \@got, \@expected, 'multiple results from regexp ok' );