1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
9 use DBIx::Class::_Util 'sigwarn_silencer';
10 use DBICTest::Util 'class_seems_loaded';
13 package TestPackage::A;
17 my $schema = DBICTest->init_schema();
21 # Test ensure_class_found
22 ok( $schema->ensure_class_found('DBIx::Class::Schema'),
23 'loaded package DBIx::Class::Schema was found' );
24 ok( ! class_seems_loaded('DBICTest::FakeComponent'),
25 'DBICTest::FakeComponent not loaded yet' );
26 ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
27 'package DBICTest::FakeComponent was found' );
28 ok( ! class_seems_loaded('DBICTest::FakeComponent'),
29 'DBICTest::FakeComponent not loaded by ensure_class_found()' );
30 ok( $schema->ensure_class_found('TestPackage::A'),
31 'anonymous package TestPackage::A found' );
32 ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
33 'fake package not found' );
35 # Test load_optional_class
36 my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
37 ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
38 ok( !$retval, 'nonexistent package not loaded' );
39 $retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
40 ok( !$@, 'load_optional_class on an existing class did not throw' );
41 ok( $retval, 'DBICTest::OptionalComponent loaded' );
42 eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
43 like( $@, qr/did not return a true value/,
44 'DBICTest::ErrorComponent threw ok' );
46 # Simulate a PAR environment
51 if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') {
52 return (sub { return 0 unless @code; $_ = shift @code; 1; } );
59 $retval = eval { $schema->load_optional_class('FAKE::PAR::PACKAGE') };
60 ok( !$@, 'load_optional_class on a nonexistent PAR class did not throw' );
61 ok( !$retval, 'nonexistent PAR package not loaded' );
64 # simulate a class which does load but does not return true
66 q/package VIRTUAL::PAR::PACKAGE;/,
70 $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
71 ok( $@, 'load_optional_class of a no-true-returning PAR module did throw' );
72 ok( !$retval, 'no-true-returning PAR package not loaded' );
74 # simulate a normal class (no one adjusted %INC so it will be tried again
76 q/package VIRTUAL::PAR::PACKAGE;/,
80 $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
81 ok( !$@, 'load_optional_class of a PAR module did not throw' );
82 ok( $retval, 'PAR package "loaded"' );
84 # see if we can still load stuff with the coderef present
85 $retval = eval { $schema->load_optional_class('DBIx::Class::ResultClass::HashRefInflator') };
86 ok( !$@, 'load_optional_class did not throw' ) || diag $@;
87 ok( $retval, 'DBIx::Class::ResultClass::HashRefInflator loaded' );
90 # Test ensure_class_loaded
91 ok( class_seems_loaded('TestPackage::A'), 'anonymous package exists' );
92 eval { $schema->ensure_class_loaded('TestPackage::A'); };
93 ok( !$@, 'ensure_class_loaded detected an anon. class' );
94 eval { $schema->ensure_class_loaded('FakePackage::B'); };
95 like( $@, qr/Can't locate/,
96 'ensure_class_loaded threw exception for nonexistent class' );
97 ok( ! class_seems_loaded('DBICTest::FakeComponent'),
98 'DBICTest::FakeComponent not loaded yet' );
99 eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
100 ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
101 ok( class_seems_loaded('DBICTest::FakeComponent'),
102 'DBICTest::FakeComponent now loaded' );
105 # Squash warnings about syntax errors in SytaxErrorComponent.pm
106 local $SIG{__WARN__} = sigwarn_silencer(
107 qr/String found where operator expected|Missing operator before/
110 eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent1') };
111 like( $@, qr/syntax error/,
112 'ensure_class_loaded(DBICTest::SyntaxErrorComponent1) threw ok' );
113 eval { $schema->load_optional_class('DBICTest::SyntaxErrorComponent2') };
114 like( $@, qr/syntax error/,
115 'load_optional_class(DBICTest::SyntaxErrorComponent2) threw ok' );
120 package Fake::ResultSet;
122 use base 'DBIx::Class::ResultSet';
124 __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3');
127 # Make sure the errors in components of resultset classes are reported right.
128 like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right");