Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / xt / extra / internals / ensure_class_loaded.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
ae515736 3use strict;
8273e845 4use warnings;
ae515736 5
6use Test::More;
c0329273 7
ae515736 8use DBICTest;
052a832c 9use DBIx::Class::_Util 'sigwarn_silencer';
817ac9e9 10use DBICTest::Util 'class_seems_loaded';
ae515736 11
12BEGIN {
13 package TestPackage::A;
14 sub some_method {}
15}
16
17my $schema = DBICTest->init_schema();
18
7dd382fb 19plan tests => 28;
efe6365b 20
21# Test ensure_class_found
22ok( $schema->ensure_class_found('DBIx::Class::Schema'),
23 'loaded package DBIx::Class::Schema was found' );
817ac9e9 24ok( ! class_seems_loaded('DBICTest::FakeComponent'),
efe6365b 25 'DBICTest::FakeComponent not loaded yet' );
26ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
27 'package DBICTest::FakeComponent was found' );
817ac9e9 28ok( ! class_seems_loaded('DBICTest::FakeComponent'),
efe6365b 29 'DBICTest::FakeComponent not loaded by ensure_class_found()' );
30ok( $schema->ensure_class_found('TestPackage::A'),
31 'anonymous package TestPackage::A found' );
32ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
33 'fake package not found' );
34
35# Test load_optional_class
36my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
37ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
38ok( !$retval, 'nonexistent package not loaded' );
39$retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
40ok( !$@, 'load_optional_class on an existing class did not throw' );
41ok( $retval, 'DBICTest::OptionalComponent loaded' );
42eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
175e2616 43like( $@, qr/did not return a true value/,
44 'DBICTest::ErrorComponent threw ok' );
efe6365b 45
7dd382fb 46# Simulate a PAR environment
47{
48 my @code;
49 local @INC = @INC;
50 unshift @INC, sub {
51 if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') {
52 return (sub { return 0 unless @code; $_ = shift @code; 1; } );
53 }
54 else {
55 return ();
56 }
57 };
58
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' );
62
63
64 # simulate a class which does load but does not return true
65 @code = (
66 q/package VIRTUAL::PAR::PACKAGE;/,
67 q/0;/,
68 );
69
504870a5 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' );
7dd382fb 73
74 # simulate a normal class (no one adjusted %INC so it will be tried again
75 @code = (
76 q/package VIRTUAL::PAR::PACKAGE;/,
77 q/1;/,
78 );
79
504870a5 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"' );
7dd382fb 83
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' );
88}
89
efe6365b 90# Test ensure_class_loaded
817ac9e9 91ok( class_seems_loaded('TestPackage::A'), 'anonymous package exists' );
efe6365b 92eval { $schema->ensure_class_loaded('TestPackage::A'); };
93ok( !$@, 'ensure_class_loaded detected an anon. class' );
94eval { $schema->ensure_class_loaded('FakePackage::B'); };
95like( $@, qr/Can't locate/,
96 'ensure_class_loaded threw exception for nonexistent class' );
817ac9e9 97ok( ! class_seems_loaded('DBICTest::FakeComponent'),
efe6365b 98 'DBICTest::FakeComponent not loaded yet' );
99eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
100ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
817ac9e9 101ok( class_seems_loaded('DBICTest::FakeComponent'),
efe6365b 102 'DBICTest::FakeComponent now loaded' );
ae515736 103
175e2616 104{
105 # Squash warnings about syntax errors in SytaxErrorComponent.pm
052a832c 106 local $SIG{__WARN__} = sigwarn_silencer(
107 qr/String found where operator expected|Missing operator before/
108 );
9d3d92ab 109
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' );
175e2616 116}
117
a63219bc 118
119eval {
120 package Fake::ResultSet;
121
122 use base 'DBIx::Class::ResultSet';
123
124 __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3');
125};
126
127# Make sure the errors in components of resultset classes are reported right.
128like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right");
129
ae515736 1301;