Commit | Line | Data |
ae515736 |
1 | use strict; |
8273e845 |
2 | use warnings; |
ae515736 |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
052a832c |
7 | use DBIx::Class::_Util 'sigwarn_silencer'; |
ae515736 |
8 | use Class::Inspector; |
9 | |
10 | BEGIN { |
11 | package TestPackage::A; |
12 | sub some_method {} |
13 | } |
14 | |
15 | my $schema = DBICTest->init_schema(); |
16 | |
7dd382fb |
17 | plan tests => 28; |
efe6365b |
18 | |
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' ); |
32 | |
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') }; |
175e2616 |
41 | like( $@, qr/did not return a true value/, |
42 | 'DBICTest::ErrorComponent threw ok' ); |
efe6365b |
43 | |
7dd382fb |
44 | # Simulate a PAR environment |
45 | { |
46 | my @code; |
47 | local @INC = @INC; |
48 | unshift @INC, sub { |
49 | if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') { |
50 | return (sub { return 0 unless @code; $_ = shift @code; 1; } ); |
51 | } |
52 | else { |
53 | return (); |
54 | } |
55 | }; |
56 | |
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' ); |
60 | |
61 | |
62 | # simulate a class which does load but does not return true |
63 | @code = ( |
64 | q/package VIRTUAL::PAR::PACKAGE;/, |
65 | q/0;/, |
66 | ); |
67 | |
504870a5 |
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' ); |
7dd382fb |
71 | |
72 | # simulate a normal class (no one adjusted %INC so it will be tried again |
73 | @code = ( |
74 | q/package VIRTUAL::PAR::PACKAGE;/, |
75 | q/1;/, |
76 | ); |
77 | |
504870a5 |
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"' ); |
7dd382fb |
81 | |
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' ); |
86 | } |
87 | |
efe6365b |
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' ); |
ae515736 |
101 | |
175e2616 |
102 | { |
103 | # Squash warnings about syntax errors in SytaxErrorComponent.pm |
052a832c |
104 | local $SIG{__WARN__} = sigwarn_silencer( |
105 | qr/String found where operator expected|Missing operator before/ |
106 | ); |
9d3d92ab |
107 | |
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' ); |
175e2616 |
114 | } |
115 | |
a63219bc |
116 | |
117 | eval { |
118 | package Fake::ResultSet; |
119 | |
120 | use base 'DBIx::Class::ResultSet'; |
121 | |
122 | __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3'); |
123 | }; |
124 | |
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"); |
127 | |
ae515736 |
128 | 1; |