From: Justin Hunter Date: Tue, 21 Apr 2009 23:53:04 +0000 (+0000) Subject: move tests over and fix up load_optional_class X-Git-Tag: v1.001000~23 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dfb3a821c9cfc269abe789ca9049f52e4111511b;p=p5sagit%2FClass-C3-Componentised.git move tests over and fix up load_optional_class --- diff --git a/lib/Class/C3/Componentised.pm b/lib/Class/C3/Componentised.pm index c216fe0..280dc06 100644 --- a/lib/Class/C3/Componentised.pm +++ b/lib/Class/C3/Componentised.pm @@ -178,15 +178,22 @@ successfully, and false if the class is not installed 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 diff --git a/t/01-basic.t b/t/01-basic.t index fea4c38..f655db8 100644 --- a/t/01-basic.t +++ b/t/01-basic.t @@ -4,11 +4,16 @@ use warnings; 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 @@ throws_ok { MyModule->load_components('+ClassC3ComponentFooThatShouldntExist'); 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' ); +} diff --git a/t/lib/MyModule/ErrorComponent.pm b/t/lib/MyModule/ErrorComponent.pm new file mode 100644 index 0000000..9f94b25 --- /dev/null +++ b/t/lib/MyModule/ErrorComponent.pm @@ -0,0 +1,8 @@ +package # hide from PAUSE + MyModule::ErrorComponent; +use warnings; +use strict; + +# this is missing on purpose +# 1; +