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