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