From: Rafael Kitover Date: Thu, 11 Jun 2009 12:42:00 +0000 (+0000) Subject: fix $c->view() bug hopefully X-Git-Tag: 5.80006~55 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=c23b894b1e0dda68841e82cfd85219d2627e7ac2 fix $c->view() bug hopefully --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index dbc5fdf..549af88 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -493,8 +493,13 @@ sub clear_errors { $c->error(0); } -# search components given a name and some prefixes sub _comp_search_prefixes { + my $c = shift; + return map $c->components->{ $_ }, $c->_comp_names_search_prefixes(@_); +} + +# search components given a name and some prefixes +sub _comp_names_search_prefixes { my ( $c, $name, @prefixes ) = @_; my $appclass = ref $c || $c; my $filter = "^${appclass}::(" . join( '|', @prefixes ) . ')::'; @@ -510,18 +515,18 @@ sub _comp_search_prefixes { my $query = ref $name ? $name : qr/^$name$/i; my @result = grep { $eligible{$_} =~ m{$query} } keys %eligible; - return map { $c->components->{ $_ } } @result if @result; + return @result if @result; # if we were given a regexp to search against, we're done. return if ref $name; # regexp fallback $query = qr/$name/i; - @result = map { $c->components->{ $_ } } grep { $eligible{ $_ } =~ m{$query} } keys %eligible; + @result = grep { $eligible{ $_ } =~ m{$query} } keys %eligible; # no results? try against full names if( !@result ) { - @result = map { $c->components->{ $_ } } grep { m{$query} } keys %eligible; + @result = grep { m{$query} } keys %eligible; } # don't warn if we didn't find any results, it just might not exist @@ -558,7 +563,9 @@ sub _comp_names { my $filter = "^${appclass}::(" . join( '|', @prefixes ) . ')::'; - my @names = map { s{$filter}{}; $_; } $c->_comp_search_prefixes( undef, @prefixes ); + my @names = map { s{$filter}{}; $_; } + $c->_comp_names_search_prefixes( undef, @prefixes ); + return @names; } diff --git a/t/aggregate/live_component_view_single.t b/t/aggregate/live_component_view_single.t index 6c50c18..9396ffb 100644 --- a/t/aggregate/live_component_view_single.t +++ b/t/aggregate/live_component_view_single.t @@ -25,9 +25,11 @@ else { sub run_tests { { - is(get('/view_by_name?view=Dummy'), 'TestAppOneView::View::Dummy', + is(get('/view_by_name?view=Dummy'), 'AClass', '$c->view("name") returns blessed instance'); - is(get('/view_no_args'), 'TestAppOneView::View::Dummy', + is(get('/view_by_regex?view=Dummy'), 'AClass', + '$c->view(qr/name/) returns blessed instance'); + is(get('/view_no_args'), 'AClass', '$c->view() returns blessed instance'); } } diff --git a/t/lib/TestAppOneView/Controller/Root.pm b/t/lib/TestAppOneView/Controller/Root.pm index 1da451c..61a9bf5 100644 --- a/t/lib/TestAppOneView/Controller/Root.pm +++ b/t/lib/TestAppOneView/Controller/Root.pm @@ -21,4 +21,14 @@ sub view_by_name : Local { $c->res->body(Scalar::Util::blessed($v)); } +sub view_by_regex : Local { + my ( $self, $c ) = @_; + + my $v_name = $c->req->param('view'); + + my ($v) = $c->view(qr/$v_name/); + + $c->res->body(Scalar::Util::blessed($v)); +} + 1; diff --git a/t/lib/TestAppOneView/View/Dummy.pm b/t/lib/TestAppOneView/View/Dummy.pm index bba0855..c579995 100644 --- a/t/lib/TestAppOneView/View/Dummy.pm +++ b/t/lib/TestAppOneView/View/Dummy.pm @@ -2,4 +2,12 @@ package TestAppOneView::View::Dummy; use base 'Catalyst::View'; +sub COMPONENT { + bless {}, 'AClass' +} + +package AClass; + +use base 'Catalyst::View'; + 1; diff --git a/t/unit_core_mvc.t b/t/unit_core_mvc.t index 0953e2c..8cb1fcb 100644 --- a/t/unit_core_mvc.t +++ b/t/unit_core_mvc.t @@ -1,4 +1,4 @@ -use Test::More tests => 45; +use Test::More tests => 46; use strict; use warnings; @@ -8,9 +8,6 @@ my @complist = map { "MyApp::$_"; } qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/; -my $thingie={}; -bless $thingie,'MyApp::Model::Test::Object'; -push @complist,$thingie; { package MyApp; @@ -19,6 +16,10 @@ push @complist,$thingie; __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } ); + my $thingie={}; + bless $thingie, 'Some::Test::Object'; + __PACKAGE__->components->{'MyApp::Model::Test::Object'} = $thingie; + # allow $c->log->warn to work __PACKAGE__->setup_log; } @@ -32,7 +33,7 @@ is( MyApp->model('Model'), 'MyApp::M::Model', 'M::Model ok' ); is( MyApp->model('Dummy::Model'), 'MyApp::Model::Dummy::Model', 'Model::Dummy::Model ok' ); -isa_ok( MyApp->model('Test::Object'), 'MyApp::Model::Test::Object', 'Test::Object ok' ); +isa_ok( MyApp->model('Test::Object'), 'Some::Test::Object', 'Test::Object ok' ); is( MyApp->controller('Model::Dummy::Model'), 'MyApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' ); @@ -81,7 +82,12 @@ is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyApp::V::Vie no warnings 'redefine'; local *Catalyst::Log::warn = sub { $warnings++ }; - like (MyApp->model , qr/^MyApp\::(M|Model)\::/ , 'model() with no defaults returns *something*'); + ok( my $model = MyApp->model ); + + ok( (($model =~ /^MyApp\::(M|Model)\::/) || + $model->isa('Some::Test::Object')), + 'model() with no defaults returns *something*' ); + ok( $warnings, 'model() w/o a default is random, warnings thrown' ); }