Tests, fix args as well as captureargs and be less violent - only encode /
[catagits/Catalyst-Runtime.git] / t / unit_core_mvc.t
index 549d758..b5bbd8b 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests => 44;
+use Test::More tests => 51;
 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' );
 }
 
@@ -150,17 +156,72 @@ is ( MyApp->model , 'MyApp::Model::M', 'default_model in class method ok');
 {
     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};
+    {
+        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 = MyApp->view('V', qw/foo2 bar2/);
+    my $x = $c->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/);
+    $c->view('::View::V', qw/foo3 bar3/);
     is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok');
+
+
+}
+
+{
+    my $warn = '';
+    no warnings 'redefine';
+    local *Catalyst::Log::warn = sub { $warn .= $_[1] };
+
+    is_deeply (MyApp->controller('MyApp::Controller::C'),
+        MyApp->components->{'MyApp::Controller::C'},
+        'controller by fully qualified name ok');
+
+    # You probably meant $c->controller('C') instead of $c->controller({'MyApp::Controller::C'})
+    my ($suggested_comp_name, $orig_comp_name) = $warn =~ /You probably meant (.*) instead of (.*) /;
+    isnt($suggested_comp_name, $orig_comp_name, 'suggested fix in warning for fully qualified component names makes sense' );
+}
+
+{
+    package MyApp::WithoutRegexFallback;
+
+    use base qw/Catalyst/;
+
+    __PACKAGE__->config( { disable_component_resolution_regex_fallback => 1 } );
+
+    __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) }
+        qw/MyApp::WithoutRegexFallback::Controller::Another::Foo/ } );
+
+    # allow $c->log->warn to work
+    __PACKAGE__->setup_log;
+}
+
+{
+    # test if non-regex component retrieval still works
+    is( MyApp::WithoutRegexFallback->controller('Another::Foo'),
+        'MyApp::WithoutRegexFallback::Controller::Another::Foo', 'controller Another::Foo found');
+}
+
+{
+    my $warnings = 0;
+    no warnings 'redefine';
+    local *Catalyst::Log::warn = sub { $warnings++ };
+
+    # try to get nonexisting object w/o regexp fallback
+    is( MyApp::WithoutRegexFallback->controller('Foo'), undef, 'no controller Foo found');
+    ok( !$warnings, 'no regexp fallback warnings' );
 }