Commit | Line | Data |
ae515736 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | use Class::Inspector; |
8 | |
9 | BEGIN { |
10 | package TestPackage::A; |
11 | sub some_method {} |
12 | } |
13 | |
14 | my $schema = DBICTest->init_schema(); |
15 | |
7dd382fb |
16 | plan tests => 28; |
efe6365b |
17 | |
18 | # Test ensure_class_found |
19 | ok( $schema->ensure_class_found('DBIx::Class::Schema'), |
20 | 'loaded package DBIx::Class::Schema was found' ); |
21 | ok( !Class::Inspector->loaded('DBICTest::FakeComponent'), |
22 | 'DBICTest::FakeComponent not loaded yet' ); |
23 | ok( $schema->ensure_class_found('DBICTest::FakeComponent'), |
24 | 'package DBICTest::FakeComponent was found' ); |
25 | ok( !Class::Inspector->loaded('DBICTest::FakeComponent'), |
26 | 'DBICTest::FakeComponent not loaded by ensure_class_found()' ); |
27 | ok( $schema->ensure_class_found('TestPackage::A'), |
28 | 'anonymous package TestPackage::A found' ); |
29 | ok( !$schema->ensure_class_found('FAKE::WONT::BE::FOUND'), |
30 | 'fake package not found' ); |
31 | |
32 | # Test load_optional_class |
33 | my $retval = eval { $schema->load_optional_class('ANOTHER::FAKE::PACKAGE') }; |
34 | ok( !$@, 'load_optional_class on a nonexistent class did not throw' ); |
35 | ok( !$retval, 'nonexistent package not loaded' ); |
36 | $retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') }; |
37 | ok( !$@, 'load_optional_class on an existing class did not throw' ); |
38 | ok( $retval, 'DBICTest::OptionalComponent loaded' ); |
39 | eval { $schema->load_optional_class('DBICTest::ErrorComponent') }; |
175e2616 |
40 | like( $@, 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 | |
624764ae |
67 | TODO: { |
68 | local $TODO = "Current load_optional_class cannot find PAR"; |
69 | $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') }; |
70 | ok( $@, 'load_optional_class of a no-true-returning PAR module did throw' ); |
71 | ok( !$retval, 'no-true-returning PAR package not loaded' ); |
72 | } |
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 | |
624764ae |
80 | TODO: { |
81 | local $TODO = "Current load_optional_class cannot find PARs"; |
82 | $retval = eval { $schema->load_optional_class('VIRTUAL::PAR::PACKAGE') }; |
83 | ok( !$@, 'load_optional_class of a PAR module did not throw' ); |
84 | ok( $retval, 'PAR package "loaded"' ); |
85 | } |
7dd382fb |
86 | |
87 | # see if we can still load stuff with the coderef present |
88 | $retval = eval { $schema->load_optional_class('DBIx::Class::ResultClass::HashRefInflator') }; |
89 | ok( !$@, 'load_optional_class did not throw' ) || diag $@; |
90 | ok( $retval, 'DBIx::Class::ResultClass::HashRefInflator loaded' ); |
91 | } |
92 | |
efe6365b |
93 | # Test ensure_class_loaded |
94 | ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' ); |
95 | eval { $schema->ensure_class_loaded('TestPackage::A'); }; |
96 | ok( !$@, 'ensure_class_loaded detected an anon. class' ); |
97 | eval { $schema->ensure_class_loaded('FakePackage::B'); }; |
98 | like( $@, qr/Can't locate/, |
99 | 'ensure_class_loaded threw exception for nonexistent class' ); |
100 | ok( !Class::Inspector->loaded('DBICTest::FakeComponent'), |
101 | 'DBICTest::FakeComponent not loaded yet' ); |
102 | eval { $schema->ensure_class_loaded('DBICTest::FakeComponent'); }; |
103 | ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' ); |
104 | ok( Class::Inspector->loaded('DBICTest::FakeComponent'), |
105 | 'DBICTest::FakeComponent now loaded' ); |
ae515736 |
106 | |
175e2616 |
107 | { |
108 | # Squash warnings about syntax errors in SytaxErrorComponent.pm |
109 | local $SIG{__WARN__} = sub { |
110 | my $warning = shift; |
111 | warn $warning unless ( |
112 | $warning =~ /String found where operator expected/ or |
113 | $warning =~ /Missing operator before/ |
114 | ); |
115 | }; |
9d3d92ab |
116 | |
117 | eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent1') }; |
118 | like( $@, qr/syntax error/, |
119 | 'ensure_class_loaded(DBICTest::SyntaxErrorComponent1) threw ok' ); |
120 | eval { $schema->load_optional_class('DBICTest::SyntaxErrorComponent2') }; |
121 | like( $@, qr/syntax error/, |
122 | 'load_optional_class(DBICTest::SyntaxErrorComponent2) threw ok' ); |
175e2616 |
123 | } |
124 | |
a63219bc |
125 | |
126 | eval { |
127 | package Fake::ResultSet; |
128 | |
129 | use base 'DBIx::Class::ResultSet'; |
130 | |
131 | __PACKAGE__->load_components('+DBICTest::SyntaxErrorComponent3'); |
132 | }; |
133 | |
134 | # Make sure the errors in components of resultset classes are reported right. |
135 | like($@, qr!\Qsyntax error at t/lib/DBICTest/SyntaxErrorComponent3.pm!, "Errors from RS components reported right"); |
136 | |
ae515736 |
137 | 1; |