Commit | Line | Data |
c0329273 |
1 | BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } |
2 | |
ae515736 |
3 | use strict; |
8273e845 |
4 | use warnings; |
ae515736 |
5 | |
6 | use Test::More; |
c0329273 |
7 | |
ae515736 |
8 | use DBICTest; |
052a832c |
9 | use DBIx::Class::_Util 'sigwarn_silencer'; |
ae515736 |
10 | use Class::Inspector; |
11 | |
12 | BEGIN { |
13 | package TestPackage::A; |
14 | sub some_method {} |
15 | } |
16 | |
17 | my $schema = DBICTest->init_schema(); |
18 | |
7dd382fb |
19 | plan tests => 28; |
efe6365b |
20 | |
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::Inspector->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::Inspector->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' ); |
34 | |
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') }; |
175e2616 |
43 | like( $@, 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 |
91 | ok( Class::Inspector->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::Inspector->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::Inspector->loaded('DBICTest::FakeComponent'), |
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 | |
119 | eval { |
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. |
128 | like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right"); |
129 | |
ae515736 |
130 | 1; |