deprecate compute_all_applicable_attributes
[gitmo/Class-MOP.git] / t / 083_load_class.t
1 use strict;
2 use warnings;
3 use Test::More tests => 33;
4 use Test::Exception;
5
6 require Class::MOP;
7 use lib 't/lib';
8
9 ok(!Class::MOP::is_class_loaded(), "is_class_loaded with no argument returns false");
10 ok(!Class::MOP::is_class_loaded(''), "can't load the empty class");
11 ok(!Class::MOP::is_class_loaded(\"foo"), "can't load a class name reference??");
12
13 ok(!Class::MOP::_is_valid_class_name(undef), 'undef is not a valid class name');
14 ok(!Class::MOP::_is_valid_class_name(''), 'empty string is not a valid class name');
15 ok(!Class::MOP::_is_valid_class_name(\"foo"), 'a reference is not a valid class name');
16 ok(!Class::MOP::_is_valid_class_name('bogus name'), q{'bogus name' is not a valid class name});
17 ok(Class::MOP::_is_valid_class_name('Foo'), q{'Foo' is a valid class name});
18 ok(Class::MOP::_is_valid_class_name('Foo::Bar'), q{'Foo::Bar' is a valid class name});
19 ok(Class::MOP::_is_valid_class_name('Foo_::Bar2'), q{'Foo_::Bar2' is a valid class name});
20 throws_ok { Class::MOP::load_class('bogus name') } qr/Invalid class name \(bogus name\)/;
21
22 throws_ok {
23     Class::MOP::load_class('__PACKAGE__')
24 } qr/__PACKAGE__\.pm.*\@INC/, 'errors sanely on __PACKAGE__.pm';
25
26 my $meta = Class::MOP::load_class('BinaryTree');
27 ok($meta, "successfully loaded the class BinaryTree");
28 is($meta->name, "BinaryTree", "load_class returns the metaclass");
29 can_ok('BinaryTree' => 'traverse');
30
31 do {
32     package Class;
33     sub method {}
34 };
35
36
37 my $ret = Class::MOP::load_class('Class');
38 ok($ret, "this should not die!");
39 is( $ret, "Class", "class name returned" );
40
41 ok( !Class::MOP::does_metaclass_exist("Class"), "no metaclass for non MOP class" );
42
43 throws_ok {
44     Class::MOP::load_class('FakeClassOhNo');
45 }
46 qr/Can't locate /;
47
48 throws_ok {
49     Class::MOP::load_class('SyntaxError');
50 }
51 qr/Missing right curly/;
52
53 throws_ok {
54     Class::MOP::load_class('This::Does::Not::Exist');
55 }
56 qr/Could not load class \(This::Does::Not::Exist\) because :/,
57     'Many Moose tests rely on the exact formatting of this error';
58
59 {
60     package Other;
61     use constant foo => "bar";
62 }
63
64 lives_ok {
65     ok(Class::MOP::is_class_loaded("Other"), 'is_class_loaded(Other)');
66 }
67 "a class with just constants is still a class";
68
69 {
70     package Lala;
71     use metaclass;
72 }
73
74 isa_ok( Class::MOP::load_class("Lala"), "Class::MOP::Class", "when an object has a metaclass it is returned" );
75
76 lives_ok {
77     is(Class::MOP::load_first_existing_class("Lala", "Does::Not::Exist"), "Lala", 'load_first_existing_class 1/2 params ok, class name returned');
78     is(Class::MOP::load_first_existing_class("Does::Not::Exist", "Lala"), "Lala", 'load_first_existing_class 2/2 params ok, class name returned');
79 } 'load_classes works';
80
81 throws_ok {
82     Class::MOP::load_first_existing_class("Does::Not::Exist", "Also::Does::Not::Exist")
83 } qr/Could not load class \(Does::Not::Exist.*Could not load class \(Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception';
84
85 {
86     sub whatever {
87         TestClassLoaded::this_method_does_not_even_exist();
88     }
89
90     ok( ! Class::MOP::is_class_loaded('TestClassLoaded'),
91         'the mere mention of TestClassLoaded in the whatever sub does not make us think it has been loaded' );
92 }
93
94 {
95     require TestClassLoaded::Sub;
96     ok( ! Class::MOP::is_class_loaded('TestClassLoaded'),
97         'requiring TestClassLoaded::Sub does not make us think TestClassLoaded is loaded' );
98 }
99
100 {
101     require TestClassLoaded;
102     ok( Class::MOP::is_class_loaded('TestClassLoaded'),
103         'We see that TestClassLoaded is loaded after requiring it (it has methods but no $VERSION or @ISA)' );
104 }
105
106 {
107     require TestClassLoaded2;
108     ok( Class::MOP::is_class_loaded('TestClassLoaded2'),
109         'We see that TestClassLoaded2 is loaded after requiring it (it has a $VERSION but no methods or @ISA)' );
110 }
111
112 {
113     require TestClassLoaded3;
114     ok( Class::MOP::is_class_loaded('TestClassLoaded3'),
115         'We see that TestClassLoaded3 is loaded after requiring it (it has an @ISA but no methods or $VERSION)' );
116 }