Restore ability to handle underdefined root (t/prefetch/incomplete.t)
[dbsrgits/DBIx-Class.git] / t / 90ensure_class_loaded.t
CommitLineData
ae515736 1use strict;
8273e845 2use warnings;
ae515736 3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7use Class::Inspector;
8
9BEGIN {
10 package TestPackage::A;
11 sub some_method {}
12}
13
14my $schema = DBICTest->init_schema();
15
7dd382fb 16plan tests => 28;
efe6365b 17
18# Test ensure_class_found
19ok( $schema->ensure_class_found('DBIx::Class::Schema'),
20 'loaded package DBIx::Class::Schema was found' );
21ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
22 'DBICTest::FakeComponent not loaded yet' );
23ok( $schema->ensure_class_found('DBICTest::FakeComponent'),
24 'package DBICTest::FakeComponent was found' );
25ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
26 'DBICTest::FakeComponent not loaded by ensure_class_found()' );
27ok( $schema->ensure_class_found('TestPackage::A'),
28 'anonymous package TestPackage::A found' );
29ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'),
30 'fake package not found' );
31
32# Test load_optional_class
33my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') };
34ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
35ok( !$retval, 'nonexistent package not loaded' );
36$retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
37ok( !$@, 'load_optional_class on an existing class did not throw' );
38ok( $retval, 'DBICTest::OptionalComponent loaded' );
39eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
175e2616 40like( $@, qr/did not return a true value/,
41 'DBICTest::ErrorComponent threw ok' );
efe6365b 42
7dd382fb 43# Simulate a PAR environment
44{
45 my @code;
46 local @INC = @INC;
47 unshift @INC, sub {
48 if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') {
49 return (sub { return 0 unless @code; $_ = shift @code; 1; } );
50 }
51 else {
52 return ();
53 }
54 };
55
56 $retval = eval { $schema->load_optional_class('FAKE::PAR::PACKAGE') };
57 ok( !$@, 'load_optional_class on a nonexistent PAR class did not throw' );
58 ok( !$retval, 'nonexistent PAR package not loaded' );
59
60
61 # simulate a class which does load but does not return true
62 @code = (
63 q/package VIRTUAL::PAR::PACKAGE;/,
64 q/0;/,
65 );
66
504870a5 67 $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
68 ok( $@, 'load_optional_class of a no-true-returning PAR module did throw' );
69 ok( !$retval, 'no-true-returning PAR package not loaded' );
7dd382fb 70
71 # simulate a normal class (no one adjusted %INC so it will be tried again
72 @code = (
73 q/package VIRTUAL::PAR::PACKAGE;/,
74 q/1;/,
75 );
76
504870a5 77 $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') };
78 ok( !$@, 'load_optional_class of a PAR module did not throw' );
79 ok( $retval, 'PAR package "loaded"' );
7dd382fb 80
81 # see if we can still load stuff with the coderef present
82 $retval = eval { $schema->load_optional_class('DBIx::Class::ResultClass::HashRefInflator') };
83 ok( !$@, 'load_optional_class did not throw' ) || diag $@;
84 ok( $retval, 'DBIx::Class::ResultClass::HashRefInflator loaded' );
85}
86
efe6365b 87# Test ensure_class_loaded
88ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
89eval { $schema->ensure_class_loaded('TestPackage::A'); };
90ok( !$@, 'ensure_class_loaded detected an anon. class' );
91eval { $schema->ensure_class_loaded('FakePackage::B'); };
92like( $@, qr/Can't locate/,
93 'ensure_class_loaded threw exception for nonexistent class' );
94ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
95 'DBICTest::FakeComponent not loaded yet' );
96eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
97ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
98ok( Class::Inspector->loaded('DBICTest::FakeComponent'),
99 'DBICTest::FakeComponent now loaded' );
ae515736 100
175e2616 101{
102 # Squash warnings about syntax errors in SytaxErrorComponent.pm
103 local $SIG{__WARN__} = sub {
104 my $warning = shift;
105 warn $warning unless (
106 $warning =~ /String found where operator expected/ or
107 $warning =~ /Missing operator before/
108 );
109 };
9d3d92ab 110
111 eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent1') };
112 like( $@, qr/syntax error/,
113 'ensure_class_loaded(DBICTest::SyntaxErrorComponent1) threw ok' );
114 eval { $schema->load_optional_class('DBICTest::SyntaxErrorComponent2') };
115 like( $@, qr/syntax error/,
116 'load_optional_class(DBICTest::SyntaxErrorComponent2) threw ok' );
175e2616 117}
118
a63219bc 119
120eval {
121 package Fake::ResultSet;
122
123 use base 'DBIx::Class::ResultSet';
124
125 __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3');
126};
127
128# Make sure the errors in components of resultset classes are reported right.
129like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right");
130
ae515736 1311;