Revert "convert all uses of Test::Exception to Test::Fatal."
[gitmo/Class-MOP.git] / t / 083_load_class.t
CommitLineData
3f7759c1 1use strict;
2use warnings;
86a4d873 3use Test::More;
8371f3de 4use Test::Exception;
3f7759c1 5
6require Class::MOP;
7use lib 't/lib';
8
8371f3de 9dies_ok {
bd0ad278 10 Class::MOP::is_class_loaded()
8371f3de 11} "is_class_loaded with no argument dies";
bd0ad278 12
3f7759c1 13ok(!Class::MOP::is_class_loaded(''), "can't load the empty class");
14ok(!Class::MOP::is_class_loaded(\"foo"), "can't load a class name reference??");
15
2c0fb064 16ok(!Class::MOP::_is_valid_class_name(undef), 'undef is not a valid class name');
17ok(!Class::MOP::_is_valid_class_name(''), 'empty string is not a valid class name');
18ok(!Class::MOP::_is_valid_class_name(\"foo"), 'a reference is not a valid class name');
19ok(!Class::MOP::_is_valid_class_name('bogus name'), q{'bogus name' is not a valid class name});
20ok(Class::MOP::_is_valid_class_name('Foo'), q{'Foo' is a valid class name});
21ok(Class::MOP::_is_valid_class_name('Foo::Bar'), q{'Foo::Bar' is a valid class name});
22ok(Class::MOP::_is_valid_class_name('Foo_::Bar2'), q{'Foo_::Bar2' is a valid class name});
8371f3de 23throws_ok { Class::MOP::load_class('bogus name') } qr/Invalid class name \(bogus name\)/;
3f7759c1 24
8371f3de 25throws_ok {
9bbf7fe2 26 Class::MOP::load_class('__PACKAGE__')
8371f3de 27} qr/__PACKAGE__\.pm.*\@INC/, 'errors sanely on __PACKAGE__.pm';
9bbf7fe2 28
7716a8f9 29Class::MOP::load_class('BinaryTree');
3f7759c1 30can_ok('BinaryTree' => 'traverse');
31
32do {
33 package Class;
34 sub method {}
35};
36
4c7e2bf4 37
7716a8f9 38{
39 local $@;
40 eval { Class::MOP::load_class('Class') };
9914477f 41 ok( ! $@, 'load_class does not die if the package is already defined' );
7716a8f9 42}
4c7e2bf4 43
44ok( !Class::MOP::does_metaclass_exist("Class"), "no metaclass for non MOP class" );
3f7759c1 45
8371f3de 46throws_ok {
3f7759c1 47 Class::MOP::load_class('FakeClassOhNo');
8371f3de 48}
202ccce0 49qr/Can't locate /;
3f7759c1 50
8371f3de 51throws_ok {
3f7759c1 52 Class::MOP::load_class('SyntaxError');
8371f3de 53}
202ccce0 54qr/Missing right curly/;
3f7759c1 55
8371f3de 56throws_ok {
44da14be 57 delete $INC{'SyntaxError.pm'};
58 Class::MOP::load_first_existing_class(
59 'FakeClassOhNo', 'SyntaxError', 'Class'
60 );
8371f3de 61}
2beec805 62qr/Missing right curly/,
44da14be 63 'load_first_existing_class does not pass over an existing (bad) module';
64
8371f3de 65throws_ok {
fea44045 66 Class::MOP::load_class('This::Does::Not::Exist');
8371f3de 67}
a02f24cb 68qr{Can't locate This/Does/Not/Exist\.pm in \@INC},
69 'load_first_existing_class throws a familiar error for a single module';
fea44045 70
9e275e86 71{
72 package Other;
73 use constant foo => "bar";
74}
75
8371f3de 76lives_ok {
2c0fb064 77 ok(Class::MOP::is_class_loaded("Other"), 'is_class_loaded(Other)');
8371f3de 78}
b3b7a216 79"a class with just constants is still a class";
4c7e2bf4 80
81{
82 package Lala;
83 use metaclass;
84}
85
8371f3de 86lives_ok {
1d8153bd 87 is(Class::MOP::load_first_existing_class("Lala", "Does::Not::Exist"), "Lala", 'load_first_existing_class 1/2 params ok, class name returned');
88 is(Class::MOP::load_first_existing_class("Does::Not::Exist", "Lala"), "Lala", 'load_first_existing_class 2/2 params ok, class name returned');
8371f3de 89} 'load_classes works';
1d8153bd 90
8371f3de 91throws_ok {
063ad0c5 92 Class::MOP::load_first_existing_class("Does::Not::Exist", "Also::Does::Not::Exist")
8371f3de 93} qr/Does::Not::Exist.*Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception';
5a24cf8a 94
b8575233 95{
96 sub whatever {
97 TestClassLoaded::this_method_does_not_even_exist();
98 }
99
100 ok( ! Class::MOP::is_class_loaded('TestClassLoaded'),
101 'the mere mention of TestClassLoaded in the whatever sub does not make us think it has been loaded' );
102}
5a24cf8a 103
7f820be6 104{
105 require TestClassLoaded::Sub;
106 ok( ! Class::MOP::is_class_loaded('TestClassLoaded'),
107 'requiring TestClassLoaded::Sub does not make us think TestClassLoaded is loaded' );
108}
109
110{
111 require TestClassLoaded;
112 ok( Class::MOP::is_class_loaded('TestClassLoaded'),
113 'We see that TestClassLoaded is loaded after requiring it (it has methods but no $VERSION or @ISA)' );
114}
115
116{
117 require TestClassLoaded2;
118 ok( Class::MOP::is_class_loaded('TestClassLoaded2'),
119 'We see that TestClassLoaded2 is loaded after requiring it (it has a $VERSION but no methods or @ISA)' );
120}
121
122{
123 require TestClassLoaded3;
124 ok( Class::MOP::is_class_loaded('TestClassLoaded3'),
125 'We see that TestClassLoaded3 is loaded after requiring it (it has an @ISA but no methods or $VERSION)' );
126}
d9d8a21b 127
128{
129 {
130 package Not::Loaded;
131 our @ISA;
132 }
133
134 ok( ! Class::MOP::is_class_loaded('Not::Loaded'),
135 'the mere existence of an @ISA for a package does not mean a class is loaded' );
136}
137
138{
139 {
140 package Loaded::Ish;
141 our @ISA = 'Foo';
142 }
143
144 ok( Class::MOP::is_class_loaded('Loaded::Ish'),
145 'an @ISA with members does mean a class is loaded' );
146}
147
4154c4d0 148{
149 {
150 package Class::WithVersion;
151 our $VERSION = 23;
152 };
153
154 ok( Class::MOP::is_class_loaded('Class::WithVersion', { -version => 13 }),
155 'version 23 satisfies version requirement 13' );
156
157 ok( !Class::MOP::is_class_loaded('Class::WithVersion', { -version => 42 }),
158 'version 23 does not satisfy version requirement 42' );
159
8371f3de 160 throws_ok {
4154c4d0 161 Class::MOP::load_first_existing_class('Affe', 'Tiger', 'Class::WithVersion' => { -version => 42 });
8371f3de 162 } qr/Class::WithVersion version 42 required--this is only version 23/,
4154c4d0 163 'load_first_existing_class gives correct exception on old version';
164
8371f3de 165 lives_ok {
4154c4d0 166 Class::MOP::load_first_existing_class('Affe', 'Tiger', 'Class::WithVersion' => { -version => 13 });
8371f3de 167 } 'loading class with required version with load_first_existing_class';
4154c4d0 168
8371f3de 169 throws_ok {
4154c4d0 170 Class::MOP::load_class('Class::WithVersion' => { -version => 42 });
8371f3de 171 } qr/Class::WithVersion version 42 required--this is only version 23/,
4154c4d0 172 'load_class gives correct exception on old version';
173
8371f3de 174 lives_ok {
4154c4d0 175 Class::MOP::load_class('Class::WithVersion' => { -version => 13 });
8371f3de 176 } 'loading class with required version with load_class';
4154c4d0 177
178}
179
86a4d873 180done_testing;