when checking @ISA to see if a class is loaded, make sure it actually has values...
[gitmo/Class-MOP.git] / t / 083_load_class.t
CommitLineData
3f7759c1 1use strict;
2use warnings;
d9d8a21b 3use Test::More tests => 36;
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
07940968 26my $meta = Class::MOP::load_class('BinaryTree');
27ok($meta, "successfully loaded the class BinaryTree");
28is($meta->name, "BinaryTree", "load_class returns the metaclass");
3f7759c1 29can_ok('BinaryTree' => 'traverse');
30
31do {
32 package Class;
33 sub method {}
34};
35
4c7e2bf4 36
37my $ret = Class::MOP::load_class('Class');
38ok($ret, "this should not die!");
39is( $ret, "Class", "class name returned" );
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}
65qr/Could not load class \(This::Does::Not::Exist\) because :/,
66 'Many Moose tests rely on the exact formatting of this error';
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
83isa_ok( Class::MOP::load_class("Lala"), "Class::MOP::Class", "when an object has a metaclass it is returned" );
5a24cf8a 84
85lives_ok {
1d8153bd 86 is(Class::MOP::load_first_existing_class("Lala", "Does::Not::Exist"), "Lala", 'load_first_existing_class 1/2 params ok, class name returned');
87 is(Class::MOP::load_first_existing_class("Does::Not::Exist", "Lala"), "Lala", 'load_first_existing_class 2/2 params ok, class name returned');
5a24cf8a 88} 'load_classes works';
1d8153bd 89
5a24cf8a 90throws_ok {
063ad0c5 91 Class::MOP::load_first_existing_class("Does::Not::Exist", "Also::Does::Not::Exist")
5a24cf8a 92} qr/Could not load class \(Does::Not::Exist.*Could not load class \(Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception';
93
b8575233 94{
95 sub whatever {
96 TestClassLoaded::this_method_does_not_even_exist();
97 }
98
99 ok( ! Class::MOP::is_class_loaded('TestClassLoaded'),
100 'the mere mention of TestClassLoaded in the whatever sub does not make us think it has been loaded' );
101}
5a24cf8a 102
7f820be6 103{
104 require TestClassLoaded::Sub;
105 ok( ! Class::MOP::is_class_loaded('TestClassLoaded'),
106 'requiring TestClassLoaded::Sub does not make us think TestClassLoaded is loaded' );
107}
108
109{
110 require TestClassLoaded;
111 ok( Class::MOP::is_class_loaded('TestClassLoaded'),
112 'We see that TestClassLoaded is loaded after requiring it (it has methods but no $VERSION or @ISA)' );
113}
114
115{
116 require TestClassLoaded2;
117 ok( Class::MOP::is_class_loaded('TestClassLoaded2'),
118 'We see that TestClassLoaded2 is loaded after requiring it (it has a $VERSION but no methods or @ISA)' );
119}
120
121{
122 require TestClassLoaded3;
123 ok( Class::MOP::is_class_loaded('TestClassLoaded3'),
124 'We see that TestClassLoaded3 is loaded after requiring it (it has an @ISA but no methods or $VERSION)' );
125}
d9d8a21b 126
127{
128 {
129 package Not::Loaded;
130 our @ISA;
131 }
132
133 ok( ! Class::MOP::is_class_loaded('Not::Loaded'),
134 'the mere existence of an @ISA for a package does not mean a class is loaded' );
135}
136
137{
138 {
139 package Loaded::Ish;
140 our @ISA = 'Foo';
141 }
142
143 ok( Class::MOP::is_class_loaded('Loaded::Ish'),
144 'an @ISA with members does mean a class is loaded' );
145}
146