Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / t / 90ensure_class_loaded.t
CommitLineData
ae515736 1use strict;
2use warnings;
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
a63219bc 16plan tests => 20;
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
43# Test ensure_class_loaded
44ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
45eval { $schema->ensure_class_loaded('TestPackage::A'); };
46ok( !$@, 'ensure_class_loaded detected an anon. class' );
47eval { $schema->ensure_class_loaded('FakePackage::B'); };
48like( $@, qr/Can't locate/,
49 'ensure_class_loaded threw exception for nonexistent class' );
50ok( !Class::Inspector->loaded('DBICTest::FakeComponent'),
51 'DBICTest::FakeComponent not loaded yet' );
52eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); };
53ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
54ok( Class::Inspector->loaded('DBICTest::FakeComponent'),
55 'DBICTest::FakeComponent now loaded' );
ae515736 56
175e2616 57{
58 # Squash warnings about syntax errors in SytaxErrorComponent.pm
59 local $SIG{__WARN__} = sub {
60 my $warning = shift;
61 warn $warning unless (
62 $warning =~ /String found where operator expected/ or
63 $warning =~ /Missing operator before/
64 );
65 };
9d3d92ab 66
67 eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent1') };
68 like( $@, qr/syntax error/,
69 'ensure_class_loaded(DBICTest::SyntaxErrorComponent1) threw ok' );
70 eval { $schema->load_optional_class('DBICTest::SyntaxErrorComponent2') };
71 like( $@, qr/syntax error/,
72 'load_optional_class(DBICTest::SyntaxErrorComponent2) threw ok' );
175e2616 73}
74
a63219bc 75
76eval {
77 package Fake::ResultSet;
78
79 use base 'DBIx::Class::ResultSet';
80
81 __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3');
82};
83
84# Make sure the errors in components of resultset classes are reported right.
85like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right");
86
ae515736 871;