X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Funit_core_mvc.t;h=8cb1fcb3a3d64edcebde54505c132979dc468701;hb=a2aac3b8867dea286d03eba07a6fbe2e237cf1ae;hp=0dbbd80df6a815af9915016da2796fd99c81409f;hpb=c7ded7aaf69e506924a5406349fd665c7717acb8;p=catagits%2FCatalyst-Runtime.git diff --git a/t/unit_core_mvc.t b/t/unit_core_mvc.t index 0dbbd80..8cb1fcb 100644 --- a/t/unit_core_mvc.t +++ b/t/unit_core_mvc.t @@ -1,4 +1,4 @@ -use Test::More tests => 27; +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; @@ -18,6 +15,13 @@ push @complist,$thingie; use base qw/Catalyst/; __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; } is( MyApp->view('View'), 'MyApp::V::View', 'V::View ok' ); @@ -29,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' ); @@ -39,6 +43,11 @@ is( MyApp->controller('C'), 'MyApp::Controller::C', 'Controller::C ok' ); is( MyApp->model('M'), 'MyApp::Model::M', 'Model::M ok' ); +# failed search +{ + is( MyApp->model('DNE'), undef, 'undef for invalid search' ); +} + is_deeply( [ sort MyApp->views ], [ qw/V View/ ], 'views ok' ); @@ -51,7 +60,14 @@ is_deeply( [ sort MyApp->models ], [ qw/Dummy::Model M Model Test::Object/ ], 'models ok'); -is (MyApp->view , 'MyApp::V::View', 'view() with no defaults ok'); +{ + my $warnings = 0; + no warnings 'redefine'; + local *Catalyst::Log::warn = sub { $warnings++ }; + + like (MyApp->view , qr/^MyApp\::(V|View)\::/ , 'view() with no defaults returns *something*'); + ok( $warnings, 'view() w/o a default is random, warnings thrown' ); +} is ( bless ({stash=>{current_view=>'V'}}, 'MyApp')->view , 'MyApp::View::V', 'current_view ok'); @@ -61,7 +77,19 @@ is ( bless ({stash=>{current_view_instance=> $view }}, 'MyApp')->view , $view, ' is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyApp::V::View' }}, 'MyApp')->view , $view, 'current_view_instance precedes current_view ok'); -is (MyApp->model , 'MyApp::M::Model', 'model() with no defaults ok'); +{ + my $warnings = 0; + no warnings 'redefine'; + local *Catalyst::Log::warn = sub { $warnings++ }; + + 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' ); +} is ( bless ({stash=>{current_model=>'M'}}, 'MyApp')->model , 'MyApp::Model::M', 'current_model ok'); @@ -79,14 +107,77 @@ MyApp->config->{default_model} = 'M'; is ( bless ({stash=>{}}, 'MyApp')->model , 'MyApp::Model::M', 'default_model ok'); is ( MyApp->model , 'MyApp::Model::M', 'default_model in class method ok'); +# regexp behavior tests +{ + # is_deeply is used because regexp behavior means list context + is_deeply( [ MyApp->view( qr{^V[ie]+w$} ) ], [ 'MyApp::V::View' ], 'regexp view ok' ); + is_deeply( [ MyApp->controller( qr{Dummy\::Model$} ) ], [ 'MyApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' ); + is_deeply( [ MyApp->model( qr{Dum{2}y} ) ], [ 'MyApp::Model::Dummy::Model' ], 'regexp model ok' ); + + # object w/ qr{} + is_deeply( [ MyApp->model( qr{Test} ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' ); + + { + my $warnings = 0; + no warnings 'redefine'; + local *Catalyst::Log::warn = sub { $warnings++ }; + + # object w/ regexp fallback + is_deeply( [ MyApp->model( 'Test' ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' ); + ok( $warnings, 'regexp fallback warnings' ); + } + + is_deeply( [ MyApp->view('MyApp::V::View$') ], [ 'MyApp::V::View' ], 'Explicit return ok'); + is_deeply( [ MyApp->controller('MyApp::C::Controller$') ], [ 'MyApp::C::Controller' ], 'Explicit return ok'); + is_deeply( [ MyApp->model('MyApp::M::Model$') ], [ 'MyApp::M::Model' ], 'Explicit return ok'); +} + +{ + my @expected = qw( MyApp::C::Controller MyApp::Controller::C ); + is_deeply( [ sort MyApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' ); +} + +{ + my @expected = qw( MyApp::V::View MyApp::View::V ); + is_deeply( [ sort MyApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' ); +} + +{ + my @expected = qw( MyApp::M::Model MyApp::Model::M ); + is_deeply( [ sort MyApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' ); +} + +# failed search +{ + is( scalar MyApp->controller( qr{DNE} ), 0, '0 results for failed search' ); +} + #checking @args passed to ACCEPT_CONTEXT -my $args; { - no warnings; - *MyApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; - *MyApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; -} -MyApp->model('M', qw/foo bar/); -is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok'); -MyApp->view('V', qw/baz moo/); -is_deeply($args, [qw/baz moo/], '$c->view args passed to ACCEPT_CONTEXT ok'); + my $args; + + { + no warnings 'once'; + *MyApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; + *MyApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; + } + + my $c = bless {}, 'MyApp'; + + # test accept-context with class rather than instance + MyApp->model('M', qw/foo bar/); + is_deeply($args, [qw/foo bar/], 'MyApp->model args passed to ACCEPT_CONTEXT ok'); + + + $c->model('M', qw/foo bar/); + is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok'); + + my $x = $c->view('V', qw/foo2 bar2/); + is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok'); + + # regexp fallback + $c->view('::View::V', qw/foo3 bar3/); + is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok'); + + +}