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