making unit_core_component.t work
André Walker [Tue, 12 Jul 2011 19:58:48 +0000 (16:58 -0300)]
lib/Catalyst.pm
t/aggregate/unit_core_component.t

index e6a9f4d..352f762 100644 (file)
@@ -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;
index 2875f0c..005015a 100644 (file)
@@ -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' );