X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F90ensure_class_loaded.t;h=e933c00a370a97a7b28fc875b9d22c1bd17a5a45;hb=26aea721d0dd94fe9920efa3c01f081642399c28;hp=c901d06b465192847c0c7b0ab407f7d7a8f09b07;hpb=9d3d92abe4fefa68f63ba4c90eb60f9e899911d4;p=dbsrgits%2FDBIx-Class.git diff --git a/t/90ensure_class_loaded.t b/t/90ensure_class_loaded.t index c901d06..e933c00 100644 --- a/t/90ensure_class_loaded.t +++ b/t/90ensure_class_loaded.t @@ -1,9 +1,10 @@ use strict; -use warnings; +use warnings; use Test::More; use lib qw(t/lib); use DBICTest; +use DBIx::Class::_Util 'sigwarn_silencer'; use Class::Inspector; BEGIN { @@ -13,7 +14,7 @@ BEGIN { my $schema = DBICTest->init_schema(); -plan tests => 19; +plan tests => 28; # Test ensure_class_found ok( $schema->ensure_class_found('DBIx::Class::Schema'), @@ -40,6 +41,50 @@ eval { $schema->load_optional_class('DBICTest::ErrorComponent') }; like( $@, qr/did not return a true value/, 'DBICTest::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 { $schema->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 { $schema->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 { $schema->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 { $schema->load_optional_class('DBIx::Class::ResultClass::HashRefInflator') }; + ok( !$@, 'load_optional_class did not throw' ) || diag $@; + ok( $retval, 'DBIx::Class::ResultClass::HashRefInflator loaded' ); +} + # Test ensure_class_loaded ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' ); eval { $schema->ensure_class_loaded('TestPackage::A'); }; @@ -56,13 +101,9 @@ ok( Class::Inspector->loaded('DBICTest::FakeComponent'), { # Squash warnings about syntax errors in SytaxErrorComponent.pm - local $SIG{__WARN__} = sub { - my $warning = shift; - warn $warning unless ( - $warning =~ /String found where operator expected/ or - $warning =~ /Missing operator before/ - ); - }; + local $SIG{__WARN__} = sigwarn_silencer( + qr/String found where operator expected|Missing operator before/ + ); eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent1') }; like( $@, qr/syntax error/, @@ -72,4 +113,16 @@ ok( Class::Inspector->loaded('DBICTest::FakeComponent'), 'load_optional_class(DBICTest::SyntaxErrorComponent2) threw ok' ); } + +eval { + package Fake::ResultSet; + + use base 'DBIx::Class::ResultSet'; + + __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3'); +}; + +# Make sure the errors in components of resultset classes are reported right. +like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right"); + 1;