Make get_method_map private (as _full_method_map) and deprecate the public
[gitmo/Class-MOP.git] / t / 083_load_class.t
1 use strict;
2 use warnings;
3 use Test::More tests => 32;
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 Class::MOP::load_class('BinaryTree');
27 can_ok('BinaryTree' => 'traverse');
28
29 do {
30     package Class;
31     sub method {}
32 };
33
34
35 {
36     local $@;
37     eval { Class::MOP::load_class('Class') };
38     ok( ! $@, 'load_class does not die if the package is already defined' );
39 }
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     delete $INC{'SyntaxError.pm'};
55     Class::MOP::load_first_existing_class(
56         'FakeClassOhNo', 'SyntaxError', 'Class'
57     );
58 }
59 qr/Missing right curly/,
60     'load_first_existing_class does not pass over an existing (bad) module';
61
62 throws_ok {
63     Class::MOP::load_class('This::Does::Not::Exist');
64 }
65 qr/Could not load class \(This::Does::Not::Exist\) because :/,
66     'Many Moose tests rely on the exact formatting of this error';
67
68 {
69     package Other;
70     use constant foo => "bar";
71 }
72
73 lives_ok {
74     ok(Class::MOP::is_class_loaded("Other"), 'is_class_loaded(Other)');
75 }
76 "a class with just constants is still a class";
77
78 {
79     package Lala;
80     use metaclass;
81 }
82
83 lives_ok {
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');
86 } 'load_classes works';
87
88 throws_ok {
89     Class::MOP::load_first_existing_class("Does::Not::Exist", "Also::Does::Not::Exist")
90 } qr/Could not load class \(Does::Not::Exist.*Could not load class \(Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception';
91
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 }
100
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 }
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