[Bast-commits] r5942 - in Class-C3-Componentised/trunk:
lib/Class/C3 t t/lib/MyModule
arcanez at dev.catalyst.perl.org
arcanez at dev.catalyst.perl.org
Tue Apr 21 23:53:04 GMT 2009
Author: arcanez
Date: 2009-04-22 00:53:04 +0100 (Wed, 22 Apr 2009)
New Revision: 5942
Added:
Class-C3-Componentised/trunk/t/lib/MyModule/ErrorComponent.pm
Modified:
Class-C3-Componentised/trunk/lib/Class/C3/Componentised.pm
Class-C3-Componentised/trunk/t/01-basic.t
Log:
move tests over and fix up load_optional_class
Modified: Class-C3-Componentised/trunk/lib/Class/C3/Componentised.pm
===================================================================
--- Class-C3-Componentised/trunk/lib/Class/C3/Componentised.pm 2009-04-21 21:29:24 UTC (rev 5941)
+++ Class-C3-Componentised/trunk/lib/Class/C3/Componentised.pm 2009-04-21 23:53:04 UTC (rev 5942)
@@ -178,15 +178,22 @@
sub load_optional_class {
my ($class, $f_class) = @_;
- if ($class->ensure_class_found($f_class)) {
- eval { $class->ensure_class_loaded($f_class) };
- croak "Failed to load $f_class: $@" if $@;
+ eval { $class->ensure_class_loaded($f_class) };
+ my $err = $@; # so we don't lose it
+ if (! $err) {
return 1;
}
- return 0;
+ else {
+ my $fn = (join ('/', split ('::', $f_class) ) ) . '.pm';
+ if ($err =~ /Can't locate ${fn} in \@INC/ ) {
+ return 0;
+ }
+ else {
+ die $err;
+ }
+ }
}
-
=head1 AUTHOR
Matt S. Trout and the DBIx::Class team
Modified: Class-C3-Componentised/trunk/t/01-basic.t
===================================================================
--- Class-C3-Componentised/trunk/t/01-basic.t 2009-04-21 21:29:24 UTC (rev 5941)
+++ Class-C3-Componentised/trunk/t/01-basic.t 2009-04-21 23:53:04 UTC (rev 5942)
@@ -4,11 +4,16 @@
use FindBin;
use Test::More;
use Test::Exception;
+use Class::Inspector;
use lib "$FindBin::Bin/lib";
+plan tests => 22;
-plan tests => 6;
+BEGIN {
+ package TestPackage::A;
+ sub some_method {}
+}
use_ok('MyModule');
@@ -23,7 +28,70 @@
is(MyModule->new->message, "Foo MyModule", "it worked");
-is(MyModule->load_optional_class('ClassC3ComponentFooThatShouldntExist'), 0, "load_optional_class NonexistantClass returned false");
-is(MyModule->load_optional_class('MyModule::Plugin::Foo'), 1, "load_optional_class MyModule::Plugin::Foo (previously loaded module) returned true");
-is(MyModule->load_optional_class('MyModule::OwnComponent'), 1, "load_optional_class MyModule::OwnComponent (not previously loaded module) returned true");
+ok( MyModule->ensure_class_found('MyModule::Plugin::Foo'),
+ 'loaded package MyModule::Plugin::Foo was found' );
+ok( !Class::Inspector->loaded('MyModule::OwnComponent'),
+ 'MyModule::OwnComponent not loaded yet' );
+ok( MyModule->ensure_class_found('MyModule::OwnComponent'),
+ 'package MyModule::OwnComponent was found' );
+ok( !Class::Inspector->loaded('MyModule::OwnComponent'),
+ 'MyModule::OwnComponent not loaded by ensure_class_found()' );
+ok( MyModule->ensure_class_found('TestPackage::A'),
+ 'anonymous package TestPackage::A found' );
+ok( !MyModule->ensure_class_found('FAKE::WONT::BE::FOUND'),
+ 'fake package not found' );
+# Test load_optional_class
+my $retval = eval { MyModule->load_optional_class('ANOTHER::FAKE::PACKAGE') };
+ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
+ok( !$retval, 'nonexistent package not loaded' );
+$retval = eval { MyModule->load_optional_class('MyModule::OwnComponent') };
+ok( !$@, 'load_optional_class on an existing class did not throw' );
+ok( $retval, 'MyModule::OwnComponent loaded' );
+eval { MyModule->load_optional_class('MyModule::ErrorComponent') };
+like( $@, qr/did not return a true value/,
+ 'MyModule::ErrorComponent threw ok' );
+
+# Simulate a PAR environment
+{
+ my @code;
+ local @INC = @INC;
+ unshift @INC, sub {
+ if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') {
+ return (sub { return 0 unless @code; $_ = shift @code; 1; } );
+ }
+ else {
+ return ();
+ }
+ };
+
+ $retval = eval { MyModule->load_optional_class('FAKE::PAR::PACKAGE') };
+ ok( !$@, 'load_optional_class on a nonexistent PAR class did not throw' );
+ ok( !$retval, 'nonexistent PAR package not loaded' );
+
+
+ # simulate a class which does load but does not return true
+ @code = (
+ q/package VIRTUAL::PAR::PACKAGE;/,
+ q/0;/,
+ );
+
+ $retval = eval { MyModule->load_optional_class('VIRTUAL::PAR::PACKAGE') };
+ ok( $@, 'load_optional_class of a no-true-returning PAR module did throw' );
+ ok( !$retval, 'no-true-returning PAR package not loaded' );
+
+ # simulate a normal class (no one adjusted %INC so it will be tried again
+ @code = (
+ q/package VIRTUAL::PAR::PACKAGE;/,
+ q/1;/,
+ );
+
+ $retval = eval { MyModule->load_optional_class('VIRTUAL::PAR::PACKAGE') };
+ ok( !$@, 'load_optional_class of a PAR module did not throw' );
+ ok( $retval, 'PAR package "loaded"' );
+
+ # see if we can still load stuff with the coderef present
+ $retval = eval { MyModule->load_optional_class('Class::C3') };
+ ok( !$@, 'load_optional_class did not throw' ) || diag $@;
+ ok( $retval, 'Class::C3 loaded' );
+}
Added: Class-C3-Componentised/trunk/t/lib/MyModule/ErrorComponent.pm
===================================================================
--- Class-C3-Componentised/trunk/t/lib/MyModule/ErrorComponent.pm (rev 0)
+++ Class-C3-Componentised/trunk/t/lib/MyModule/ErrorComponent.pm 2009-04-21 23:53:04 UTC (rev 5942)
@@ -0,0 +1,8 @@
+package # hide from PAUSE
+ MyModule::ErrorComponent;
+use warnings;
+use strict;
+
+# this is missing on purpose
+# 1;
+
More information about the Bast-commits
mailing list