7 use DBIx::Class::_Util 'sigwarn_silencer';
11 package TestPackage::A;
15 my $schema = DBICTest->init_schema();
19 # Test ensure_class_found
20 ok( $schema->ensure_class_found('DBIx::Class::Schema'),
21 'loaded package DBIx::Class::Schema was found' );
22 ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
23 'DBICTest::FakeComponent not loaded yet' );
24 ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
25 'package DBICTest::FakeComponent was found' );
26 ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
27 'DBICTest::FakeComponent not loaded by ensure_class_found()' );
28 ok( $schema->ensure_class_found('TestPackage::A'),
29 'anonymous package TestPackage::A found' );
30 ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
31 'fake package not found' );
33 # Test load_optional_class
34 my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
35 ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
36 ok( !$retval, 'nonexistent package not loaded' );
37 $retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
38 ok( !$@, 'load_optional_class on an existing class did not throw' );
39 ok( $retval, 'DBICTest::OptionalComponent loaded' );
40 eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
41 like( $@, qr/did not return a true value/,
42 'DBICTest::ErrorComponent threw ok' );
44 # Simulate a PAR environment
49 if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') {
50 return (sub { return 0 unless @code; $_ = shift @code; 1; } );
57 $retval = eval { $schema->load_optional_class('FAKE::PAR::PACKAGE') };
58 ok( !$@, 'load_optional_class on a nonexistent PAR class did not throw' );
59 ok( !$retval, 'nonexistent PAR package not loaded' );
62 # simulate a class which does load but does not return true
64 q/package VIRTUAL::PAR::PACKAGE;/,
68 $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
69 ok( $@, 'load_optional_class of a no-true-returning PAR module did throw' );
70 ok( !$retval, 'no-true-returning PAR package not loaded' );
72 # simulate a normal class (no one adjusted %INC so it will be tried again
74 q/package VIRTUAL::PAR::PACKAGE;/,
78 $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
79 ok( !$@, 'load_optional_class of a PAR module did not throw' );
80 ok( $retval, 'PAR package "loaded"' );
82 # see if we can still load stuff with the coderef present
83 $retval = eval { $schema->load_optional_class('DBIx::Class::ResultClass::HashRefInflator') };
84 ok( !$@, 'load_optional_class did not throw' ) || diag $@;
85 ok( $retval, 'DBIx::Class::ResultClass::HashRefInflator loaded' );
88 # Test ensure_class_loaded
89 ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
90 eval { $schema->ensure_class_loaded('TestPackage::A'); };
91 ok( !$@, 'ensure_class_loaded detected an anon. class' );
92 eval { $schema->ensure_class_loaded('FakePackage::B'); };
93 like( $@, qr/Can't locate/,
94 'ensure_class_loaded threw exception for nonexistent class' );
95 ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
96 'DBICTest::FakeComponent not loaded yet' );
97 eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
98 ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
99 ok( Class::Inspector->loaded('DBICTest::FakeComponent'),
100 'DBICTest::FakeComponent now loaded' );
103 # Squash warnings about syntax errors in SytaxErrorComponent.pm
104 local $SIG{__WARN__} = sigwarn_silencer(
105 qr/String found where operator expected|Missing operator before/
108 eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent1') };
109 like( $@, qr/syntax error/,
110 'ensure_class_loaded(DBICTest::SyntaxErrorComponent1) threw ok' );
111 eval { $schema->load_optional_class('DBICTest::SyntaxErrorComponent2') };
112 like( $@, qr/syntax error/,
113 'load_optional_class(DBICTest::SyntaxErrorComponent2) threw ok' );
118 package Fake::ResultSet;
120 use base 'DBIx::Class::ResultSet';
122 __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3');
125 # Make sure the errors in components of resultset classes are reported right.
126 like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right");