X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=t%2Funit_core_mvc.t;h=0953e2cb338de4c5fe63ab2f346d31f1a8a1bd6e;hp=d79653e8c0265d37c3e1f2e7567735ccdf726aa5;hb=4ac0b9cb8e9043db8a95f44af685c782bf9426e7;hpb=3b88a4556575f18382261e47214bb281694d8f16 diff --git a/t/unit_core_mvc.t b/t/unit_core_mvc.t index d79653e..0953e2c 100644 --- a/t/unit_core_mvc.t +++ b/t/unit_core_mvc.t @@ -1,4 +1,4 @@ -use Test::More tests => 13; +use Test::More tests => 45; use strict; use warnings; @@ -18,6 +18,9 @@ push @complist,$thingie; use base qw/Catalyst/; __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } ); + + # allow $c->log->warn to work + __PACKAGE__->setup_log; } is( MyApp->view('View'), 'MyApp::V::View', 'V::View ok' ); @@ -39,6 +42,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' ); @@ -50,3 +58,120 @@ is_deeply( [ sort MyApp->controllers ], is_deeply( [ sort MyApp->models ], [ qw/Dummy::Model M Model Test::Object/ ], 'models 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'); + +my $view = bless {} , 'MyApp::View::V'; +is ( bless ({stash=>{current_view_instance=> $view }}, 'MyApp')->view , $view, 'current_view_instance ok'); + +is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyApp::V::View' }}, 'MyApp')->view , $view, + 'current_view_instance precedes current_view ok'); + +{ + my $warnings = 0; + no warnings 'redefine'; + local *Catalyst::Log::warn = sub { $warnings++ }; + + like (MyApp->model , qr/^MyApp\::(M|Model)\::/ , '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'); + +my $model = bless {} , 'MyApp::Model::M'; +is ( bless ({stash=>{current_model_instance=> $model }}, 'MyApp')->model , $model, 'current_model_instance ok'); + +is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyApp::M::Model' }}, 'MyApp')->model , $model, + 'current_model_instance precedes current_model ok'); + +MyApp->config->{default_view} = 'V'; +is ( bless ({stash=>{}}, 'MyApp')->view , 'MyApp::View::V', 'default_view ok'); +is ( MyApp->view , 'MyApp::View::V', 'default_view in class method ok'); + +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 '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'); + + +}