r12983@zaphod: kd | 2008-04-28 18:10:27 +1000
[catagits/Catalyst-Runtime.git] / t / unit_core_mvc.t
index 0dbbd80..549d758 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests => 27;
+use Test::More tests => 44;
 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' );
@@ -51,7 +59,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 +76,14 @@ 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++ };
+
+    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');
 
@@ -79,14 +101,66 @@ 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;
 {
+    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');
+
+    MyApp->model('M', qw/foo bar/);
+    is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok');
+
+    my $x = MyApp->view('V', qw/foo2 bar2/);
+    is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok');
+
+    # regexp fallback
+    MyApp->view('::View::V', qw/foo3 bar3/);
+    is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
+}