Stop using Class::Inspector in the testsuite.
[catagits/Catalyst-Runtime.git] / t / unit_utils_load_class.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 16;
7 use Class::MOP;
8
9 use lib "t/lib";
10
11 BEGIN { use_ok("Catalyst::Utils") };
12
13 {
14     package This::Module::Is::Not::In::Inc::But::Does::Exist;
15     sub moose {};
16 }
17
18 my $warnings = 0;
19 $SIG{__WARN__} = sub { $warnings++ };
20
21 ok( !Class::MOP::is_class_loaded("TestApp::View::Dump"), "component not yet loaded" );
22
23 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump");
24
25 ok( Class::MOP::is_class_loaded("TestApp::View::Dump"), "loaded ok" );
26 is( $warnings, 0, "no warnings emitted" );
27
28 $warnings = 0;
29
30 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump");
31 is( $warnings, 0, "calling again doesn't reaload" );
32
33 ok( !Class::MOP::is_class_loaded("TestApp::View::Dump::Request"), "component not yet loaded" );
34
35 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump::Request");
36 ok( Class::MOP::is_class_loaded("TestApp::View::Dump::Request"), "loaded ok" );
37
38 is( $warnings, 0, "calling again doesn't reaload" );
39
40 undef $@;
41 eval { Catalyst::Utils::ensure_class_loaded("This::Module::Is::Probably::Not::There") };
42 ok( $@, "doesn't defatalize" );
43 like( $@, qr/There\.pm.*\@INC/, "error looks right" );
44
45 undef $@;
46 eval { Catalyst::Utils::ensure_class_loaded("__PACKAGE__") };
47 ok( $@, "doesn't defatalize" );
48 like( $@, qr/__PACKAGE__\.pm.*\@INC/, "errors sanely on __PACKAGE__.pm" );
49
50 $@ = "foo";
51 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump::Response");
52 is( $@, "foo", '$@ is untouched' );
53
54 undef $@;
55 eval { Catalyst::Utils::ensure_class_loaded("This::Module::Is::Not::In::Inc::But::Does::Exist") };
56 ok( !$@, "no error when loading non existent .pm that *does* have a symbol table entry" ); 
57
58 undef $@;
59 eval { Catalyst::Utils::ensure_class_loaded('Silly::File::.#Name') };
60 like($@, qr/Malformed class Name/, 'errored when attempting to load a file beginning with a .');
61
62 undef $@;
63 eval { Catalyst::Utils::ensure_class_loaded('Silly::File::Name.pm') };
64 like($@, qr/Malformed class Name/, 'errored sanely when given a classname ending in .pm');
65