include test for failure mode
[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 => 18;
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 {
20     return if $_[0] =~ /Subroutine (?:un|re|)initialize redefined at .*C3\.pm/;
21     $warnings++;
22 };
23
24 ok( !Class::MOP::is_class_loaded("TestApp::View::Dump"), "component not yet loaded" );
25
26 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump");
27
28 ok( Class::MOP::is_class_loaded("TestApp::View::Dump"), "loaded ok" );
29 is( $warnings, 0, "no warnings emitted" );
30
31 $warnings = 0;
32
33 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump");
34 is( $warnings, 0, "calling again doesn't reaload" );
35
36 ok( !Class::MOP::is_class_loaded("TestApp::View::Dump::Request"), "component not yet loaded" );
37
38 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump::Request");
39 ok( Class::MOP::is_class_loaded("TestApp::View::Dump::Request"), "loaded ok" );
40
41 is( $warnings, 0, "calling again doesn't reaload" );
42
43 undef $@;
44 eval { Catalyst::Utils::ensure_class_loaded("This::Module::Is::Probably::Not::There") };
45 ok( $@, "doesn't defatalize" );
46 like( $@, qr/There\.pm.*\@INC/, "error looks right" );
47
48 undef $@;
49 eval { Catalyst::Utils::ensure_class_loaded("__PACKAGE__") };
50 ok( $@, "doesn't defatalize" );
51 like( $@, qr/__PACKAGE__\.pm.*\@INC/, "errors sanely on __PACKAGE__.pm" );
52
53 $@ = "foo";
54 Catalyst::Utils::ensure_class_loaded("TestApp::View::Dump::Response");
55 is( $@, "foo", '$@ is untouched' );
56
57 undef $@;
58 eval { Catalyst::Utils::ensure_class_loaded("This::Module::Is::Not::In::Inc::But::Does::Exist") };
59 ok( !$@, "no error when loading non existent .pm that *does* have a symbol table entry" ); 
60
61 undef $@;
62 eval { Catalyst::Utils::ensure_class_loaded('Silly::File::.#Name') };
63 like($@, qr/Malformed class Name/, 'errored when attempting to load a file beginning with a .');
64
65 undef $@;
66 eval { Catalyst::Utils::ensure_class_loaded('Silly::File::Name.pm') };
67 like($@, qr/Malformed class Name/, 'errored sanely when given a classname ending in .pm');
68
69 undef $@;
70 $warnings = 0;
71 Catalyst::Utils::ensure_class_loaded("NullPackage");
72 is( $warnings, 1, 'Loading a package which defines no symbols warns');
73 is( $@, undef, '$@ still undef' );
74