Lose another dependency: Class::Inspector
[dbsrgits/DBIx-Class.git] / xt / extra / internals / ensure_class_loaded.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use DBICTest;
9 use DBIx::Class::_Util 'sigwarn_silencer';
10 use DBICTest::Util 'class_seems_loaded';
11
12 BEGIN {
13   package TestPackage::A;
14   sub some_method {}
15 }
16
17 my $schema = DBICTest->init_schema();
18
19 plan tests => 28;
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_seems_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_seems_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') };
43 like( $@, qr/did not return a true value/,
44       'DBICTest::ErrorComponent threw ok' );
45
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
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' );
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
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"' );
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
90 # Test ensure_class_loaded
91 ok( class_seems_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_seems_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_seems_loaded('DBICTest::FakeComponent'),
102    'DBICTest::FakeComponent now loaded' );
103
104 {
105   # Squash warnings about syntax errors in SytaxErrorComponent.pm
106   local $SIG{__WARN__} = sigwarn_silencer(
107     qr/String found where operator expected|Missing operator before/
108   );
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' );
116 }
117
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
130 1;